我調用removeAll方法從我的tempDictionary中刪除,但它從d中刪除。那麼,我該如何避免隱私泄漏,併爲tempDictionary分配新的參考。如何使用現有對象的新引用創建新對象java
public static void play1(Dictionary d, int level)throws IOException
{
Dictionary tempDictionary = d ;
tempDictionary.removeAll();
}
//after looking to the link doesn't compile I tried to do exact same
class Dictionary {
private Dictionary dictionary; public Dictionary(Dictionary temp) {
this.dictionary = temp.dictionary; // error
}
}
http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java –
類字典{ 私人詞典的可能重複;公共字典(Dictionary temp){this.dictionary = temp.dictionary; //會工作? } } – Anny
在複製構造函數中,您應該分配類Dictionary的字段,而不是對象本身。你如何實現Dictionary類? –