• 在包bzu.aa中定义一个交通工具类(Vehicle):
    • 属性——载客量(capacity
    • 方法
      • 无参构造方法(给capacity初始化值为2,并输出执行交通工具类的无参构造方法。
      • 有参构造方法(传参给capacity初始化,并输出执行交通工具的有参构造方法。
      • capacitysetget方法
      • print方法:输出capacity
  • 在包bzu.aa中定义一个汽车类(Car)继承交通工具类:
    • 属性——speed
    • 方法
      • 无参构造方法(给speed初始化值为0,并输出执行汽车类的无参构造方法。
      • 有参构造方法(用super关键字调用父类的有参构造方法,传参给speed初始化,并输出执行汽车类的有参构造方法。
      • 加速(speedup):speed+10并返回speed
      • 减速(speeddown):speed-15并返回speed
      • 重写print方法:输出speedcapacity
  • 在包bzu.bb中定义一个final的公交车类(Bus),继承汽车类:
    • 属性——载客量(capacity<变量隐藏>
    • 方法
      • 无参构造方法(给capacity初始化值为20,并输出执行公交车类的无参构造方法。
      • 有参构造方法(用super关键字调用父类的有参构造方法,传参给capacity初始化,并输出执行公交车类的有参构造方法。
      • 重写print方法:输出speed capacity及父类的capacity
  • 在包bzu.bb中编写一个主类Test
    • 主函数
      • 调用无参构造方法创建一个Car的对象car;调用加速方法将速度加至50,调用print方法;调用减速方法,将速度减至20,调用print方法。
      • 调用有参构造方法创建一个Bus的对象bus;调用print方法。
        1. package bzu.aa;  
        2.   
        3. public class Vehicle {  
        4. int capacity;  
        5. public Vehicle(){  
        6.     capacity=2;  
        7.     System.out.println("执行交通工具类的无参构造方法");  
        8. }  
        9. public Vehicle(int capacity){  
        10.     this.capacity=capacity;  
        11.     System.out.println("执行交通工具类的有参构造方法");  
        12. }  
        13. int set(int capacity){  
        14.     this.capacity=capacity;  
        15.     return capacity;  
        16. }  
        17. int get(){  
        18.     return capacity;  
        19. }  
        20. public void print(){  
        21.     System.out.println("汽车的载客量为"+capacity);  
        22. }  
        23. }  
        24. package bzu.aa;  
        25.   
        26. public class Car extends Vehicle {  
        27.     public int speed;  
        28.     public Car(){  
        29.         super();  
        30.         speed = 0;  
        31.         System.out.println("执行汽车类的无参构造方法");  
        32.     }  
        33.     public Car(int speed){  
        34.         super(5);  
        35.         this.speed = speed;  
        36.         System.out.println("执行汽车类的有参构造方法");  
        37.     }  
        38.     int speedUp(int speed){  
        39.         this.speed = speed + 10;  
        40.         return speed;  
        41.     }  
        42.     int speedDown(int speed){  
        43.         this.speed = speed - 15;  
        44.         return speed;  
        45.     }  
        46.     public void print(){  
        47.         System.out.println("汽车的载客量为"+capacity+";汽车的速度为"+speed);  
        48.     }  
        49.     }  
        50. package bzu.aa;  
        51.   
        52. public class Bus extends Car {  
        53.     int capacity;  
        54.     public Bus(){  
        55.         super();  
        56.         capacity = 30;  
        57.         System.out.println("执行公交车类的无参构造方法");  
        58.     }  
        59.     public Bus(int capacity){  
        60.         super(10);  
        61.         this.capacity = capacity;  
        62.         System.out.println("执行公交车类的有参构造方法");  
        63.     }  
        64.     public void print(){  
        65.         System.out.println("公交车的载客量为"+capacity+";公交车的速度为"+speed+";交通工具的载客量为"+get());  
        66.     }  
        67. }  
        68. package bzu.aa;  
        69.   
        70. public class Test {  
        71.   
        72.     public static void main(String[] args) {  
        73.         // TODO Auto-generated method stub  
        74.         Car car = new Car();  
        75.         car.speedUp(50);  
        76.         car.print();  
        77.         car.speedDown(20);  
        78.         car.print();      
        79.         Bus bus = new Bus(25);  
        80.         bus.print();  
        81.     }  
        82.   
        83. }  
        lesson3 上机练习题-继承lesson3 上机练习题-继承lesson3 上机练习题-继承lesson3 上机练习题-继承

相关文章: