Java,具有访问限制的克隆类 [英] Java, cloning classes with access restrictions
本文介绍了Java,具有访问限制的克隆类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
所以我有:
类Personne实现了Cloneable。 ..
personne课程在其字段中使用Adress课程。
私人Adresse adresse = ADRESSE_UNKNOWN;
所以我想要的是有一个选项:
修改克隆的人员地址
但原来的人class不能访问Cloned Person类Adress字段。
请给我一个技巧。
Hi everybody,
So i have :
class Personne implements Cloneable...
The personne class uses Adress class in it's field.
private Adresse adresse=ADRESSE_UNKNOWN;
So what i want is having a option to :
modify the cloned Person adress
BUT that the original Person class won t have the access to the Cloned Person class Adress field.
Please give me a tip guys.
推荐答案
Ok伙计们,所以在我理解的帮助下。
重写clone()方法的可能性是让opurtunity修改克隆类的字段。作为建议的解决方案,将类的Adress字段重置为NULL。然后我创建了一个setter,它提供了修改克隆地址字段的选项。
Ok Guys , so with help i understood.
The possibility to override a clone() method is giving the opurtunity to modify the fields of the cloning class. As a proposed solution a reset the Adress field of the class to NULL. Then i created a setter that giving an option to modify the clone adress field.
@Override
public Personne clone()
{
Personne o;
try {
o = (Personne)super.clone();
o.adresse = null;
return o;
} catch(CloneNotSupportedException cnse) {
cnse.printStackTrace(System.err);
throw new RuntimeException();
}
}
// setter pour l adresse du Clone :
public void setAdresseClone(Adresse a){
this.clone().adresse = a;
}
这篇关于Java,具有访问限制的克隆类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文