自建接口和现成接口的区别
The difference between the self-created interfaces and ready interfaces
我有一个关于接口的问题。
比如我创建一个接口,这个接口有3个不同的抽象方法。实现这个接口的class,必须在class中有这些方法,才能为它们写一个body。但是,当我实现一个就绪接口时,我不需要为这个特定接口具有的方法编写主体。例如,如果我在我的 class 中实现 "clonable" 接口,我不必为方法 "clone" 编写主体,该方法在可克隆接口中定义。为什么会这样?
感谢您的时间和兴趣。
如果直接实现Closeable接口,需要实现close()
。
如果您正在扩展一个已经实现了 close()
的 class,那么您的超级class 已经完成了合同,您不需要覆盖它。
来自 Oracle 的文档:
If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
Link 这里:https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
没有方法的接口在Java中称为"marker"个接口。
Serializable
and Cloneable
接口就是此类接口的示例。
引用自Wikipedia page on marker interfaces:
Whereas a typical interface specifies functionality (in the form of
method declarations) that an implementing class must support, a marker
interface need not do so. The mere presence of such an interface
indicates specific behavior on the part of the implementing class.
另一方面,Closeable
不是这样的接口:
它定义了一个 close()
方法,您必须实现该方法。
也许一个例子能更好地解释这一点,见下文。
不管你声明的是接口还是其他人。要实例化一个对象,它必须有一个具体的类型,即它不能有任何抽象方法。如果 class 本身将任何方法限定为 abstract
或者如果它不实现任何从其超 class 或超接口继承的抽象方法,则它具有抽象方法,在继承层次结构中上升.
interface A{
void method1();
void method2();
}
abstract class B1 implements A {
// B1 must be declared abstract as it does not provide implementation of method2
@Override public void method1() {}
}
class B2 extends B1{
@Override public void method2() {}
}
class B3 extends B2{
static void bar(){
B1 b1 = new B1(); // ERROR B1 is abstract
B1 b2 = new B2(); // OK, all methods of A are implemented
B1 b3 = new B3(); // as above
}
}
我有一个关于接口的问题。
比如我创建一个接口,这个接口有3个不同的抽象方法。实现这个接口的class,必须在class中有这些方法,才能为它们写一个body。但是,当我实现一个就绪接口时,我不需要为这个特定接口具有的方法编写主体。例如,如果我在我的 class 中实现 "clonable" 接口,我不必为方法 "clone" 编写主体,该方法在可克隆接口中定义。为什么会这样?
感谢您的时间和兴趣。
如果直接实现Closeable接口,需要实现close()
。
如果您正在扩展一个已经实现了 close()
的 class,那么您的超级class 已经完成了合同,您不需要覆盖它。
来自 Oracle 的文档:
If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
Link 这里:https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
没有方法的接口在Java中称为"marker"个接口。
Serializable
and Cloneable
接口就是此类接口的示例。
引用自Wikipedia page on marker interfaces:
另一方面,Whereas a typical interface specifies functionality (in the form of method declarations) that an implementing class must support, a marker interface need not do so. The mere presence of such an interface indicates specific behavior on the part of the implementing class.
Closeable
不是这样的接口:
它定义了一个 close()
方法,您必须实现该方法。
也许一个例子能更好地解释这一点,见下文。
不管你声明的是接口还是其他人。要实例化一个对象,它必须有一个具体的类型,即它不能有任何抽象方法。如果 class 本身将任何方法限定为 abstract
或者如果它不实现任何从其超 class 或超接口继承的抽象方法,则它具有抽象方法,在继承层次结构中上升.
interface A{
void method1();
void method2();
}
abstract class B1 implements A {
// B1 must be declared abstract as it does not provide implementation of method2
@Override public void method1() {}
}
class B2 extends B1{
@Override public void method2() {}
}
class B3 extends B2{
static void bar(){
B1 b1 = new B1(); // ERROR B1 is abstract
B1 b2 = new B2(); // OK, all methods of A are implemented
B1 b3 = new B3(); // as above
}
}