標題:
[作業] 建構子 (一)
[打印本頁]
作者:
tonyh
時間:
2020-10-23 20:45
標題:
[作業] 建構子 (一)
本帖最後由 tonyh 於 2020-10-23 20:54 編輯
建構子,又稱建構函式或建構方法,是一種特殊的函式。
透過建構子,在自類別生成實體物件的同時,能對物件進行「初始化」。
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;
}
//方法 (尚未定義)
}
複製代碼
作者:
董宸佑
時間:
2020-10-25 14:53
public class Ch01 {
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;
}
}
複製代碼
作者:
陳宥穎
時間:
2020-10-26 18:23
public class Ch01 {
public static void main(String[] args) {
Car bus=new Car("bus",4);
bus.load=500;
Car taxi=new Car("taxi",4,10);
Car bike=new Car("bike",2,5);
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人.");
System.out.println(bike.name+"有"+bike.wheel+"個輪子,可載"+bike.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 na,int wh)
{
name=na;
wheel=wh;
}
}
複製代碼
作者:
黃宥華
時間:
2020-10-29 22:53
本帖最後由 黃宥華 於 2020-10-29 22:54 編輯
public class Ch99{
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;
}
}
複製代碼
作者:
劉愷鈞
時間:
2020-10-30 18:41
public class Ch01 {
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;
}
}
複製代碼
作者:
夏子涵
時間:
2020-10-30 19:01
public class Ch01{
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;
}
}
複製代碼
作者:
林政瑜
時間:
2020-10-30 19:04
public class Ch55{
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;
}
}
複製代碼
作者:
沈子晏
時間:
2020-10-30 19:08
public class Ch02 {
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;
}
}
複製代碼
作者:
黃柏叡
時間:
2020-10-30 19:11
public class Ch01{
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;
}
}
複製代碼
作者:
李宇澤
時間:
2020-10-30 19:45
public class Ch02{
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;
}
}
複製代碼
作者:
蔡忻霓
時間:
2020-10-30 19:46
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;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2