练习题——继承

– 在包bzu.aa中定义一个交通工具类(Vehicle):

 属性——载客量(capacity)
 方法
 无参构造方法(给capacity初始化值为2,并输出“执行交通工具类的无参构造方法。”)
 有参构造方法(传参给capacity初始化,并输出“执行交通工具的有参构造方法。”)
 capacity的set、get方法
 print方法:输出capacity
– 在包bzu.aa中定义一个汽车类(Car)继承交通工具类:
 属性——speed
 方法
 无参构造方法(给speed初始化值为0,并输出“执行汽车类的无参构造方法。”)
 有参构造方法(用super关键字调用父类的有参构造方法,传参给speed初始化,并输出“执行汽车类的有参构造方法。”)
 加速(speedup):speed+10并返回speed;
 减速(speeddown):speed-15并返回speed;
 重写print方法:输出speed和capacity。
– 在包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方法。

##

package bzu.aa;
 public class Vehicle {
    int capacity;
    Vehicle(){
        capacity=2;
        System.out.println("执行交通工具类的无参构造方法");       
    }
    Vehicle(int c){
        capacity=c;
        System.out.println("执行交通工具的有参构造方法");

    }
    public int getCapacity() {
        return capacity;
    }
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    public void print(){
        System.out.println("载客量为"+capacity);
    }


}
package bzu.aa;

public class Car extends Vehicle{
    int speed;
    Car(){
        speed=0;
        System.out.println("执行汽车类的无参构造方法");
    }
    Car(int i){
        super(i);
        //speed=i;
        System.out.println("执行汽车类的有参构造方法");
    }
    public int speedup(){
        speed=speed+10;
        return speed;
    }
    public int speeddown(){
        speed=speed-15;
        return speed;
    }
    public void print(){
        System.out.println("速度为"+speed);
        System.out.println("载客量为"+capacity);
    }

}
package bzu.aa;

public final class Bus extends Car {

    int capacity;
    Bus(){
        capacity=20;
        System.out.println("执行公交车类的无参构造方法");
    }
    Bus(int b){
        super(b);//传参给capacity初始化
        //capacity=b;
        System.out.println("执行公交车类的有参构造方法");

    }
    public void print(){
        System.out.println("速度为"+speed);
        System.out.println("载客量为"+capacity);
        System.out.println("父类的载客量为"+super.capacity);

    }
}
package bzu.aa;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car car=new Car();
        //car.speed=40;
        while(car.speed<50)
        car.speedup();
        car.print();
        while(car.speed>20)
        car.speeddown();
        //car.speeddown();
        car.print();
        Bus bus=new Bus(30);
        bus.print();

    }

}

这里写图片描述

package bzu.bb;

import bzu.aa.*;

   public final class Bus1 extends Car {

        int capacity;
       Bus1(){
            capacity=20;
            System.out.println("执行公交车类的无参构造方法");
        }
        Bus1(int b){
            super(b);//传参给capacity初始化
            //capacity=b;
            System.out.println("执行公交车类的有参构造方法");

        }
        public void print(){
            System.out.println("速度为"+speed);
            System.out.println("载客量为"+capacity);
            System.out.println("父类的载客量为"+super.capacity);

        }
    }



“`
package bzu.bb;
import bzu.aa.*;

public class Test1 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Car car=new Car();
    //car.speed=40;
    while(car.speed<50)
    car.speedup();
    car.print();
    while(car.speed>20)
    car.speeddown();
    //car.speeddown();
    car.print();
    Bus1 bus=new Bus1(30);
    bus.print();

}

}

“`这里写图片描述

如果类不在同一个包中,被调用的变量,类,方法必须是public

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值