Mandatory cloneable interface in Java

后端 未结 6 1598
北恋
北恋 2021-01-06 09:01

I\'m having a small problem in Java. I have an interface called Modifiable. Objects implementing this interface are Modifiable.

I also have a ModifyCommand class (wi

6条回答
  •  生来不讨喜
    2021-01-06 09:37

    Adding to Sean Reilly's answer, this should solve your problem, and is more type safe. It compiles and runs fine with me on JDK6:

    public interface Modifiable> extends Cloneable {
        T clone();
    }
    public class Test implements Modifiable {
        @Override
        public Test clone() {
            System.out.println("clone");
            return null;
        }
        public static void main(String[] args) {
            Test t = new Test().clone();
        }
    }
    

    I couldn't test it with Java 5 because I don't have it installed, but I guess it would work fine.

提交回复
热议问题