標題:
[作業] 建構子 (一)
[打印本頁]
作者:
tonyh
時間:
2021-12-4 20:32
標題:
[作業] 建構子 (一)
建構子,又稱建構函式或建構方法,是一種特殊的函式。
透過建構子,在自類別生成實體物件的同時,能對物件進行「初始化」。
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;
}
//方法 (尚未定義)
}
複製代碼
作者:
林鼎傑
時間:
2021-12-4 20:46
public class Ch01 {
public static void main(String[] args)
{
Car bus=new Car("公車",40,6);
Car truck=new Car("卡車",3,8);
Car taxi=new Car("計程車",5,4);
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,load;
Car(String n, int a, int b)
{
name=n;
load=a;
wheel=b;
}
}
複製代碼
作者:
黃子倢
時間:
2021-12-4 20:47
public class Ch01 {
public static void main(String[] args) {
Car bus=new Car("公車", 8, 40);
Car truck=new Car("卡車", 10, 2);
Car taxi=new Car("計程車", 4, 6);
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, load;
Car(String n, int w, int l)
{
name=n;
wheel=w;
load=l;
}
}
複製代碼
作者:
許洧熏
時間:
2021-12-4 20:48
public class Ch01{
public static void main(String args[])
{
Car bus=new Car("高鐵",8);
bus.load=45;
Car truck=new Car("飛機",10,17);
Car taxi=new Car("捷運",18,9);
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;
}
}
複製代碼
作者:
余柏緯
時間:
2021-12-4 20:49
public class Ch03 {
public static void main(String[] args) {
Car bus=new Car("公車",6);
bus.load=40;
Car turck=new Car("卡車",8,3);
Car taxi=new Car("計程車",4,5);
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人.");
System.out.println(turck.name+"有"+turck.wheel+"個輪子,可載"+turck.load+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人.");
}
}
class Car
{
String name;
int wheel,load;
Car(String n,int w,int l)
{
name=n;
wheel=w;
load=l;
}
Car(String n,int w)
{
name=n;
wheel=w;
}
}
複製代碼
作者:
林土水
時間:
2021-12-4 20:49
public class Ch01 {
public static void main(String[] args)
{
Car bus=new Car("公車",40,6);
Car truck=new Car("卡車",3,8);
Car taxi=new Car("計程車",5,4);
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,load;
Car(String n, int a, int b)
{
name=n;
load=a;
wheel=b;
}
}
複製代碼
作者:
蔡寓珉
時間:
2021-12-4 20:49
public class Ch01{
public static void main(String args[])
{
Car bus=new Car("高鐵",8);
bus.load=45;
Car truck=new Car("飛機",10,17);
Car taxi=new Car("捷運",18,9);
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;
}
}
複製代碼
作者:
陳志祐
時間:
2021-12-4 20:50
public class Ch56 {
public static void main(String[] args)
{
Car bus=new Car("公車",6,40);
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人");
Car truck=new Car("卡車",8,3);
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.load+"人");
Car taxi=new Car("計程車",4,5);
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人");
}
}
class Car{
String name;
int wheel,load;
Car(String n,int w,int l)
{
name=n;
wheel=w;
load=l;
}
}
複製代碼
作者:
朱奕祐
時間:
2021-12-4 20:50
public class Ch03 {
public static void main(String[] args) {
Car bus=new Car("公車",6);
bus.load=40;
Car turck=new Car("卡車",8,3);
Car taxi=new Car("計程車",4,5);
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人.");
System.out.println(turck.name+"有"+turck.wheel+"個輪子,可載"+turck.load+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人.");
}
}
class Car
{
String name;
int wheel,load;
Car(String n,int w,int l)
{
name=n;
wheel=w;
load=l;
}
Car(String n,int w)
{
name=n;
wheel=w;
}
}
複製代碼
作者:
呂尚霖
時間:
2021-12-11 18:24
本帖最後由 呂尚霖 於 2021-12-11 19:28 編輯
[code]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;
}
}
複製代碼
作者:
林佑宸
時間:
2021-12-11 19:27
public class Ch01 {
public static void main(String[] args)
{
Car bus=new Car("公車",40,6);
Car truck=new Car("卡車",3,8);
Car taxi=new Car("計程車",5,4);
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,load;
Car(String n, int a, int b)
{
name=n;
load=a;
wheel=b;
}
}
複製代碼
作者:
林土水
時間:
2021-12-11 19:46
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() //沒帶任何參數的建構子
{
}
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;
}
//方法 (尚未定義)
}
}
}
複製代碼
作者:
俞成章
時間:
2021-12-11 19:47
package ch51;
public class CH51{
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;
}
}
複製代碼
作者:
王秉鈞
時間:
2021-12-11 19:57
public class Ch25
public static void main(String[] args) {
{
Car bus=new Car("公車",6);
bus.load=40;
Car truck=new Car("卡車",8,3);
Car car=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(car.name+"有"+car.wheel+"個輪子可載"+car.load+"人");
}
}
calss 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