public class Vehicles {
private String brand;
private String color;
//构造方法
public Vehicles(String brand, String color) {
this.brand = brand;
this.color = color;
}
public void run() {
System.out.println("我已经开动了");
}
public void showinfo() {
System.out.println("商标: " + brand);
System.out.println("颜色: " + color);
}
}
保存为Vehicles.java
/////////////////////////////////////////////////////////////
public class Car extends Vehicles {
private int seats;
//构造方法
public Car(String brand, String color, int seats) {
super(brand, color);
this.seats = seats;
}
public void showCar() {
super.showinfo();
System.out.println("座位: " + seats + " 个");
}
}
保存为 Car.java
//////////////////////////////////////////////////////////////////////////////
public class Truck extends Vehicles {
private float load;
public Truck(String brand, String color, float load) {
super(brand, color);
this.load = load;
}
public void showTruck() {
super.showinfo();
System.out.println("载重 :" + load + "吨");
}
}
保存为Truck.java
//////////////////////////////////////////////////////////////////////////////
public class Test {
public static void main(String[] args) {
Vehicles vehicle = new Vehicles("奥迪", "黑色");
vehicle.showinfo();
Car car = new Car("桑塔纳", "红色", 5);
car.showCar();
Truck truck = new Truck("解放", "蓝色", 10);
truck.showTruck();
}
}