標題:
物件導向基礎概念 (一)
[打印本頁]
作者:
tonyh
時間:
2020-10-23 19:55
標題:
物件導向基礎概念 (一)
定義一 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;
//建構子 (尚未定義)
//方法 (尚未定義)
}
複製代碼
作者:
劉愷鈞
時間:
2020-10-23 20:53
import java.util.InputMismatchException;
import java.util.Scanner;
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;
}
複製代碼
作者:
夏子涵
時間:
2020-10-23 20:53
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;
}
複製代碼
作者:
孫嘉駿
時間:
2020-10-23 20:54
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;
}
複製代碼
作者:
董宸佑
時間:
2020-10-23 20:54
public class Ch01
{
public static void main(String[] args) throws BodyException
{
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;
}
複製代碼
作者:
沈子晏
時間:
2020-10-23 21:04
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;
}
複製代碼
作者:
陳宥穎
時間:
2020-10-23 21:04
public class Ch1000 {
public static void main(String[] args) {
Car taxi=new Car();
taxi.name="計程車";
taxi.load=3;
taxi.wheel=4;
Car bus=new Car();
bus.name="公車";
bus.load=100;
bus.wheel=4;
Car bike=new Car();
bike.name="自行車";
bike.load=2;
bike.wheel=4;
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人");
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.load+"人");
System.out.println(bike.name+"有"+bike.wheel+"個輪子,可載"+bike.load+"人");
}
}
class Car
{
String name;
int wheel;
int load;
}
複製代碼
作者:
黃宥華
時間:
2020-10-23 21:04
import java.util.Scanner;
public class Ch09
{
public static void main(String[] args){
Car bus=new Car();
bus.name="公車";
bus.w=6;
bus.l=40;
Car truck=new Car();
truck.name="卡車";
truck.w=8;
truck.l=3;
Car taxi=new Car();
taxi.name="計程小車車";
taxi.w=4;
taxi.l=5;
System.out.println(bus.name+"有"+bus.w+"個輪子,可以載"+bus.l+"個人");
System.out.println(truck.name+"有"+truck.w+"個輪子,可以載"+truck.l+"個人");
System.out.println(taxi.name+"有"+taxi.w+"個輪子,可以載"+taxi.l+"個人");
}
}
class Car{
String name;
int w;
int l;
}
複製代碼
作者:
黃柏叡
時間:
2020-10-23 21:04
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;
}
複製代碼
作者:
李宇澤
時間:
2020-10-30 19:46
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;
}
複製代碼
作者:
蔡忻霓
時間:
2020-10-30 20:14
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