package Dec2016; public class Ques1209 { public static void main(String[] args){ People g = new People("張三"); // 定義監護人 People p1 = new People("兒子1", g); People p2 = p1.clone(); // 兒子2的信息通過拷貝兒子1來的 p2.setName("兒子2"); p1.getGuarder().setName("李四"); // 將兒子1的監護人改為李四 System.out.println(p1.getName()+"的監控人是:"+p1.getGuarder().getName()); System.out.println(p2.getName()+"的監控人是:"+p2.getGuarder().getName()); } } class People implements Cloneable{ private String name; private People guarder; // 監護人 /* get、set方法省略 */ public People(String name) { this.name = name; } public People(String name, People guarder) { this.name = name; this.guarder = guarder; } // 拷貝實現 @Override public People clone(){ People p = null; try { p = (People) super.clone(); } catch (Exception e) { e.printStackTrace(); } return p; } }
今日問題:
請問主程序能否正常運行輸出結果?(點擊以下“【Java每日一題】20161208”查看20161207問題解析)
題目原發布於公眾號、簡書:【Java每日一題】20161209,【Java每日一題】20161209