一、面向?qū)ο?/h2>
EG:舉個例子,造一輛小汽車
1.畫圖紙
(1)定義車的屬性:顏色,速度,幾座,牌子。
(2)定義車的行為:跑。
2.拿著圖紙生產(chǎn)汽車
1.面向?qū)ο?/h6>
2.類:圖紙
(1)面向?qū)ο蟮膶傩裕哼@一類事物具有相同的屬性。
(2)面向?qū)ο蟮膭幼鳎哼@一類事物具有相同的行為。
3.對象:類的具體化,具體某一樣?xùn)|西。
4.傳參
public class Car {
String color;
int speed;
int seat;
public void fly(String color){
System.out.println(color+"\t顏色的車跑得快");
}
public static void main(String[] args) {
Car c=new Car();
c.speed=100;
c.color="red";
c.fly("黑色");
}
}
5.this關(guān)鍵字,指的就是當(dāng)前類的對象。
public class Car {
String color;
int speed;
int seat;
public void run(){
System.out.println("車能跑");
System.out.println(this);
System.out.println(this.color);
}
public void fly(String color){
System.out.println(color+"\t顏色的車跑得快");
}
public static void main(String[] args) {
Car c=new Car();
c.speed=100;
c.color="red";
System.out.println(c.color);
c.run();
c.fly("黑色");
}
}
3.構(gòu)造方法
1.創(chuàng)建對象時自動調(diào)用的無參構(gòu)造方法。
2.形式:public xxx(){} 作用:初始化對象
3.與類名一致
public class Car {
String color;
int speed;
int seat;
public void run(){
System.out.println(this.color+"顏色的車在跑");
System.out.println(this.speed+"速度的車在跑");
System.out.println(this.seat+"座位的車在跑");
}
public Car(String color,int speed,int seat){ //構(gòu)造方法。
this.color=color;
this.speed=speed;
this.seat=seat;
}
public static void main(String[] args) {
Car c=new Car("紅色",120,10);
Car c1=new Car("綠色",180,15);
c.run();
c1.run();
}
}
4.構(gòu)造方法也可以進(jìn)行重載
public class sportCar {
String name; //這里的name=this.name;
int speed;
String color;
int seat;
String country;
public sportCar(String name,int speed,String color,String country){
this.name=name;
this.speed=speed;
this.color=color;
this.country=country;
}
public sportCar(String name,int speed,String country){
this.name=name;
this.speed=speed;
this.country=country;
}
public static void main(String[] args) {
sportCar car1=new sportCar("阿斯頓馬丁",110,"黑色","英國");
sportCar car2=new sportCar("蘭博基尼",200,"意大利");
}
}
5.構(gòu)造方法一樣,參數(shù)不同,這就叫重載。同名不同參。
6.比如兩個方法中有大量重疊的參數(shù),可以簡寫。
public class sportCar {
String name;
int speed;
String color;
int seat;
String country;
public sportCar(String name,int speed,String color,String country){
this(name,speed,country);//簡寫,省略大量ctrl c ctrl v。
this.color=color;
}
public sportCar(String name,int speed,String country){
this.name=name;
this.speed=speed;
this.country=country;
}
public static void main(String[] args) {
sportCar car1=new sportCar("阿斯頓馬丁",110,"黑色","英國");
sportCar car2=new sportCar("蘭博基尼",200,"意大利");
}
}
7.EG:注意new對象時候,Hero(),構(gòu)造方法此時不能為空,必須有參,因?yàn)樯厦嬉呀?jīng)定義,此時狀態(tài)不是默認(rèn)狀態(tài)。
public class Hero {
String name;
String skill_w;
String skill_a;
String skill_d;
String skill_s;
//構(gòu)造方法,兩個,同名不同參數(shù)
public Hero(String name){
this.name=name;
}
public Hero(String name,String skill_a,String skill_d,String skill_s,String skill_w){
this(name);
this.skill_a=skill_a;
this.skill_d=skill_d;
this.skill_s=skill_s;
this.skill_w=skill_w;
}
//方法
public void fight(){
System.out.println(this.name+"英雄的移動技巧");
}
public static void main(String[] args) {
Hero h1=new Hero("亞索","左移","右移","后退","前進(jìn)");
h1.fight();
}
}
8.蜘蛛俠和滅霸例子
2.類:圖紙
(1)面向?qū)ο蟮膶傩裕哼@一類事物具有相同的屬性。
(2)面向?qū)ο蟮膭幼鳎哼@一類事物具有相同的行為。
3.對象:類的具體化,具體某一樣?xùn)|西。
4.傳參
public class Car {
String color;
int speed;
int seat;
public void fly(String color){
System.out.println(color+"\t顏色的車跑得快");
}
public static void main(String[] args) {
Car c=new Car();
c.speed=100;
c.color="red";
c.fly("黑色");
}
}
5.this關(guān)鍵字,指的就是當(dāng)前類的對象。
public class Car {
String color;
int speed;
int seat;
public void run(){
System.out.println("車能跑");
System.out.println(this);
System.out.println(this.color);
}
public void fly(String color){
System.out.println(color+"\t顏色的車跑得快");
}
public static void main(String[] args) {
Car c=new Car();
c.speed=100;
c.color="red";
System.out.println(c.color);
c.run();
c.fly("黑色");
}
}
3.構(gòu)造方法
1.創(chuàng)建對象時自動調(diào)用的無參構(gòu)造方法。
2.形式:public xxx(){} 作用:初始化對象
3.與類名一致
public class Car {
String color;
int speed;
int seat;
public void run(){
System.out.println(this.color+"顏色的車在跑");
System.out.println(this.speed+"速度的車在跑");
System.out.println(this.seat+"座位的車在跑");
}
public Car(String color,int speed,int seat){ //構(gòu)造方法。
this.color=color;
this.speed=speed;
this.seat=seat;
}
public static void main(String[] args) {
Car c=new Car("紅色",120,10);
Car c1=new Car("綠色",180,15);
c.run();
c1.run();
}
}
4.構(gòu)造方法也可以進(jìn)行重載
public class sportCar {
String name; //這里的name=this.name;
int speed;
String color;
int seat;
String country;
public sportCar(String name,int speed,String color,String country){
this.name=name;
this.speed=speed;
this.color=color;
this.country=country;
}
public sportCar(String name,int speed,String country){
this.name=name;
this.speed=speed;
this.country=country;
}
public static void main(String[] args) {
sportCar car1=new sportCar("阿斯頓馬丁",110,"黑色","英國");
sportCar car2=new sportCar("蘭博基尼",200,"意大利");
}
}
5.構(gòu)造方法一樣,參數(shù)不同,這就叫重載。同名不同參。
6.比如兩個方法中有大量重疊的參數(shù),可以簡寫。
public class sportCar {
String name;
int speed;
String color;
int seat;
String country;
public sportCar(String name,int speed,String color,String country){
this(name,speed,country);//簡寫,省略大量ctrl c ctrl v。
this.color=color;
}
public sportCar(String name,int speed,String country){
this.name=name;
this.speed=speed;
this.country=country;
}
public static void main(String[] args) {
sportCar car1=new sportCar("阿斯頓馬丁",110,"黑色","英國");
sportCar car2=new sportCar("蘭博基尼",200,"意大利");
}
}
7.EG:注意new對象時候,Hero(),構(gòu)造方法此時不能為空,必須有參,因?yàn)樯厦嬉呀?jīng)定義,此時狀態(tài)不是默認(rèn)狀態(tài)。
public class Hero {
String name;
String skill_w;
String skill_a;
String skill_d;
String skill_s;
//構(gòu)造方法,兩個,同名不同參數(shù)
public Hero(String name){
this.name=name;
}
public Hero(String name,String skill_a,String skill_d,String skill_s,String skill_w){
this(name);
this.skill_a=skill_a;
this.skill_d=skill_d;
this.skill_s=skill_s;
this.skill_w=skill_w;
}
//方法
public void fight(){
System.out.println(this.name+"英雄的移動技巧");
}
public static void main(String[] args) {
Hero h1=new Hero("亞索","左移","右移","后退","前進(jìn)");
h1.fight();
}
}
8.蜘蛛俠和滅霸例子
新建超人類SuperMan.java,對象類為蜘蛛俠
public class SuperMan {
String name;
int blood;
int attack;
//構(gòu)造方法
public SuperMan(String name,int blood, int attack){
this.name=name;
this.blood=blood;
this.attack=attack;
}
public void fight(Monster ms){
System.out.println(this.name+"暴揍"+ms.name);
ms.blood-=this.attack;
System.out.println("滅霸能量"+ms.blood);
}
}
再建怪獸類Monster.java,對象為滅霸
public class Monster {
String name;
int blood;
int attack;
public Monster(String name,int blood, int attack){
this.name=name;
this.blood=blood;
this.attack=attack;
}
public void fighted(SuperMan sp){
System.out.println(this.name+"在揍"+sp.name);
sp.blood-=this.attack;
System.out.println("蜘蛛俠能量"+sp.blood);
}
}
最后新建對戰(zhàn)場所Client.java
public class Client {
public static void main(String[] args) {
SuperMan sp=new SuperMan("蜘蛛俠",1000,20);
Monster ms=new Monster("滅霸",500,30);
sp.fight(ms);
System.out.println("/*************************************分割/");
ms.fighted(sp);
}
}
4.static對象.
1.被static修飾的成員變量叫做靜態(tài)變量,也叫做類變量,說明這個變量是屬于這個類的,而不是屬于是對象。
public class Student {
String name;
String sex;
int age;
public Student(String name,String sex,int age){
this.name=name;
this.sex=sex;
this.age=age;
}
public static void main(String[] args) {
Student s1=new Student("張三","男",25);
Student s2=new Student("李四","女",22);
s1.age=30;
s2.age=20;
System.out.println(s1.age);
System.out.println("/*******************************/");
System.out.println(s2.age);
}
}
2.特點(diǎn)
1.數(shù)據(jù)共享。
2.屬于類,不屬于對象。
3.先于對象產(chǎn)生。
注:在靜態(tài)方法和靜態(tài)塊里面不能使用this。
在靜態(tài)方法里可以調(diào)用靜態(tài)東西,不能調(diào)用動態(tài)。
5.包跟導(dǎo)包
1.在idea中,新建com.xxx.xxx然后新建類。
2.兩個類,在TestPerson.java中創(chuàng)建Person對象,然后alt+enter導(dǎo)入進(jìn)來。
3.包的本質(zhì)就是文件夾,import+包+類。
4.在自己包里面不需要導(dǎo)包。
5.java.lang下所有內(nèi)容都不需要導(dǎo)包
6.訪問權(quán)限
public:所有人都可用。
default:包訪問權(quán)限,只有自己包內(nèi)可用。
private:私有的。
注:變量前面加public表示對外開放,不加則表示默認(rèn)protected,則只對同包開放。
package com.itppf.entity;//包的聲明
public class Person {
public String pub="public";
protected String pro="protected";
private String pri="private";
public static void main(String[] args) {
Person p1=new Person();
System.out.println(p1.pub);
System.out.println(p1.pro);
System.out.println(p1.pri);
}
}
1.同一個類下面都可以訪問。
新建類Person1.java,所以就有。
私有方法private就會報錯。
其余三個都可以打印出來default,public,protected。
新建包c(diǎn)om.itppf.dao。
只有公共方法public可以,其余的都報錯。
7.get/set方法
1.由于某些情況下,我們需要保護(hù)個人信息,需要使用到private訪問權(quán)限,而必要的參數(shù)拿不過去,因此需要將它賦值給set()方法。
具體
this.xxx=xxx;
2.然后使用get方法返回值進(jìn)行拿取。
舉個例子.
新建一個學(xué)生類,Student.java
package com.itppf.entity;
public class Student {
int id;
String name;
String sex;
String address;
//成員方法
public void base(){
System.out.println(this.name+"的基本信息");
}
}
在這個方法中定義了學(xué)生的基本信息。
然后在新建主函數(shù)窗口,StudentTest.java
package com.itppf.entity;
public class StudentTest {
public static void main(String[] args) {
Student s1=new Student();
s1.name="張三";
s1.base();
}
}
然后輸出“張三的基本信息”。
當(dāng)我們使用private修飾時,會不識別id,name等屬性。
package com.itppf.entity;
public class Student {
private int id;
private String name;
private String sex;
private String address;
//成員方法
public void base(){
System.out.println(this.name+"的基本信息");
}
}
因此這個時候我們就需要方法。
private后的name根本拿不到,必須要set傳入?yún)?shù)后,根據(jù)get方法返回值去拿取。
完整的get/set方法學(xué)生例子。
package com.itppf.entity;
public class Student {
private int id;
private String name;
private String sex;
private String address;
//set方法,由于上面定義的方法全是私有的,不能夠在其他類使用,而我們在某些情況下要保護(hù)這些信息,因此使用set/get方法去拿取。
public void setId(int id){
this.id=id; //id是傳遞進(jìn)來的值,而this.id則是成員變量(int id)。
}
public int getId(){
return this.id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setSex(String sex){
this.sex=sex;
}
public String getSex(){
return this.sex;
}
public void setAddress(String address) {
this.address=address;
}
public String getAddress(){
return this.address;
}
}
package com.itppf.entity;
public class StudentTest {
public static void main(String[] args) {
Student s1=new Student();
s1.setId(1);
s1.setName("李四");
s1.setSex("男");
s1.setAddress("中國甘肅");
System.out.println(s1.getId());
System.out.println(s1.getName());
System.out.println(s1.getSex());
System.out.println(s1.getAddress());
System.out.println("該生學(xué)號為:"+s1.getId()+";\n姓名為:"+s1.getName()+";\n性別為:"+s1.getSex()+";\n籍貫為:"+s1.getAddress());
}
}
輸出:
8.繼承
1.子類可以繼承父類中除了private以外的其他內(nèi)容。
2.形式:public class 類 extends 父類。
3.作用:簡化代碼的開發(fā)。
Father.java
package com.itppf.dao;
public class Father {
private void hobby(){
System.out.println("父親喜歡搓麻將");
}
public void temper(){
System.out.println("父親的脾氣好");
}
}
Son.java
package com.itppf.dao;
public class Son extends Father{
}
package com.itppf.dao;
public class ExtendsTest {
public static void main(String[] args) {
Son s1=new Son();
s1.temper();
}
}
但是,private修飾的方法不能夠被調(diào)用。比如上面的hobby()方法。
在實(shí)戰(zhàn)中,比如登錄的實(shí)現(xiàn),有member.java、admin.java、然后再新建user.java。member和admin繼承user共有的方法。
繼承:子類對父類進(jìn)行了擴(kuò)展。
舉個例子
package com.itppf.dao;
public class Father {
public void temper(){
System.out.println("父親的脾氣好");
}
}
在子類繼承父類的基礎(chǔ)上進(jìn)行了擴(kuò)展,hobby()方法。
package com.itppf.dao;
public class Son extends Father{
public void hobby(){
System.out.println("我喜歡打籃球");
}
}
package com.itppf.dao;
public class ExtendsTest {
public static void main(String[] args) {
Son s1=new Son();
s1.temper();
s1.hobby();
}
}
9.super關(guān)鍵字
1.super表示父類中的內(nèi)容。
2.舉個例子
3.新建類Animal作為父類。
public String name="動物";package com.itppf.dao;
public class Animal {
public String name="動物";
}
然后實(shí)例化對象Tiger
package com.itppf.dao;
public class Tiger extends Animal{
public void eat(){
System.out.println(this.name+"吃草");
}
public static void main(String[] args) {
Tiger tiger1=new Tiger();
tiger1.eat();
}
}
打印也沒有問題
但是。。。。
如果在Tiger(子類)中添加變量String name=“奶牛”
package com.itppf.dao;
public class Tiger extends Animal{
public String name="奶牛";
public void eat(){
System.out.println(this.name+"吃草");
}
public static void main(String[] args) {
Tiger tiger1=new Tiger();
tiger1.eat();
}
}
則運(yùn)行
說明這個是有順序的,類似于就近原則,先找自己類,再找父類。
因此我們要用到super關(guān)鍵字,super表示父類。
看上面的這個例子。
package com.itppf.dao;
public class Animal {
public String name="動物";
public Animal(){
System.out.println("父類的構(gòu)造方法");
}
}
package com.itppf.dao;
public class Tiger extends Animal{
public String name="奶牛";
public Tiger(){
System.out.println("子類的構(gòu)造方法");
}
public void eat(){
System.out.println(this.name+"吃草");
System.out.println(super.name+"吃草");
}
public static void main(String[] args) {
Tiger tiger1=new Tiger();
tiger1.eat();
}
}
你會發(fā)現(xiàn),先調(diào)用父類的構(gòu)造方法,然后調(diào)用子類的構(gòu)造方法。
其實(shí)這里默認(rèn)了super(),如果添加上,super(),結(jié)果依然一樣。
package com.itppf.dao;
public class Tiger extends Animal{
public String name="奶牛";
public Tiger(){
super(); //調(diào)用父類的構(gòu)造方法
System.out.println("子類的構(gòu)造方法");
}
public void eat(){
System.out.println(this.name+"吃草");
System.out.println(super.name+"吃草");
}
public static void main(String[] args) {
Tiger tiger1=new Tiger();
tiger1.eat();
}
}
但是注意super(),必須放在第一行,否則會報錯。
參數(shù)的傳遞
1.super可以獲取到父類中的內(nèi)容。
2.如果父類中無參,可以不寫。如果有參數(shù),必須要寫上。
package com.itppf.dao;
public class Animal {
public String name="動物";
public Animal(String name){
System.out.println("父類的構(gòu)造方法");
this.name=name;
}
}
package com.itppf.dao;
public class Tiger extends Animal{
public String name="奶牛";
public Tiger(){
super("老虎"); //name=“老虎”,然后傳到this.name,從而修改了動物。
System.out.println("子類的構(gòu)造方法");
}
public void eat(){
System.out.println(super.name);
System.out.println(this.name);
}
public static void main(String[] args) {
Tiger tiger1=new Tiger();
tiger1.eat();
}
}
super和this區(qū)別父類和子類中重名的內(nèi)容。
10.方法的重寫
1.重寫:子類對父類中的方法進(jìn)行重新的定義。
2.語法:子類和父類中方法的聲明完全一致。
3.重寫又稱為方法的覆蓋。
4.“構(gòu)造方法不能重寫!!!,構(gòu)造方法也不能被繼承!!! 構(gòu)造方法可以重載!! public Demo(){ } public Demo(int a){ } 這個是構(gòu)造方法的重載!!!”
舉個例子
package com.itppf.dao;
public class Phone {
public void name(){
System.out.println("我是所有手機(jī)的總稱!");
}
}
package com.itppf.dao;
public class Vivo extends Phone{
@Override
public void name() {
super.name(); //super可以調(diào)用父類中被重寫了的內(nèi)容。
System.out.println("不!重寫之后,我是vivo");
}
public static void main(String[] args) {
Vivo v1=new Vivo();
v1.name();
}
}
以上就是我跟這IT老男孩學(xué)習(xí)的面向?qū)ο蟮墓P記,大多是我自己感悟,有的也不一定正確,希望大家批評指正。我覺得經(jīng)過學(xué)習(xí)后基本了解了面向?qū)ο螅院笮枰獙W(xué)習(xí)的還有很多,數(shù)據(jù)結(jié)構(gòu)以及數(shù)組,對接口,繼承,封裝,多態(tài)的深入了解,以及java的二十三種設(shè)計模式,加油吧。
筆記和代碼已放到github上面:
筆記:https://github.com/FSDWNS/theory-doc.git
代碼:https://github.com/FSDWNS/Java-web.git