標題:
物件導向基礎概念 (一)
[打印本頁]
作者:
陳弘修
時間:
2021-1-23 09:23
標題:
物件導向基礎概念 (一)
定義一 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-1-23 11:19
package a01;
public class a01{
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;
}
複製代碼
作者:
林羿丞
時間:
2021-1-23 11:20
package ddd;
public class ddd{
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;
}
複製代碼
作者:
朱閎聿
時間:
2021-1-23 11:24
package a01;
public class a01
{
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;
}
複製代碼
作者:
王銘鴻
時間:
2021-1-23 11:24
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=2;
truck.load=10;
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+"1個興冠感染者和4位成員.");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.load+"人.");
}
}
class Car
{
String name;
int wheel;
int load;
}
複製代碼
作者:
黃柏智
時間:
2021-1-23 11:29
package A01;
import java.util.Scanner;
public class A02
{
static Scanner s=new Scanner(System.in);
//方法中發生例外但沒處理,以throws語法拋給呼叫者處理
public static void main(String[] args) throws MyException
{
pokemon fire=new pokemon();
fire.name="小火龍";
fire.AD=15;
fire.AP=5;
pokemon water=new pokemon();
water.name="傑尼龜";
water.AD=10;
water.AP=10;
pokemon grass=new pokemon();
grass.name="妙蛙種子";
grass.AD=5;
grass.AP=15;
System.out.println(fire.name+"有"+fire.AD+"AD,"+"有"+fire.AP+"AP,");
System.out.println(water.name+"有"+fire.AD+"AD,"+"有"+water.AP+"AP,");
System.out.println(grass.name+"有"+grass.AD+"AD,"+"有"+grass.AP+"AP,");
}
}
class pokemon
{
String name;
int AD;
int AP;
}
複製代碼
回復
1#
陳弘修
作者:
陳莉榛
時間:
2021-1-23 11:33
package a01;
import java.util.*;
public class a01 {
static Scanner s =new Scanner(System.in);
public static void main(String[] args){
LOL hero1=new LOL();
hero1.name="提摩";
hero1.asi=4;
hero1.R=1;
LOL hero2=new LOL();
hero2.name="艾希";
hero2.asi=2;
hero2.R=75;
LOL hero3=new LOL();
hero3.name="吶兒";
hero3.asi=4;
hero3.R=60;
System.out.println(hero1.name+"有"+hero1.asi+"隻腳,大招冷卻"+hero1.R+"秒.");
System.out.println(hero2.name+"有"+hero2.asi+"隻腳,大招冷卻"+hero2.R+"秒.");
System.out.println(hero3.name+"有"+hero3.asi+"隻腳,大招冷卻"+hero3.R+"秒.");
}
}
class LOL
{
String name;
int asi;
int R;
}
複製代碼
作者:
龔品誠
時間:
2021-1-23 11:39
public class Owo{
public static void main(String args[])
{
Drug marijuana=new marijuana();
marijuana.name="大麻";
marijuana.bags=1(t);
marijuana.bag=40(g);
Drug Heroin=new Heroin();
Heroin.name="海洛因";
Heroin.bags=2.3(t);
Heroin.bag=18(g);
Drug Cocaine=new Cocaine();
Cocaine.name="古柯鹼";
Cocaine.bags=4.7(t);
Cocaine.bag=7(g);
System.out.println(marijuana.name+"有"+marijuana.bags+"頓,每包重量:"+marijuana.bag+"克.");
System.out.println(Heroin.name+"有"+Heroin.bags+"頓,每包重量:"+Heroin.bag+"克.");
System.out.println(Cocaine.name+"有"+Cocaine.bags+"頓,每包重量:"+Cocaine.bag+"克.");
}
}
class Drug
{
String name;
int bags;
int bag;
}
複製代碼
作者:
李穎俊
時間:
2021-1-30 09:27
package aa;
public class aa {
public static void main(String[] args) {
Car bus=new Car();
bus.name="公車";
bus.wheel=6;
bus.lood=40;
Car truck=new Car();
truck.name="卡車";
truck.wheel=8;
truck.lood=3;
Car taxi=new Car();
taxi.name="計程車";
taxi.wheel=4;
taxi.lood=5;
System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.lood+"人");
System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.lood+"人");
System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.lood+"人");
}
}
class Car
{
String name;
int wheel;
int lood;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2