问题
作者:互联网
今天继续在复习继承,在打代码时碰到了一个问题,百度过,但是没有找到解决的方法
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的调用会错问题?
标签:weight,int,System,loader,问题,wheels,public 来源: https://www.cnblogs.com/pkz-hn/p/10678816.html