【发布时间】:2012-04-21 12:49:09
【问题描述】:
这是我遇到问题的代码:
界面:
public interface anInterface {
void printSomething();
}
实现接口的类:
public class aClass implements anInterface {
public aClass() {
}
public void printSomethingElse() {
System.out.println("Something else");
}
@Override
public void printSomething() {
System.out.println("Something");
}
}
以及主要功能:
public static void main(String[] args) {
anInterface object = new aClass();
object.printSomething(); // works fine
object.printSomethingElse(); // error
}
错误:找不到符号。 符号:方法 printSomethingElse();
谁能告诉我为什么这不起作用?
在 Java 中,当您有一个实现某个接口的类时,是否可以向该类添加方法,即使这些方法尚未在接口中声明?还是我必须声明我将在接口中使用的所有方法?
我在 C# 中也试过了,也没用。
我做错了什么?
谢谢!!!
【问题讨论】:
标签: java class interface methods implements