標題:
物件導向基礎概念 (一)
[打印本頁]
作者:
許婷芳
時間:
2019-8-30 11:45
標題:
物件導向基礎概念 (一)
本帖最後由 許婷芳 於 2019-8-31 15:26 編輯
定義一 Car 類別, 並在Car類別下建立三個實體物件, 分別為bus, truck 與 taxi.
執行參考畫面如下:
[attach]7017[/attach]
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;
//建構子 (尚未定義)
//方法 (尚未定義)
}
複製代碼
作者:
蔡依宸
時間:
2019-8-31 15:58
public class Mayday {
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;
}
複製代碼
作者:
洪藜芸
時間:
2019-8-31 16:00
public class As {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
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;
}
複製代碼
作者:
陳柏霖
時間:
2019-8-31 16:01
public class Hello {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
Car bus=new Car();
bus.name="公車";
bus.wheel=6;
bus.loed=40;
Car truck=new Car();
truck.name="卡車";
truck.wheel=8;
truck.loed=3;
Car taxl=new Car();
taxl.name="計程車";
taxl.wheel=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:02
本帖最後由 章幼莛 於 2019-8-31 16:18 編輯
public class Ch31
{
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=4;
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+"人");
}
}
public class Car
{
String name;
int wheel;
int load;
}
複製代碼
作者:
洪子涵
時間:
2019-8-31 16:06
public class aaa {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
Car bus=new Car();
bus.wheels=6;
bus.load=40;
bus.name="公車";
Car truck=new Car();
truck.wheels=8;
truck.load=3;
truck.name="卡車";
Car taxi=new Car();
taxi.wheels=4;
taxi.load=5;
taxi.name="計程車";
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;
}
複製代碼
作者:
蔡季庭
時間:
2019-8-31 16:38
public class C66 {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
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;
}
複製代碼
作者:
戴安利
時間:
2019-9-2 21:07
public class Haha
{
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;
}
複製代碼
作者:
戴偉宸
時間:
2019-9-2 21:08
public class work1{
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://istak.org.tw/seed/)
Powered by Discuz! 7.2