设计一个汽车类 Car 类,包含的属性有车轮的个数 wheels 和车重 weight。 小汽车类 SmallCar 是 Car 类的子类。包含的属性有载人数 loader。卡车 类 Truck 类是 SmallCar 类的子类,其中包含的属性有载重量 payload,每 个类都有构造方法和输出相关数据的方法。编写程序测试类。java编写
时间: 2024-03-27 11:35:13 浏览: 32
行业分类-设备装置-全方位轮及使用该全方位轮的全向移动平台.zip
好的,这是一个简单的实现:
```java
public class Car {
private int wheels;
private double weight;
public Car(int wheels, double weight) {
this.wheels = wheels;
this.weight = weight;
}
public int getWheels() {
return wheels;
}
public double getWeight() {
return weight;
}
public void display() {
System.out.println("Wheels: " + wheels);
System.out.println("Weight: " + weight);
}
}
public class SmallCar extends Car {
private int loader;
public SmallCar(int wheels, double weight, int loader) {
super(wheels, weight);
this.loader = loader;
}
public int getLoader() {
return loader;
}
public void display() {
super.display();
System.out.println("Loader: " + loader);
}
}
public class Truck extends SmallCar {
private double payload;
public Truck(int wheels, double weight, int loader, double payload) {
super(wheels, weight, loader);
this.payload = payload;
}
public double getPayload() {
return payload;
}
public void display() {
super.display();
System.out.println("Payload: " + payload);
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car(4, 1200);
car.display();
SmallCar smallCar = new SmallCar(4, 800, 5);
smallCar.display();
Truck truck = new Truck(6, 5000, 2, 10000);
truck.display();
}
}
```
输出结果为:
```
Wheels: 4
Weight: 1200.0
Wheels: 4
Weight: 800.0
Loader: 5
Wheels: 6
Weight: 5000.0
Loader: 2
Payload: 10000.0
```
阅读全文