导航:首页 > 编程语言 > 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相关的资料

热点内容
精品php源码 浏览:958
自己编写云服务器抢红包 浏览:201
java解压缩文件加密 浏览:886
dlink打印服务器默认地址 浏览:351
php休眠函数 浏览:371
金蝶如何打开服务器 浏览:764
e4a手游辅助源码 浏览:776
什么app可以实时直播 浏览:105
苹果13的app闪退什么原因 浏览:774
尾盘选股源码公式 浏览:449
php日期运算 浏览:930
天龙八部长歌服务器什么时候开的 浏览:198
鬼泣4模型在那个文件夹 浏览:228
单片机的串行口 浏览:57
phpjson转化为数组 浏览:267
pdf导入excel 浏览:427
苹果xsmax信任app在哪里设置 浏览:52
自动外链php源码 浏览:244
我的世界新手奖励箱命令 浏览:145
linux更新vim 浏览:997