標題:
建構子 (一)
[打印本頁]
作者:
tonyh
時間:
2015-1-24 16:17
標題:
建構子 (一)
本帖最後由 tonyh 於 2015-1-24 16:49 編輯
建構子, 又稱建構函式或建構方法, 是一種特殊的函式.
透過建構子, 在自類別生成實體物件的同時, 能對物件進行 "初始化".
[attach]1134[/attach]
public class ch57 //主類別
{
public static void main(String args[]) //主方法
{
Car bus=new Car("公車",6,40); //在Car類別下,生成一個名為bus的實體物件
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 //定義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-1-24 16:48
public class ch57
{
public static void main(String args[])
{
Car x[]={new Car("公車",6,40),new Car("卡車",8,3),new Car("計程車",4,5)};
for(int i=0;i<3;i++)
System.out.println(x[i].name+"有"+x[i].wheel+"個輪子,可載"+x[i].carry+"人.");
}
}
class Car
{
String name;
int wheel;
int carry;
Car(String n,int w,int c)
{
name=n;
wheel=w;
carry=c;
}
}
複製代碼
作者:
李允軒
時間:
2015-1-24 16:48
public class ch52 //主類別
{
public static void main(String args[]) //主方法
{
Car bus=new Car("公車",6,40); //在Car類別下,生成一個名為bus的實體物件
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 //定義Car類別
{
String name;
int wheel;
int carry;
Car(String name,int wheel,int carry)
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
作者:
張峻瑋
時間:
2015-1-24 16:48
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;
}
}
複製代碼
作者:
周雍程
時間:
2015-1-24 16:50
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;
}
}
複製代碼
作者:
張郁庭
時間:
2015-1-24 16:50
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;
}
}
複製代碼
作者:
林宇翔
時間:
2015-1-24 16:50
public class ch55
{
public static void main(String args[])
{
Car bus=new Car("公車",6,40);
Car bus=new Car("卡車",8,3);
Car bus=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-1-24 16:51
import java.util.*;
public class ch52
{
public static void main(String args[])
{ Car bus=new Car("公車",6,40);
Car truck=new Car("卡車",6,40);
Car taxi=new Car("計程車",6,40);
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.carry+"人");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.carry+"人");
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.carry+"人");
}
}
class Car
{
String name;
int wheel;
int carry;
Car(String,int wheel,int carry)
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
作者:
張彥承
時間:
2015-1-24 16:51
import java.util.*;
public class ch57
{
public static void main(String args[])
{ Car bus=new Car("公車",6,40);
Car truck=new Car("卡車",6,40);
Car taxi=new Car("計程車",6,40);
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.carry+"人");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.carry+"人");
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.carry+"人");
}
}
class Car
{
String name;
int wheel;
int carry;
Car(String,int wheel,int carry)
{
this.name=name;
this.wheel=wheel;
this.carry=carry;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2