浅谈关于java中的深浅拷贝
一.浅拷贝(shallow copy) 1.如何实现浅拷贝? Object类 是所有类的直接或间接父类,Object中存在clone方法,如下 protected native Object clone() throws CloneNotSupportedException; 如果想要使一个类的对象能够调用clone方法 ,则需要 实现Cloneable接口, 并重写 clone方法 : public class Student implements Cloneable{ private int sno ; private String name; //getter ,setter 省略 @Override public Object clone() throws CloneNotSupportedException { Student s = null; try{ s = (Student)super.clone(); }catch (Exception e){ e.printStackTrace(); } return s; } } 现在测试clone方法: @Test public void test04() throws CloneNotSupportedException { //创建Student对象 Student s1 = new Student(); s1