java问题,求大神解决
左右之间 2015-03-02 01:38:37 1、怎么调用父类被重写的方法?创建了子类对象,用super.方法名的方式为什么会编译错误?
class Z {
static void f() {
System.out.println("Hello!");
}
}
class B extends Z {
static void f() {
System.out.println("Goodbye!");
}
public static void main(String[] args) {
B bb = new B();
super.f();
}
}
错误:
-------------------------------------------------------------------------------
Z.java:14: 错误: 无法从静态上下文中引用非静态 变量 super
super.f();
^
1 个错误
-------------------------------------------------------------------------------
2、为什么没有结果输出?
代码:
class Computer {
public void turnOn(Computer computer) {
}
public static void main(String[] args) {
Computer computer = new Computer();
computer.turnOn(new PC());
computer.turnOn(new NBC());
}
}
class PC extends Computer {
public void turnOn() {
System.out.println("PC has turn on");
}
}
class NBC extends Computer {
public void turnOn() {
System.out.println("NBC has turn on");
}
}