有人可以解释一下java中标记接口的合同吗?
例如:如果Clonable
是一个没有字段/方法的标记接口,那么clone()
定义在哪里?
为什么我们要在使用Clonable
时实现 i/f clone()
?
那么我的问题是,如果clone()
是java.lang.Object
类的方法,为什么要实现Clonable
i/f 来覆盖clone()
.
有人可以详细说明 java 的这种约定吗?
提前致谢
有人可以解释一下java中标记接口的合同吗?
例如:如果Clonable
是一个没有字段/方法的标记接口,那么clone()
定义在哪里?
为什么我们要在使用Clonable
时实现 i/f clone()
?
那么我的问题是,如果clone()
是java.lang.Object
类的方法,为什么要实现Clonable
i/f 来覆盖clone()
.
有人可以详细说明 java 的这种约定吗?
提前致谢
clone()
java.lang.Object
在所有类扩展的类中定义,但是它是protected
. 这实际上是一个具体的方法实现,它逐个字段地克隆对象,但前提是您已经实现了Cloneable
接口以表明这是允许的。
在实践中,许多人重写了该clone()
方法,以便他们可以制作它public
并允许从类外部进行克隆。
这整个模式非常不寻常,不是您通常会复制的东西,我想不出 JVM 中有许多其他示例,其中有配对的标记接口和方法。从Java 5开始,最好使用注解作为标记。例如,@XmlRootElement
用于将类型标记为 Jax-B 可序列化(Java 5 之后)与Serializable
用于指示类是二进制可序列化的接口(Java 5 之前)的对比。
What is a marker interface ?
Interface which does not contain any method to implement are called marker or the tag interfaces.
Why marker interfaces ?
The basic idea of having marker interfaces are to mention that the class is implementing an interface of which the behavior is implicit. The class is not expected to implement anything to adhere to the contract defined by the interface. In contrast it is to indicate the JVM regarding an expected functionality to perform implicitly.
Java Example
Can we create custom marker interfaces ?
Yes, It is possible.
Clonable
不包含clone()
受保护的方法java.lang.Object
。
更多信息在这里
来自 Josh Bloch 的 Effective Java 的引文:
“Cloneable 接口旨在作为对象的 mixin 接口,用于宣传它们允许克隆。不幸的是,它无法达到此目的......这是一种非常非典型的接口使用,而不是要模拟的......为了实现接口要对一个类产生任何影响,它和它的所有超类都必须遵守一个相当复杂、无法执行且大部分未记录的协议”
标记接口是标记类的常用技术。它们不会向类添加行为(通常)。Clonable
接口就是这样一个标签:每个带有标签的类都Clonable
能够克隆自己(这是规则)。
Same with Serializable
, although there is some more hidden magic behind that marker interface (the object serializer looks for some methods and fields, that the tagged class may implement or not)
Bonus info: forget about Clonable
, its broken. If you want to create clones in real life, look for the copy constructor pattern.
The java.lang.object
class is a super/parent class of all java classes,
if you want to create an object in java ,then it should be implements java.lang.object
class.
If you are not importing Object super class in your code then compiler will implicitly import that in your code..
SO automatically all its properties and behavior is available for you are object(program) ,including clone() method, if you call clone() method in your program that mean clone() method is called from super class (Object class),not from child class.
Marker Interfaces: Its true Marker Interfaces are empty interface it does not contains properties and behaviors. Now, The Question may be Raised.
Q. Who will implement Predefined Marker interface, if used in our program?
Answer: JVM will take that responsibility because, inside JVM that Marker interface functionality is defined, so it implements and adding some advance functionality to you are program.
So programmer no need to implements Clonable
Marker interface, JVM will take that responsibility.
标记接口本身没有任何主体。它们只是要求 java 解释器以预定义的特定方式来处理扩展它们的类的对象。