java中对象的复制

来源:互联网 发布:宿舍破解版路由器mac 编辑:程序博客网 时间:2024/06/05 08:35


java中简单变量默认是值传递,类使用的是引用传递,那么如何复制一个不影响原类的新类呢?

Object中提供了clone()方法

class test implents Cloneable{test t1 = new test();  test t2 = null;  try {   t2 = (test)t1.clone();  } catch (CloneNotSupportedException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }}

0 0
原创粉丝点击