標題:
建構子 (一)
[打印本頁]
作者:
tonyh
時間:
2016-7-1 21:14
標題:
建構子 (一)
本帖最後由 tonyh 於 2017-10-5 15:16 編輯
建構子, 又稱建構函式或建構方法, 是一種特殊的函式.
透過建構子, 在自類別生成實體物件的同時, 能對物件進行 "初始化".
public class Ch57{
public static void main(String args[])
{
Car bus=new Car("公車",6);
bus.load=40;
Car truck=new Car("卡車",8,3);
Car taxi=new Car("計程車",4,5);
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人.");
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.load+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人.");
}
}
class Car //自訂類別
{
String name; //宣告該類別擁有哪些屬性
int wheel;
int load;
Car() //沒帶任何參數的建構子
{
}
Car(String name, int wheel, int load) //帶三個參數的建構子
{
this.name=name;
this.wheel=wheel;
this.load=load;
}
Car(String n, int w) //帶兩個參數的建構子
{
name=n;
wheel=w;
}
//方法 (尚未定義)
}
複製代碼
作者:
李知易
時間:
2016-7-1 21:30
public class Ch58
{
public static void main(String args[])
{
Car bus=new Car("公車",6,40);
Car truck=new Car("卡車",8,3);
Car taxi=new Car("計程車",4,5);
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人.");
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.load+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人.");
}
}
class Car
{
String name;
int wheel;
int load;
Car(String name, int wheel, int load)
{
this.name=name;
this.wheel=wheel;
this.load=load;
}
複製代碼
作者:
沈子耕
時間:
2016-7-1 21:32
public class Ch57{
public static void main(String args[]){
Car bus=new Car("巴士", 6, 40);
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人");
Car taxi=new Car("計程車", 4, 5);
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人");
Car truck=new Car("卡車",8 ,3);
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.load+"人");
}
}
class Car{
String name;
int wheel;
int load;
Car(String name, int wheel, int load){
this.name=name;
this.wheel=wheel;
this.load=load;
}
}
複製代碼
作者:
黃璽安
時間:
2016-7-8 18:52
public class Ch57{
public static void main(String args[])
{
Car bus=new Car("公車",6);
bus.load=40;
Car truck=new Car("卡車",8,3);
Car taxi=new Car("計程車",4,5);
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人.");
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.load+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人.");
}
}
class Car
{
String name;
int wheel;
int load;
Car()
{
}
Car(String name, int wheel, int load)
{
this.name=name;
this.wheel=wheel;
this.load=load;
}
Car(String n, int w)
{
name=n;
wheel=w;
}
//方法 (尚未定義)
}
複製代碼
作者:
曾挺桂
時間:
2016-7-8 19:56
public class Ch57{
public static void main(String args[])
{
Car bus=new Car("公車",6);
bus.load=40;
Car truck=new Car("卡車",8,3);
Car taxi=new Car("計程車",4,5);
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人.");
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.load+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人.");
}
}
class Car
{
String name;
int wheel;
int load;
Car()
{
}
Car(String name, int wheel, int load)
{
this.name=name;
this.wheel=wheel;
this.load=load;
}
Car(String n, int w)
{
name=n;
wheel=w;
}
}
複製代碼
作者:
梁和雋
時間:
2016-7-8 19:57
本帖最後由 梁和雋 於 2016-7-8 20:06 編輯
public class Ch10101101010110101000101010110111111010111101010
{
public static void main(String args[])
{
Car bus=new Car("公車",6,40);
Car truck=new Car("卡車",8,3);
Car taxi=new Car("計程車",4,5);
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人");
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.load+"人");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人");
}
}
class Car
{
String name;
int wheel;
int load;
Car(String name, int wheel, int load)
{
this.name=name;
this.wheel=wheel;
this.load=load;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2