標題:
物件導向基礎概念 (一)
[打印本頁]
作者:
tonyh
時間:
2019-1-28 19:19
標題:
物件導向基礎概念 (一)
定義一 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;
//建構子 (尚未定義)
//方法 (尚未定義)
}
複製代碼
作者:
趙一鳴
時間:
2019-1-28 20:00
本帖最後由 趙一鳴 於 2019-1-28 20:05 編輯
import java.util.Scanner;
public class Ch50
{
public static void main(String[] args)
{
Car bus=new Car();
bus.name="公車";
bus.wheel=8;
bus.load=30;
Car truck=new Car();
truck.name="卡車";
truck.wheel=6;
truck.load=6;
Car taxi=new Car();
taxi.name="計程車";
taxi.wheel=4;
taxi.load=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+"個人");
}
}
複製代碼
作者:
曾堂桂
時間:
2019-1-28 20:01
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;
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+"人.");
}
}
複製代碼
作者:
洪翊庭
時間:
2019-1-28 20:04
public class Ch57 {
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=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+"人.");
}
}
複製代碼
作者:
洪翊展
時間:
2019-1-28 20:06
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;
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-1-28 20:17
本帖最後由 戴嘉禾 於 2019-1-28 21:01 編輯
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;
String name;
}
複製代碼
作者:
may
時間:
2019-1-28 20:38
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=4;
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人;"+ truck.name+"有"+truck.wheel+"個輪子,可載"+truck.load+"人;" +taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人。");
}
}
複製代碼
作者:
黃宇瑄
時間:
2019-2-1 20:30
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;
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-2-1 20:30
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;
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