我看了幕友的代碼,但是我一直看不懂下面的代碼,為什么要寫這樣的構(gòu)造方法
我看了幕友的代碼,但是我一直看不懂下面的代碼,為什么要寫這樣的構(gòu)造方法
public?class?PiCar?extends?Car?{
public?int?deadWeight;
public?int?passengerCapacity;
public?PiCar(String?name,?int?passengerCapacity,?int?rent,int?deadWeight)?{
this.name?=name;
this.rent?=rent;
this.passengerCapacity?=?passengerCapacity;
this.deadWeight?=?deadWeight;
//?TODO?自動生成的構(gòu)造函數(shù)存根
}
2016-01-02
構(gòu)造方法的作用就是在加載的時候,將所需要的參數(shù)傳入。
public?int?deadWeight;
public?int?passengerCapacity;
public?PiCar(String?name,?int?passengerCapacity,?int?rent,int?deadWeight)?{
this.name?=name;
this.rent?=rent;
this.passengerCapacity?=?passengerCapacity;
this.deadWeight?=?deadWeight;
}
deadWeight是成員變量,這里的的this.deadWeight就是指它
(String?name,?int?passengerCapacity,?int?rent,int?deadWeight)是形式參數(shù),簡稱形參
Picar picar = new?PiCar("name", passengerCapacity,rent,deadWeight) 通過new時候在括號里填寫的內(nèi)容是實際參數(shù),簡稱實參。
作用等同于:
picar.setDeadWeight()。picar.setRent()。picar.PassengerCapacity()。picar.setName()。。
分別傳入。。4個參數(shù)。。
因為更方便,所以傳入數(shù)據(jù)一般都會使用帶參數(shù)的構(gòu)造方法。
2015-12-13
利用有參構(gòu)造在new一個對象時,初始化對象的屬性
2015-11-24
習慣性動作
2015-11-24
一般構(gòu)造函數(shù)都是這么寫的啊,在生成對象的時候就傳遞進去屬性,直接將對象的屬性給確定好
如果不懂就吧課程中的構(gòu)造函數(shù)再看下 這里也不是一兩句話就能說清楚的