2020-10-07

    科技2024-11-21  18

    构造方法的重载: 1.定义一个Vehicle交通工具的基类,博涵String类型的成员属性brand商标,color颜色, 成员方法在控制台显示“已启动”和showInfo显示信息在控制台显示商标和颜色并编写构造方法初始化成员属性 编写Car小汽车类继承与Vehicle类增加int类成员seats座位还应增加成员方法showCar在控制台显示小汽车的信息并编写构造方法 编写Truck卡车类继承Vehicle类增加float类型成员属性load载重还应增加成员方法showTruck在控制台显示卡车信息并编写构造方法 在main方法中测试

    class Vehicle{ String brand; String color; public Vehicle(){ } //构造方法的重载 public Vehicle(String brand,String color){ this.brand = brand; this.color = color; } public void run(){ System.out.println("已启动"); } public void showInfo(){ System.out.println("商标:"+brand+" 颜色:"+color); } } class Car extends Vehicle{ int seats; public Car(){ } //构造方法的重载 public Car(String brand,String color,int seats){ super(brand,color); this.seats = seats; } public void showCar(){ System.out.println("商标:"+brand+" 颜色:"+color+" 座位数:"+seats); } } class Truck extends Vehicle{ float load; public Truck(){ } //构造方法的重载 public Truck(String brand,String color,float load){ super(brand,color); this.load = load; } public void showTruck(){ System.out.println("商标:"+brand+" 颜色:"+color+" 载重:"+load+"kg"); } } public class Test{ public static void main(String[] args) { Vehicle vehicle = new Vehicle(); vehicle.brand = "宝马"; vehicle.color = "蓝色"; vehicle.run(); vehicle.showInfo(); Vehicle vehicle1 = new Vehicle("奔驰","白色"); vehicle1.showInfo(); Car car = new Car("凯迪拉克","紫色",5); car.showCar(); Truck truck = new Truck("大运","红色",1000); truck.showTruck(); } }
    Processed: 0.010, SQL: 8