標題:
物件導向基礎概念 (一)
[打印本頁]
作者:
tonyh
時間:
2015-3-7 13:51
標題:
物件導向基礎概念 (一)
本帖最後由 tonyh 於 2015-3-7 15:29 編輯
定義一 Car 類別, 並在Car類別下建立三個實體物件, 分別為bus, truck 與 taxi.
執行參考畫面如下:
[attach]1161[/attach]
public class ch57 //主類別
{
public static void main(String args[]) //主方法
{
Car bus=new Car(); //在Car類別下,生成一個名為bus的實體物件
Car truck=new Car();
Car taxi=new Car();
bus.name="公車"; //定義物件的屬性
bus.wheel=6;
bus.carry=40;
truck.name="卡車";
truck.wheel=8;
truck.carry=3;
taxi.name="計程車";
taxi.wheel=4;
taxi.carry=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;
}
複製代碼
作者:
張瀚仁
時間:
2015-3-7 14:38
public class ch57
{
public static void main(String args[])
{
Car bus=new Car();
Car truck=new Car();
Car taxi=new Car();
bus.name="公車";
bus.wheels=6;
bus.carry=40;
truck.name="卡車";
truck.wheels=8;
truck.carry=3;
taxi.name="公車";
taxi.wheels=4;
taxi.carry=5;
System.out.println(bus.name+"有"+bus.wheels+"個輪子"+"可在"+bus.carry+"人");
System.out.println(truck.name+"有"+truck.wheels+"個輪子"+"可在"+truck.carry+"人");
System.out.println(taxi.name+"有"+taxi.wheels+"個輪子"+"可在"+taxi.carry+"人");
}
}
class Car
{
String name;
int wheels;
int carry;
}
複製代碼
作者:
許逸群
時間:
2015-3-7 14:39
public class ch57
{
public static void main(String args[])
{
Car bus=new Car();
Car truck=new Car();
Car taxi=new Car();
bus.name="公車";
bus.wheels=6;
bus.carry=40;
truck.name="卡車";
truck.wheels=8;
truck.carry=3;
taxi.name="公車";
taxi.wheels=4;
taxi.carry=5;
System.out.println(bus.name+"有"+bus.wheels+"個輪子"+"可在"+bus.carry+"人");
System.out.println(truck.name+"有"+truck.wheels+"個輪子"+"可在"+truck.carry+"人");
System.out.println(taxi.name+"有"+taxi.wheels+"個輪子"+"可在"+taxi.carry+"人");
}
}
class Car
{
String name;
int wheels;
int carry;
}
複製代碼
作者:
劉泳鱔
時間:
2015-3-7 14:39
public class ch57
{
public static void main(String args[])
{
Car bus=new Car();
Car truck=new Car();
Car taxi=new Car();
bus.name="公車";
bus.wheels=6;
bus.carry=40;
truck.name="卡車";
truck.wheels=8;
truck.carry=3;
taxi.name="公車";
taxi.wheels=4;
taxi.carry=5;
System.out.println(bus.name+"有"+bus.wheels+"個輪子"+"可在"+bus.carry+"人");
System.out.println(truck.name+"有"+truck.wheels+"個輪子"+"可在"+truck.carry+"人");
System.out.println(taxi.name+"有"+taxi.wheels+"個輪子"+"可在"+taxi.carry+"人");
}
}
class Car
{
String name;
int wheels;
int carry;
}
複製代碼
作者:
林以諾
時間:
2015-3-13 17:39
public class ch57
{
public static void main(String args[])
{
Car bus=new Car();
Car truck=new Car();
Car taxi=new Car();
bus.name="公車";
bus.wheel=6;
bus.carry=40;
truck.name="卡車";
truck.wheel=8;
truck.carry=3;
taxi.name="計程車";
taxi.wheel=4;
taxi.carry=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;
}
複製代碼
作者:
劉泳鱔
時間:
2015-6-12 18:31
public class Ch57
{
public static void main(String args[])
{
Car bus=new Car();
Car truck=new Car();
Car taxi=new Car();
bus.name="公車";
bus.wheel=6;
bus.carry=40;
truck.name="卡車";
truck.carry=3;
taxi.name="計程車";
truck.wheel=8;
taxi.wheel=4;
taxi.carry=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;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2