OOP继承扩展

时间:2017-01-04 13:05:46

标签: java inheritance override abstract

仅在获取阶段中使用一个摘要运行。我真的不知道为什么,OMG?

abstract public class Vehicle {
    public int nWheels = 0;
    public int VCapacity;

    // Set
    public void numWheels(int nWheels) {
        this.nWheels = nWheels;
    }
    public void VCapacity(int VCapacity) {
        this.VCapacity = VCapacity;
    }

    // Get
    public abstract int getWheels();
    public abstract int VCapacity();

    public Vehicle() { }

    public Vehicle(int nWheels, int VCapacity) {
        numWheels(nWheels);
        VCapacity(VCapacity);
    }
}

没有运行它说:

  

总线不是抽象的,并且不会覆盖车辆中的抽象方法VCapacity()   公共汽车扩展车辆

1 个答案:

答案 0 :(得分:1)

扩展abstract类时,需要覆盖父类中定义的方法。

public class Bus extends Vehicle {
    public Bus() {
        super(6, 4); // to set capacity use the Vehicle (int nWheels, int VCapacity) constructor
    }

    @Override
    public int VCapacity() {
    }
}

Vehicle中,它不接收参数。