構造方法是一種特殊的方法,用於構造實例化對象jvm
形式:[修飾符] 類名稱(){ui
}this
構造方法又分爲無參構造和有參構造方法。spa
無參構造就是不向方法裏傳參數例如:public Dog(){ };code
無參構造的做用:給屬性賦值。對象
若沒有定義無參構造則jvm默認分配一個。blog
1 public class Dog{ 2 3 String name; 4 int health; 5 int love; 6 String strain; 7 8 public Dog(){ 9 System.out.println("構造方法"); 10 health = 100; 11 love = 0; 12 } 13 … 14 }
顧名思義有參構造就是向方法裏傳參數例如:內存
public Dog(type arg,type arg1...){ }get
1 public class Dog{ 2 3 String name; 4 int health; 5 int love; 6 String strain; 7 8 public Dog(String _name,int _health,int _love,String _strain){ 9 name = _name; 10 health = _health; 11 love = _love; 12 strain = _strain; 13 }
定義了有參構造方法後,必定要習慣定義無參構造方法。it
有參方法與無參方法是重載關係。
定義了有參構造,jvm不在默認分配一個無參構造。
1 public class Dog{ 2 3 String name; 4 int health; 5 int love; 6 String strain; 7 8 /* 9 public Dog(){ 10 System.out.println("構造方法"); 11 health = 100; 12 love = 0; 13 } 14 */ 15 16 public Dog(String _name,int _health,int _love,String _strain){ 17 name = _name; 18 health = _health; 19 love = _love; 20 strain = _strain; 21 } 22 23 public void showInfo(){ 24 System.out.print("個人名字叫"+name); 25 System.out.print(",健康值"+health); 26 System.out.print(",親密度"+love); 27 System.out.println(",我是一隻"+strain); 28 } 29 } 30 Dog dog = new Dog(); 31 dog.name = name; 32 dog.health = 100; 33 dog.love = 0; 34 dog.strain = strain;
this關鍵字指的是對象的自己,用於訪問本對象屬性,用來區分紅員變量和局部變量。
【1】this調用屬性
1 public Car(String brand,String type,float price){ 2 this.brand = brand; 3 this.type = type; 4 this.price = price; 5 6 }
【2】this調用方法
1 public Dog(String name,int health,int love,String strain){ 2 this.setName(name); 3 this.setHealth(health); 4 this.setLove(love); 5 this.setStrain(strain); 6 7 // showInfo(); 8 this.showInfo(); 9 }
【3】this調用本類的方法
形式:this(arg1,arg2...)
1 public Dog(){ 2 3 } 4 5 public Dog(String name,int health,int love){ 6 this.setName(name); 7 this.setHealth(health); 8 this.setLove(love); 9 } 10 11 12 public Dog(String name,int health,int love,String strain){ 13 //this.setName(name); 14 //this.setHealth(health); 15 //this.setLove(love); 16 17 // this調用本類的其餘構造方法 18 // System.out.println("test"); 19 this(name,health,love); 20 this.setStrain(strain); 21 22 // showInfo(); 23 //this.showInfo(); 24 }
1 public Dog2(String name,int health,int love,String strain){ 2 System.out.println("this:"+this); 3 this.name = name; 4 this.health = health; 5 this.love = love; 6 this.strain = strain; 7 }
1 public class Test04{ 2 public static void main(String[] args){ 3 4 Dog2 dog = new Dog2("二狗",100,0,"土狗"); 5 System.out.println("dog:"+dog); 6 dog.showInfo(); 7 } 8 }
經過打印this中的引用,能夠看出對象dog和this指向同一內存。
通常而言,dog用於類的外部,this用於類的內部。由於類的內部根本不知道dog變量名的存在。
形式:
[修飾符] static 類型 變量名 = [默認值]
靜態變量也稱類變量,在該類下的方法均可以訪問,也就是共享變量。
訪問方式
類名.靜態變量【效率高推薦】
對象.靜態變量
1 public class Car{ 2 String brand; 3 String type; 4 float price; 5 6 static int count = 0; 7 8 public Car(){ 9 Car.count++; 10 } 11 12 public Car(String brand,String type,float price){ 13 this.brand = brand; 14 this.type = type; 15 this.price = price; 16 Car.count++; 17 } 18 19 public void showInfo(){ 20 System.out.println("車輛信息:"); 21 System.out.println("品牌:"+this.brand); 22 System.out.println("型號:"+this.type); 23 System.out.println("價格:"+this.price); 24 System.out.println("我是第"+Car.count+"輛車"); 25 } 26 27 28 }
1 public class Test01{ 2 public static void main(String[] args){ 3 Car car1 = new Car("奔馳","漏油GL300",66); 4 car1.showInfo(); 5 6 7 Car car2 = new Car("奔馳","漏油GL400",66); 8 car2.showInfo(); 9 10 System.out.println(Car.count); 11 System.out.println(car1.count); 12 System.out.println(car2.count); 13 14 } 15 }
在程序運行過程當中值不會發生改變的量叫常量。
1 public class Penguin{ 2 3 private String name; 4 private int health; 5 private int love; 6 private String gender; 7 8 static final String SEX_MALE = "雄"; 9 static final String SEX_FEMALE = "雌"; 10 11 public void setName(String name){ 12 this.name = name; 13 } 14 public String getName(){ 15 return this.name; 16 } 17 18 public void setHealth(int health){ 19 if(health>100 && health<1){ 20 this.health = 60; 21 System.out.println("健康值必須在1-100之間,默認爲60"); 22 }else{ 23 this.health = health; 24 } 25 } 26 public int getHealth(){ 27 return this.health; 28 } 29 30 public void setLove(String love){ 31 this.love = love; 32 } 33 public int getLove(){ 34 return this.love; 35 } 36 37 public void setGender(String gender){ 38 this.gender = gender; 39 } 40 public String getGender(){ 41 return this.gender; 42 } 43 44 public Penguin(){ 45 46 } 47 public Penguin(String name,String gender){ 48 this.setName(name); 49 this.setGender(gender); 50 } 51 52 public Penguin(String name,int health,int love,String gender){ 53 this(name,gender); 54 this.setHealth(health); 55 this.setLove(love); 56 } 57 58 public void showInfo(){ 59 System.out.print("個人名字叫"+name); 60 System.out.print(",健康值"+health); 61 System.out.print(",親密度"+love); 62 System.out.println(",性別"+gender); 63 } 64 65 66 } 67 public class Test02{ 68 public static void main(String[] args){ 69 70 Penguin penguin = new Penguin("大腳",100,0,Penguin.SEX_MALE); 71 } 72 }
形式:
staitc final type 名稱 = [值]
形式:
[修飾符] static 返回值類型 方法名稱 { }
訪問形式
類.靜態方法(效率高推薦)
對象.靜態方法
靜態方法訪問非靜態成員
1 public class Car{ 2 String brand; 3 String type; 4 float price; 5 6 static int count = 0; 7 8 public Car(){ 9 Car.count++; 10 } 11 12 public Car(String brand,String type,float price){ 13 this.brand = brand; 14 this.type = type; 15 this.price = price; 16 Car.count++; 17 } 18 19 public void showInfo(){ 20 System.out.println("車輛信息:"); 21 System.out.println("品牌:"+this.brand); 22 System.out.println("型號:"+this.type); 23 System.out.println("價格:"+this.price); 24 System.out.println("我是第"+Car.count+"輛車"); 25 } 26 27 public static int getCarCount(){ 28 // 在靜態方法中訪問實例變量 29 // System.out.println("品牌:"+brand); 30 31 //showInfo(); 32 //this.showInfo(); 33 34 return Car.count; 35 } 36 }
總結:
靜態方法不能訪問非靜態成員
實例方法能訪問靜態成員
Car car = new car();
1.實例化一個對象的時候jvm先把car.class 加載到方法區,
2.讀取car.class計算聲明成員變量所申請的的字節。
3.讀取car.class中的靜態變量,並給其分配空間並初始化
4.new car申請獲得一個對象,而後給其分配空間;showInfo才能夠經過car對象調用
之因此實例方法能訪問靜態成員,靜態方法不能訪問非靜態成員,就是由於靜態方法、成員先於實例方法、成員加載。
封裝:將類的某些信息隱藏在類內部,不容許外部程序直接訪問,而是經過該類提供的方法來實現對隱藏信息的操做和訪問。
1.私有制變量
2.提供公共設置器(setArg)和訪問器(getArg)
3.在設置器於訪問器中設置業務邏輯校驗。
1 public class Dog{ 2 3 // 【1】private 私有的,對外不可見 4 private String name; 5 private int health; 6 private int love; 7 private String strain; 8 9 // 【2】提供公共的設置器(setter)和訪問器(getter) 10 public void setName(String name){ 11 // 【3】邏輯校驗 12 if(name.equals("")){ 13 System.out.println("姓名不能爲空."); 14 }else{ 15 this.name = name; 16 } 17 } 18 public String getName(){ 19 return this.name; 20 } 21 22 public void setHealth(int health){ 23 if(health < 0){ 24 System.out.println("健康值不合法."); 25 this.health = 0; 26 }else{ 27 this.health = health; 28 } 29 } 30 public int getHealth(){ 31 return this.health; 32 } 33 34 public void setLove(int love){ 35 if(love < 0){ 36 System.out.println("親密度不合法."); 37 this.love = 0; 38 }else{ 39 this.love = love; 40 } 41 } 42 public int getLove(){ 43 return this.love; 44 } 45 46 public void setStrain(String strain){ 47 if(strain.equals("")){ 48 System.out.println("品種不能爲空."); 49 }else{ 50 this.strain = strain; 51 } 52 } 53 public String getStrain(){ 54 return this.strain; 55 } 56 57 58 public Dog(){ 59 60 } 61 62 public Dog(String name,int health,int love,String strain){ 63 this.setName(name); 64 this.setHealth(health); 65 this.setLove(love); 66 this.setStrain(strain); 67 } 68 69 public void showInfo(){ 70 System.out.print("個人名字叫"+this.name); 71 System.out.print(",健康值"+this.health); 72 System.out.print(",親密度"+this.love); 73 System.out.println(",我是一隻"+this.strain); 74 } 75 }