介绍
标记接口是没有变量和方法的接口(空接口),例如Serializable,Cloneable和Remote接口,这些接口都是标记接口
public interface Serializable
{
// nothing here
}
Cloneable接口
这个接口在java.lang包中。Object类中有clone()方法,当一个类实现Cloneable接口后,表示这个类可以进行合理的实体变量复制,如果没有实现该接口,当调用Object的clone方法时,会报 CloneNotSupportedException异常,通过实现该接口的类,要重写Object的clone方法。
// Java program to illustrate Cloneable interface
import java.lang.Cloneable;
// By implementing Cloneable interface
// we make sure that instances of class A
// can be cloned.
class A implements Cloneable
{
int i;
String s;
// A class constructor
public A(int i,String s)
{
this.i = i;
this.s = s;
}
// Overriding clone() method
// by simply calling Object class
// clone() method.
@Override