我是 Java 初学者。我知道接口(interface)的概念。接口(interface)主要用于实现全抽象,支持多继承和松耦合的功能。
可以有抽象方法和静态常量。它不能被实例化,类似于抽象类。接口(interface)是类的蓝图,它代表 Is-A-Relationship。
我自己刚刚试过这个示例程序:
interface Printable{
void print();
}
public class A implements Printable{
public void Print(){
System.out.println("Prints..");
}
public static void main(String args[]){
A obj=new A();
obj.print();
}
}
输出是,
Compiling the source code....
$javac A.java 2>&1
A.java:4: error: A is not abstract and does not override abstract method print() in Printable
public class A implements Printable{
^
A.java:10: error: cannot find symbol
obj.print();
^
symbol: method print()
location: variable obj of type A
2 errors
“A 不是抽象的,不能覆盖抽象方法 print()”是什么意思?
我在这里犯了什么错误?这样我就可以从错误中吸取教训!
最佳答案
void print()
对比
void Print()
这是一个严重的错误。
你遇到了错误
A is not abstract and does not override abstract method print()
因为 abstract
类可以实现 interface
而无需实际实现它定义的方法。
参见 here :
In the section on Interfaces, it was noted that a class that implements an interface must implement all of the interface's methods. It is possible, however, to define a class that does not implement all of the interface methods, provided that the class is declared to be abstract.
错误消息的另一部分来自上面的“大”错误。
关于java - 我在代码的接口(interface)部分犯了什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20171613/