1.
类:交通工具 Vehicles
属性:商标String brand 颜色 String color
方法:行驶 run 在控制台显示“我已经开动了”
显示信息 showInfo 在控制台显示商标和颜色
构造方法初始化其成员属性。
子类:小汽车 Car 继承于Vehicles类
增加属性: int seats 座位
增加方法:showCar 在控制台显示小汽车的信息
构造方法
子类:卡车Truck 类继承于Vehicles类
增加属性:float load 载重
增加方法:showTruck 在控制台显示卡车的信息
构造方法
测试类:实例化每一种对象,调用方法
package Vehicles;
public class Vehicles {
// 属性:商标String brand 颜色 String color
// 方法:行驶 run 在控制台显示“我已经开动了”
// 显示信息 showInfo 在控制台显示商标和颜色
protected String brand;
protected String color;
public Vehicles() {}
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);
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
package Vehicles;
public class Car extends Vehicles {
/