Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要Java中模板参数的可克隆性_Java_Oop_Clone_Cloneable - Fatal编程技术网

需要Java中模板参数的可克隆性

需要Java中模板参数的可克隆性,java,oop,clone,cloneable,Java,Oop,Clone,Cloneable,我有一个Java泛型类,我想克隆它,以便执行它的深度复制。 现在,我认为下面的代码可以工作,但是在clone()方法中 我不能为不是基元类型的每个成员调用clone()。 我试图要求参数来实现可克隆接口,但id还不能工作,它无法调用[first/second].clone() 如果有任何建议,我将不胜感激。 提前谢谢 [这是密码] public class Pair<F extends Cloneable, S extends Cloneable> implements Clonea

我有一个Java泛型类,我想克隆它,以便执行它的深度复制。 现在,我认为下面的代码可以工作,但是在clone()方法中 我不能为不是基元类型的每个成员调用clone()。 我试图要求参数来实现可克隆接口,但id还不能工作,它无法调用[first/second].clone()

如果有任何建议,我将不胜感激。 提前谢谢

[这是密码]

public class Pair<F extends Cloneable, S extends Cloneable> implements Cloneable {

    private F first;
    private S second;

    public Pair(F a, S b) {

        first = a;
        second = b;
    }

    public F getFirst() {
        return first;
    }

    public S getSecond() {
        return second;
    }

    public void setFirst(F first) {
        this.first = first;
    }

    public void setSecond(S second) {
        this.second = second;
    }

    @Override
    public String toString() {
        return "["+first+", "+second+"]";
    }

    @Override
    public boolean equals(Object o) {

        if(o == null)   return false;
        if(o == this)   return true;
        if(!getClass().getName().equals(o.getClass().getName()))    return false;

        Pair ref = (Pair)o;

        return first.equals(ref.first) && second.equals(ref.second);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 61 * hash + Objects.hashCode(this.first);
        hash = 61 * hash + Objects.hashCode(this.second);
        return hash;
    }

    @Override
    public Object clone() {

        try {

            Pair<F, S> cloned = (Pair<F, S>)super.clone();

            cloned.first = (F)first.clone();
            cloned.second = (S)second.clone();

            return cloned;
        }
        catch(CloneNotSupportedException e) {
            return null;
        }
    }

}
公共类对实现可克隆{
私人优先;
第二,私营企业;
公共对(F a,S b){
第一个=a;
第二个=b;
}
公共F getFirst(){
先返回;
}
公共S getSecond(){
返回第二;
}
公共无效设置优先(F优先){
this.first=first;
}
公共空间设置秒(秒){
这个秒=秒;
}
@凌驾
公共字符串toString(){
返回“[”+第一个+”,“+第二个+”];
}
@凌驾
公共布尔等于(对象o){
如果(o==null)返回false;
如果(o==this)返回true;
如果(!getClass().getName().equals(o.getClass().getName())返回false;
对ref=(对)o;
返回first.equals(ref.first)和&second.equals(ref.second);
}
@凌驾
公共int hashCode(){
int hash=7;
hash=61*hash+Objects.hashCode(this.first);
hash=61*hash+Objects.hashCode(此秒);
返回散列;
}
@凌驾
公共对象克隆(){
试一试{
Pair cloned=(Pair)super.clone();
cloned.first=(F)first.clone();
cloned.second=(S)second.clone();
返回克隆;
}
捕获(CloneNotSupportedException e){
返回null;
}
}
}

不幸的是,对于具有公共
.clone()
方法的对象,Java中没有通用接口或超类型。您可以创建自己的接口来执行此操作,但只有您自己的类可能会实现此接口,因此它对于可能具有公共
.clone()
方法的系统类仍然不起作用。

当您说“调用[first/second].clone()失败”时,失败的具体原因是什么?编译错误?例外?你能告诉我们错误的文本吗?请注意,
Clonable
是。它给了我一个编译时错误,说方法clone()没有可用的实现。如果你使用的是你自己创建的类,你能发布第一个和第二个类的代码吗!,我会想办法的