標題:
建構子 (一)
[打印本頁]
作者:
tonyh
時間:
2019-1-28 19:20
標題:
建構子 (一)
建構子, 又稱建構函式或建構方法, 是一種特殊的函式.
透過建構子, 在自類別生成實體物件的同時, 能對物件進行 "初始化".
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;
}
//方法 (尚未定義)
}
複製代碼
作者:
李沛昂
時間:
2019-1-28 20:01
public class Pig {
public static void main(String[] Pigs) {
Car bus=new Car();
bus.name="公車";
bus.wheel=6;
bus.load=40;
Car truck=new Car();
truck.name="卡車";
truck.wheel=8;
truck.load=3;
Car taxi=new Car();
taxi.name="計程車";
taxi.wheel=4;
taxi.load=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;
}
複製代碼
作者:
趙一鳴
時間:
2019-1-28 20:32
import java.util.Scanner;
public class Ch50
{
public static void main(String[] args)
{
Car bus=new Car("公車",8,30);
Car truck=new Car("卡車",6,6);
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+"個人");
}
}
複製代碼
作者:
李沛昂
時間:
2019-1-28 20:33
public class Pig {
public static void main(String[] Pigs) {
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;
}
}
複製代碼
作者:
戴嘉禾
時間:
2019-1-28 20:36
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.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;
}
}
複製代碼
作者:
曾堂桂
時間:
2019-1-28 20:36
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 n, int w, int l)
{
name=n;
wheel=w;
load=l;
}
}
複製代碼
作者:
洪翊庭
時間:
2019-1-28 20:37
public class Ch58{
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+"人.");
}
}
複製代碼
作者:
may
時間:
2019-1-28 20:59
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.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;
}
}
複製代碼
作者:
黃宇綸
時間:
2019-2-1 20:31
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;
}
}
複製代碼
作者:
黃宇瑄
時間:
2019-2-1 20:34
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;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2