日期:2014-05-20  浏览次数:20688 次

接口中继承Cloneable接口怎么写?
比如
interface InA extends Cloneable {
  public void method1();
}

public Class C1 implements InA {

}

public Class C2 {
  public static void main(String[] args) {
  InA a = new C1();
  InB = a.clone(); //出错,不能这样写,需要怎么实现这个功能?
//提示clone()方法不存在
  }
}

------解决方案--------------------
clone方法在Object类中,不过是protected限定

Java code
interface InA extends Cloneable {
    public void method1();
    public Object clone() throws CloneNotSupportedException;
}

class C1 implements InA {
    public void method1(){}
    public Object clone() throws CloneNotSupportedException {return super.clone();}
}

public class C2 {
    public static void main(String[] args) throws Exception {
        InA a = new C1();
        Object b = a.clone(); //出错,不能这样写,需要怎么实现这个功能?
        System.out.println(a);
        System.out.println(b);
    }
}