導航:首頁 > 編程語言 > javaengine

javaengine

發布時間:2024-12-08 21:03:50

『壹』 java編程 類的繼承及多態

共六個類:Vehicle.java(車類), Car.java(轎車類), Bus.java(公共汽車類), Engine.java(發動機), VehicleFactory.java(汽車工廠) 和 VehicleTest.java(測試類),具體代碼分別如下:

***************************************************************

abstract class Vehicle {
Engine engine;
double speed;
double mileage;

public Vehicle() {
}

public Vehicle(Engine engine) {
this.engine = engine;
}

public Vehicle(Engine engine, double mileage) {
this.engine = engine;
this.mileage = mileage;
}

public Vehicle(Engine engine, double speed, double mileage) {
this.engine = engine;
this.speed = speed;
this.mileage = mileage;
}

public Engine getEngine() {
return engine;
}

public void setEngine(Engine engine) {
this.engine = engine;
}

public double getSpeed() {
return speed;
}

public void setSpeed(double speed) {
this.speed = speed;
}

public double getMileage() {
return mileage;
}

public void setMileage(double mileage) {
this.mileage = mileage;
}

public double drive() {
this.setMileage(speed * (Math.round(Math.random() * 6)) + mileage);
return mileage;
}

public abstract double calGC();

public String toString() {
return "Type: " + this.getClass().getName() + ", Horsepower: "
+ engine.horsepower + ", Speed: " + speed + ", Mileage: "
+ mileage + ", Gas consumed: " + calGC();
}

}

***************************************************************

public class Car extends Vehicle {
public Car() {

}

public Car(Engine engine) {
this.engine = engine;
}

public Car(Engine engine, double mileage) {
this.engine = engine;
this.mileage = mileage;
}

public Car(Engine engine, double speed, double mileage) {
this.engine = engine;
this.speed = speed;
this.mileage = mileage;
}

public double calGC() {
return mileage * 2;
}
}

***************************************************************

public class Bus extends Vehicle {
public Bus() {

}

public Bus(Engine engine) {
this.engine = engine;
}

public Bus(Engine engine, double mileage) {
this.engine = engine;
this.mileage = mileage;
}

public Bus(Engine engine, double speed, double mileage) {
this.engine = engine;
this.speed = speed;
this.mileage = mileage;
}

public double calGC() {
return mileage * 3 + 20;
}
}

***************************************************************

public class Engine {
double horsepower;

public Engine() {
this.horsepower = 1;
}

public Engine(double horsepower) {
this.horsepower = horsepower;
}

public double getHorsepower() {
return horsepower;
}

public void setHorsepower(double horsepower) {
this.horsepower = horsepower;
}

}

***************************************************************

public class VehicleFactory {
public static Vehicle makeVehicle(Engine e) {
if ((1 <= e.getHorsepower()) && e.getHorsepower() <= 50) {
return new Car(e, 0);
} else if ((50 < e.getHorsepower()) && e.getHorsepower() <= 100) {
return new Bus(e, 0);
}
return null;
}
}

***************************************************************

public class VehicleTest {
public static void main(String[] args) {
// 新建一輛馬力28, 速度50的車
Engine e1 = new Engine(28);
Vehicle v1 = VehicleFactory.makeVehicle(e1);
v1.setSpeed(50);
v1.drive();
v1.drive();
System.out.println("第一輛車:");
System.out.println(v1);
// 新建一輛馬力88, 速度88的車
Engine e2 = new Engine(88);
Vehicle v2 = VehicleFactory.makeVehicle(e2);
v2.setSpeed(88);
v2.drive();
v2.drive();
System.out.println("第二輛車:");
System.out.println(v2);
}
}

***************************************************************

運行結果如下:

第一輛車:
Type: Car, Horsepower: 28.0, Speed: 50.0, Mileage: 200.0, Gas consumed: 400.0
第二輛車:
Type: Bus, Horsepower: 88.0, Speed: 88.0, Mileage: 616.0, Gas consumed: 1868.0

***************************************************************

閱讀全文

與javaengine相關的資料

熱點內容
考勤表加密怎麼辦 瀏覽:735
arj壓縮與解壓批處理怎麼寫 瀏覽:658
php和大數據哪個好 瀏覽:930
未來最值得投資的加密貨幣 瀏覽:526
ascii碼是編譯的時候用嗎 瀏覽:781
壓縮機感應包可以通用嗎 瀏覽:412
方舟伺服器怎麼發布到搜索列表 瀏覽:270
xml防反編譯 瀏覽:241
數據傳輸加密系統技術方案 瀏覽:842
程序員沒有準備去面試 瀏覽:4
51單片機usb滑鼠 瀏覽:881
qq伺服器的ip地址查詢 瀏覽:112
java仿qq聊天 瀏覽:401
解壓的ipa重新打包 瀏覽:144
程序員那麼可愛vip版 瀏覽:240
程序員怎麼升職 瀏覽:245
圖形化命令按鈕vb 瀏覽:987
vcu盤加密怎麼設置 瀏覽:415
如何加密備份微信聊天記錄 瀏覽:529
安卓手機如何模擬鍵盤 瀏覽:932