我无法理解为什么我在下面指出的行上收到错误.如果有特定主题涵盖它,请提供一行(或解释).
public interface Button {
void press();
}
public class Bomb implements Button {
public void press() {
System.out.println("Doesn't work");
}
void boom() {
System.out.println("Exploded");
}
}
public class Test {
public static void main(String[] args) {
Button redButton = new Bomb();
redButton.press();
redButton.boom(); // <-- Error is on this line.
}
}
Run Code Online (Sandbox Code Playgroud)
Button redButton = new Bomb();
Run Code Online (Sandbox Code Playgroud)
接口Button未定义方法boom().
运行时实例可能是Bomb(有boom()),但编译器不知道(它只看到编译时类型,即Button).
您需要使用类定义的接口Bomb:
Bomb redButton = new Bomb();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43 次 |
| 最近记录: |