直接上代码。。。。。。。重写哪部分在代码里有
import java.util.*;
public class Main {
public static void main(String args[]) {
Person p1 = new Person("张三",12, "中国", "长沙");
Person p2 = (Person) p1.clone();
Person p3 = p1;
p1.street = "杭州";
p1.setName("李四");
p1.getNameAge();
System.out.println("深度拷贝");
p2.getNameAge();
System.out.println("直接拷贝");
p3.getNameAge();
}
}
class Address{
String country;
String street;
}
class Person extends Address implements Cloneable{
private String name;
private int age;
private Address add;
Person(String _name, int _age,String _country, String _street){
this.name = _name;
this.age = _age;
this.country = _country;
this.street = _street;
}
void setName(String _name){
this.name = _name;
}
void getNameAge(){
System.out.println(this.name+" "+this.age+" "+this.country+" "+this.street);
}
@Override
public Object clone(){
Person per = null;
try {
per = (Person)super.clone();
}catch (CloneNotSupportedException e){
}
return per;
}
}
