標題:
物件導向基礎概念 (一)
[打印本頁]
作者:
tonyh
時間:
2016-7-1 20:17
標題:
物件導向基礎概念 (一)
本帖最後由 tonyh 於 2017-10-5 15:20 編輯
定義一 Car 類別, 並在Car類別下建立三個實體物件, 分別為bus, truck 與 taxi.
執行參考畫面如下:
public class Ch56{
public static void main(String args[])
{
Car bus=new Car(); //在Car類別下新增一個名為bus的物件, 該物件尚未初始化
bus.name="公車"; //對bus進行初始化
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;
//建構子 (尚未定義)
//方法 (尚未定義)
}
複製代碼
作者:
沈子耕
時間:
2016-7-1 21:10
public class Ch56{
public static void main(String args[]){
Car bus=new Car();
bus.name="巴士";
bus.wheel=6;
bus.load=40;
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人");
Car taxi=new Car();
taxi.name="計程車";
taxi.wheel=4;
taxi.load=5;
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人");
Car truck=new Car();
truck.name="卡車";
truck.wheel=8;
truck.load=3;
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.load+"人");
}
}
class Car{
String name;
int wheel;
int load;
}
複製代碼
作者:
洪振庭
時間:
2016-7-1 21:13
public class Ch56
{
public static void main(String args[])throws BodyException
{
Car bus=new Car();
bus.n="公車";
bus.w=6;
bus.l=40;
Car truck=new Car();
truck.n="卡車";
truck.w=8;
truck.l=2;
Car taxi=new Car();
taxi.n="小黃";
taxi.w=4;
taxi.l=5;
System.out.println(bus.n+"有"+bus.w+"輪子,可載"+bus.l+"個人.");
System.out.println(truck.n+"有"+truck.w+"輪子,可載"+truck.l+"個人.");
System.out.println(taxi.n+"有"+taxi.w+"輪子,可載"+taxi.l+"個人.");
}
}
class Car
{
System n;
int w;
int l;
}
複製代碼
作者:
黃璽安
時間:
2016-7-1 21:14
public class Ch58
{
public static void main(String args[])
{
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;
//建構子 (尚未定義)
//方法 (尚未定義)
}
複製代碼
作者:
李知易
時間:
2016-7-1 21:28
public class Ch57
{
public static void main(String args[])
{
Car car=new car;
Car.name="轎車";
Car.wheel=4;
Car.load=5;
Car bike=new car;
Car.name="腳踏車";
Car.wheel=2;
Car.load=1;
Car scooter=new car;
Car.name="機車";
Car.wheel=4;
Car.load=40;
Car sportcar=new car;
Car.name="跑車";
Car.wheel=4;
Car.load=2;
System.out.print(car.name+"有"+car.wheel+"個輪子 , 可載"+car.load+"個人!");
System.out.print(bike.name+"有"+bike.wheel+"個輪子 , 可載"+bike.load+"個人!");
System.out.print(scooter.name+"有"+scooter.wheel+"個輪子 , 可載"+scooter.load+"個人!");
System.out.print(sportcar.name+"有"+sportcar.wheel+"個輪子 , 可載"+sportcar.load+"個人!");
}
class Car
{
String name;
int wheel;
int load;
}
}
複製代碼
作者:
曾挺桂
時間:
2016-7-1 21:32
import java.util.*;
public class Ch54
{
public class Ch56{
public static void main(String args[])
{
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;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2