標題:
物件導向基礎概念 (一)
[打印本頁]
作者:
tonyh
時間:
2021-11-27 20:51
標題:
物件導向基礎概念 (一)
定義一 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;
//建構子 (尚未定義)
//方法 (尚未定義)
}
複製代碼
作者:
陳志祐
時間:
2021-12-4 19:39
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 truck=new Car();
truck.name="卡車";
truck.wheel=8;
truck.load=3;
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.load+"人");
Car taxi=new Car();
taxi.name="計程車";
taxi.wheel=4;
taxi.load=5;
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人");
}
}
class Car{
String name;
int wheel,load;
}
複製代碼
作者:
黃子倢
時間:
2021-12-4 19:40
public class Ch01 {
public static void main(String[] args) {
Car bus=new Car();
bus.name="公車";
bus.wheel=8;
bus.load=40;
Car truck=new Car();
truck.name="卡車";
truck.wheel=10;
truck.load=2;
Car taxi=new Car();
taxi.name="計程車";
taxi.wheel=4;
taxi.load=6;
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, load;
}
複製代碼
作者:
余柏緯
時間:
2021-12-4 19:40
public class Ch03 {
public static void main(String[] args) {
Car bus=new Car();
bus.name="公車";
bus.wheel=6;
bus.load=40;
Car turck=new Car();
turck.name="卡車";
turck.wheel=8;
turck.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(turck.name+"有"+turck.wheel+"個輪子,可載"+turck.load+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人.");
}
}
class Car
{
String name;
int wheel,load;
}
複製代碼
作者:
朱奕祐
時間:
2021-12-4 19:41
public class Ch03 {
public static void main(String[] args) {
Car bus=new Car();
bus.name="公車";
bus.wheel=6;
bus.load=40;
Car turck=new Car();
turck.name="卡車";
turck.wheel=8;
turck.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(turck.name+"有"+turck.wheel+"個輪子,可載"+turck.load+"人.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人.");
}
}
class Car
{
String name;
int wheel,load;
}
複製代碼
作者:
林土水
時間:
2021-12-4 19:43
public class Ch01{
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;
truk.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+"個輪子,可載"+truk.load+"人.");
}
}
class Car //自訂類別
{
String name; //宣告該類別擁有哪些屬性
int wheel;
int load;
//建構子 (尚未定義)
//方法 (尚未定義)
}
複製代碼
作者:
林鼎傑
時間:
2021-12-4 19:43
public class Ch01 {
public static void main(String[] args)
{
Car bus=new Car();
bus.name="公車";
bus.load=40;
bus.wheel=6;
Car truck=new Car();
truck.name="卡車";
truck.load=3;
truck.wheel=8;
Car taxi=new Car();
taxi.name="計程車";
taxi.load=5;
taxi.wheel=4;
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,load;
}
複製代碼
作者:
蔡寓珉
時間:
2021-12-4 19:46
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+"人.");
}
}
複製代碼
作者:
許洧熏
時間:
2021-12-4 19:50
public class Ch01{
public static void main(String args[])
{
Car bus=new Car();
bus.name="高鐵";
bus.wheel=8;
bus.load=58;
Car truck=new Car();
truck.name="飛機";
truck.wheel=100;
truck.load=54;
Car taxi=new Car();
taxi.name="捷運";
taxi.wheel=45;
taxi.load=25;
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;
}
複製代碼
作者:
王秉鈞
時間:
2021-12-4 19:50
public class Ch24 {
public static void main(String[] args)
{
Car train=new Car();
train.name="火車";
train.wheel=100;
train.load=250;
Car scooter=new Car();
scooter.name="摩托車";
scooter.wheel=2;
scooter.load=2;
Car car=new Car();
car.name="汽車";
car.wheel=4;
car.load=5;
System.out.println(train.name+"有"+train.wheel+"個輪子,可在"+train.load+"人");
System.out.println(scooter.name+"有"+scooter.wheel+"個輪子,可在"+scooter.load+"人");
System.out.println(car.name+"有"+car.wheel+"個輪子,可在"+car.load+"人");
}
}
class Car
{
String name;
int wheel;
int load;
}
}
複製代碼
作者:
俞成章
時間:
2021-12-4 19:52
[code][code][code]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
int wheel;
int load;
}
複製代碼
[/code][/code][/code]
作者:
呂尚霖
時間:
2021-12-4 20:32
public class Ch02 {
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