標題:
建構子 (一)
[打印本頁]
作者:
tonyh
時間:
2018-1-20 13:51
標題:
建構子 (一)
本帖最後由 tonyh 於 2019-1-28 19:20 編輯
建構子, 又稱建構函式或建構方法, 是一種特殊的函式.
透過建構子, 在自類別生成實體物件的同時, 能對物件進行 "初始化".
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;
}
//方法 (尚未定義)
}
複製代碼
作者:
蕭澧邦
時間:
2018-1-20 14: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;
}
//方法 (尚未定義)
}
複製代碼
作者:
洪榜蔓
時間:
2018-1-20 14:19
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;
}
複製代碼
作者:
高睿辰
時間:
2018-1-20 14:23
本帖最後由 高睿辰 於 2018-1-20 14:53 編輯
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;
}
}
複製代碼
作者:
蔡幸融
時間:
2018-1-20 14:45
public class Ch50
{
public static void main(String[] args)
{
Car bus=new Car("公車",6,40);
Car truch=new Car("卡車",8,3);
Car taxi=new Car("計程車",4,5);
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人.");
System.out.println(truch.name+"有"+truch.wheel+"個輪子,可載"+truch.load+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人.");
}
}
class Car
{
String name;
int wheel;
int load;
Car(String n, int w, int l)
{
name=n;
wheel=w;
load=l;
}
}
複製代碼
作者:
譚暐霖
時間:
2018-1-20 14:47
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;
}
}
複製代碼
作者:
莊旻叡
時間:
2018-1-20 14:51
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;
}
}
複製代碼
作者:
黃宥鈞
時間:
2018-1-20 17:34
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(String name, int wheel, int load)
{
this.name=name;
this.wheel=wheel;
this.load=load;
}
Car(String n, int w)
{
name=n;
wheel=w;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2