实现对象克隆的Java代码示例
时间: 2023-06-01 16:05:41 浏览: 119
以下是一个简单的Java代码示例来实现对象克隆:
```
public class Person implements Cloneable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public Person clone() throws CloneNotSupportedException {
return (Person) super.clone();
}
public static void main(String[] args) {
Person person1 = new Person("张三", 18);
try {
Person person2 = person1.clone();
System.out.println(person2.name + " " + person2.age);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
```
阅读全文