java 浅复制 代码

时间:2023-03-09 19:03:07
java 浅复制 代码

1  类实现Cloneable接口

2  重写clone()方法

3  类变量引用类型无法复制

class Dog extends Pet implements Cloneable{

int c;
 int d;
 public Dog clone() {
  Dog clone=null;
  try {
   clone=(Dog) super.clone();
  } catch (CloneNotSupportedException e) {
   e.printStackTrace();
  }
  return clone;
 }
}