標題:
建構子 (一)
[打印本頁]
作者:
tonyh
時間:
2015-3-7 15:08
標題:
建構子 (一)
本帖最後由 tonyh 於 2015-3-7 15:30 編輯
建構子, 又稱建構函式或建構方法, 是一種特殊的函式.
透過建構子, 在自類別生成實體物件的同時, 能對物件進行 "初始化".
[attach]1163[/attach]
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.carry+"人.");
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.carry+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.carry+"人.");
}
}
class Car
{
String name;
int wheel;
int carry;
Car(String name, int wheel, int carry) //定義Car類別的建構子
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
作者:
張瀚仁
時間:
2015-3-7 15:31
public class ch55
{
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.carry+"人.");
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.carry+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.carry+"人.");
}
}
class Car
{
String name;
int wheel;
int carry;
Car(String name, int wheel, int carry)
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
作者:
劉泳鱔
時間:
2015-3-7 15:33
public class ch57
{
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.wheels+"個輪子"+"可在"+bus.carry+"人");
System.out.println(truck.name+"有"+truck.wheels+"個輪子"+"可在"+truck.carry+"人");
System.out.println(taxi.name+"有"+taxi.wheels+"個輪子"+"可在"+taxi.carry+"人");
}
}
class Car
{
String name;
int wheels;
int carry=carry;
car(String name, int wheel, int carry)
{
this name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
作者:
許逸群
時間:
2015-3-7 15:33
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.carry+"人.");
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.carry+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.carry+"人.");
}
}
class Car
{
String name;
int wheel;
int carry;
Car(String name, int wheel, int carry)
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
作者:
林以諾
時間:
2015-3-13 18:22
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.carry+"人.");
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.carry+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.carry+"人.");
}
}
class Car
{
String name;
int wheel;
int carry;
Car(String name, int wheel, int carry)
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
作者:
劉泳鱔
時間:
2015-6-12 18:49
public class Ch57
{
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.carry+"人");
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可在"+truck.carry+"人");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可在"+taxi.carry+"人");
}
}
class Car
{
String name;
int wheel;
int carry;
Car(String name,int wheel,int carry)
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2