我已经重写了一个方法并尝试访问该重写的方法,但是编译器说“无法从静态内容中引用非静态变量super”。
我尝试在子类的构造函数中调用重写的方法,效果很好,但是我想在构造函数之外访问它。
class vehicle{
static String name;
static int model;
static int price;
static String station;
public vehicle(){
model=220;
name="Ducati";
price=500000;
station="London";
}
public static void display(){
System.out.println("Name of the vehicle: "+name+"\nmodel: "+model+"\nprice: ```"+price+"\nservice station: "+station);
}
}
class bike extends vehicle{
static double rate;
public bike(){
rate=0.25;
}
public static void display(){
System.out.println("Vehicle name: "+name+"\nmodel number: "+model+"\ncost: "+price+"\nservice station: "+station+"\n discount rate: "+rate);
}
public void discount(){
double rate2=price*rate;
double rate3=price-rate2;
System.out.println("Discount rate of the bike is "+rate3);
}
public static void main(String args[]){
bike bee=new bike();
super.display();
display();
bee.discount();
}
}
我想知道为什么我无法在代码的指定位置访问阴影方法
答案 0 :(得分:0)
super。您可以使该方法不是静态的,然后可以调用它。