问题

今天继续在复习继承,在打代码时碰到了一个问题,百度过,但是没有找到解决的方法

package Vehicle;

public class Vehicle {
    int wheels;
    double weight;
    
    public Vehicle() {
        
    }
    public Vehicle(int wheels,double weight) {
        this.wheels = wheels;
        this.weight = weight;
    }
    public void printMSG() {
        System.out.println("车轮个数:" + wheels);
        System.out.println("车重:" + weight);
    }

}

package Vehicle;

public class Car extends Vehicle{
    static int loader;
    public Car() {
        
    }
    public Car(int wheels,double weight,int loader) {
        super(wheels,weight);
        this.loader = loader;
    }
    public void SetLoader() {
        this.loader = loader;
    }
    public int GetLoader() {
        return loader;
    }
    public void printMSG() {
        System.out.println("车轮个数:" + wheels + "个");
        System.out.println("车重" + weight + "千克");
        System.out.println("载人数:" + loader + "人");
    }

}
package Vehicle;

public class Truck extends Car{
    double payload;
    public Truck() {
        
    }
    public Truck(int wheels,double weight,double payload) {
        super(wheels,weight,loader);
        this.payload = payload;
    }
    public void printMSG() {
        System.out.println("车轮个数:" + wheels + "个");
        System.out.println("车重:" + weight + "千克");
        System.out.println("载人数:" + loader + "人");
        System.out.println("载重量:" + payload + "千克");
    }

}
package Vehicle;

public class Test {

    public static void main(String[] args) {
        Car c1 = new Car(4,120.0,6);
        c1.printMSG();
        Truck t1 = new Truck(12,1600.12,4000);
        t1.printMSG();

    }

}
这是根据系统提示之后所改的代码
之前知己的代码在Truck类继承Car类时,super();的调用出现了错误。

在Car类中写入了这段代码   protected int loader;

在Truck类继承用super调用时出错

系统提示,需要将int loader转换为静态   static int loader;

根据系统的提示,更改之后,代码能够成功运行。

问题:  为什么protected要换成static?

             用protected时,为什么super的调用会错问题?

转载于:https://www.cnblogs.com/pkz-hn/p/10678816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值