標題:
建構子 (一)
[打印本頁]
作者:
許婷芳
時間:
2019-8-30 11:46
標題:
建構子 (一)
建構子, 又稱建構函式或建構方法, 是一種特殊的函式.
透過建構子, 在自類別生成實體物件的同時, 能對物件進行 "初始化".
[attach]7018[/attach]
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-8-31 16:17
本帖最後由 章幼莛 於 2019-8-31 16:33 編輯
public class Ch31
{
public static void main(String[] args)
{
Car bus=new Car("公車",6);
bus.load=40;
Car truck=new Car("卡車",4,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+"人");
}
}
public class Car
{
String name;
int wheel;
int load;
Car(String a,int b)
{
this.name=a;
this.wheel=b;
}
Car(String a,int b,int c)
{
this.name=a;
this.wheel=b;
this.load=c;
}
}
複製代碼
作者:
洪藜芸
時間:
2019-8-31 16:42
public class As {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
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 n, int w, int l)
{
this.name=n;
this.wheel=w;
this.load=l;
}
Car(String na, int wh)
{
this.name=na;
this.wheel=wh;
}
}
複製代碼
作者:
蔡依宸
時間:
2019-8-31 16:42
public class Mayday {
public static void main(String[] args) {
Car bus= new Car("公車",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+"人.");
}
}
複製代碼
作者:
陳智鈞
時間:
2019-8-31 16:42
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+"人.");
}
}
public class Car {
String name;
int wheel;
int load;
public Car() {
// TODO 自動產生的建構子 Stub
}
Car(String aa,int bb,int cc)
{
this.name=aa;
this.wheel=bb;
this.load=cc;
}
}
複製代碼
作者:
陳柏霖
時間:
2019-8-31 16:42
public class Hello {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
Car bus=new Car("公車",6);
bus.loed=40;
Car truck=new Car("卡車",8);
truck.loed=3;
Car taxl=new Car("計程車",4);
taxl.loed=5;
System.out.println(bus.name+"有"+bus.wheel+"個輪胎可在"+bus.loed+"個人");
System.out.println(truck.name+"有"+truck.wheel+"個輪胎可在"+truck.loed+"個人");
System.out.println(taxl.name+"有"+taxl.wheel+"個輪胎可在"+taxl.loed+"個人");
}
}
複製代碼
作者:
洪子涵
時間:
2019-8-31 16:43
public class aaa {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
Car bus=new Car("公車",6,40);
Car truck=new Car("卡車",8,3);
Car taxi=new Car("計程車",4,5);
System.out.println(bus.name+"有"+bus.wheels+"個輪子,可載"+bus.load+"人");
System.out.println(truck.name+"有"+truck.wheels+"個輪子,可載"+truck.load+"人");
System.out.println(taxi.name+"有"+taxi.wheels+"個輪子,可載"+taxi.load+"人");
}
}
class Car
{
String name;
int wheels,load;
Car(String a,int b,int c)
{
name=a;
wheels=b;
load=c;
}
Car(String n, int w)
{
name=n;
wheels=w;
}
}
複製代碼
作者:
許婷芳
時間:
2019-9-21 16:27
package ch01;
public class Dog
{
static int sum=0;
String name;
int age;
double w;
Dog(String n, int a, double w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
複製代碼
作者:
許婷芳
時間:
2019-9-21 16:27
package ch01;
public class Cat
{
static int sum=0;
String name;
int age;
double w;
Cat(String n, int a, double w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2