標題:
物件導向基礎概念 (一)
[打印本頁]
作者:
tonyh
時間:
2017-4-15 10:22
標題:
物件導向基礎概念 (一)
定義一 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;
//建構子 (尚未定義)
//方法 (尚未定義)
}
複製代碼
作者:
黃茂勛
時間:
2017-4-15 11:09
public class Ch57
{
public static void main(String args[])throws MyException
{
Car bus=new Car();
bus.name="公車";
bus.wheel=4;
bus.load=20;
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;
}
複製代碼
作者:
陸長辰
時間:
2017-4-15 11:12
import java.util.Scanner;
public class Ch50
{
public static void main(String args[])throws MyException
{
Car bus=new Car();
bus.name="公車";
bus.wheel=4;
bus.load=20;
Car truck=new Car();
truck.name="卡車";
truck.wheel=40;
truck.load=200;
Car taxi=new Car();
taxi.name="計程車";
taxi.wheel=87;
taxi.load=20000;
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;
}
複製代碼
作者:
陳泓瑜
時間:
2017-4-15 11:14
public class Ch45
{
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;
}
複製代碼
作者:
林侑成
時間:
2017-4-15 11:15
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