標題:
建構子 (二)
[打印本頁]
作者:
tonyh
時間:
2015-3-20 17:43
標題:
建構子 (二)
本帖最後由 tonyh 於 2015-3-20 18:30 編輯
定義帶不同參數數量的建構子
[attach]1174[/attach]
public class ch59
{
public static void main(String args[])
{
Car bus=new Car("公車",6,40);
Car truck=new Car("卡車",8,3);
Car taxi=new Car("計程車");
taxi.wheel=4;
taxi.carry=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, carry;
Car() //不帶任何參數的建構子
{
}
Car(String name) //帶有一個參數的建構子
{
this.name=name;
}
Car(int wheel, int carry) //帶有二個參數的建構子
{
this.wheel=wheel;
this.carry=carry;
}
Car(String name, int wheel, int carry) //帶有三個參數的建構子
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
作者:
林以諾
時間:
2015-3-20 18:39
public class ch59
{
public static void main(String args[])
{
Car bus=new Car("公車",6,40);
Car truck=new Car("卡車",8,3);
Car taxi=new Car("計程車");
taxi.wheel=4;
taxi.carry=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, carry;
Car()
{
}
Car(String name)
{
this.name=name;
}
Car(int wheel, int carry)
{
this.wheel=wheel;
this.carry=carry;
}
Car(String name, int wheel, int carry)
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
作者:
張瀚仁
時間:
2015-3-20 18:39
public class ch59
{
public static void main(String args[])
{
Car bus=new Car("公車",3,50);
Car truck=new Car("卡車",84,33);
Car taxi=new Car("計程車");
taxi.wheel=42;
taxi.carry=55;
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, carry;
Car()
{
}
Car(String name)
{
this.name=name;
}
Car(int wheel, int carry)
{
this.wheel=wheel;
this.carry=carry;
}
Car(String name, int wheel, int carry)
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
作者:
許逸群
時間:
2015-3-20 18:44
public class ch59
{
public static void main(String args[])
{
Car bus=new Car("公車",3,50);
Car truck=new Car("卡車",84,33);
Car taxi=new Car("計程車");
taxi.wheel=42;
taxi.carry=55;
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, carry;
Car()
{
}
Car(String name)
{
this.name=name;
}
Car(int wheel, int carry)
{
this.wheel=wheel;
this.carry=carry;
}
Car(String name, int wheel, int carry)
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2