public class Student2 implements Cloneable {
private String name;
private int age;
private Professor2 professor;
public Student2(String name?? int age?? Professor2 professor) {
this.name = name;
this.age = age;
this.professor = professor;
}
@Override
public String toString() {
return "Student2 [name=" + name + "?? age=" + age + "?? Professor2="
+ professor.toString() + "]";
}
@Override
public Object clone() {
Student2 o = null;
try {
// ?????????Object?е?clone???????????????????????Object?е?clone()
// ???????????????????ж??????????????????????????????????洢????С?
o = (Student2) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
o.professor = (Professor2) professor.clone();
return o;
}
public static void main(String[] args) {
Professor2 professor = new Professor2("beijing"?? 12.0);
Student2 s1 = new Student2("zhangsan"?? 18?? professor);
Student2 s2 = (Student2) s1.clone();
System.out.println(s1);
System.out.println(s2);
System.out.println("**************************************");
s2.name = "lisi";
s2.age = 20;
s2.professor.setAddress("shanghai");
s2.professor.setSalary(230.0);
System.out.println(s1);
System.out.println(s2);
}
}
Student2 [name=zhangsan?? age=18?? Professor2=Professor2 [address=beijing?? salary=12.0]]
Student2 [name=zhangsan?? age=18?? Professor2=Professor2 [address=beijing?? salary=12.0]]
**************************************
Student2 [name=zhangsan?? age=18?? Professor2=Professor2 [address=beijing?? salary=12.0]]
Student2 [name=lisi?? age=20?? Professor2=Professor2 [address=shanghai?? salary=230.0]]
???????clone?????????????
public interface Prototype {
public Prototype clone();
public void setName(String name);
public String getName();
}
<span style="font-size:14px;">public class ConcretePrototypeA implements Prototype {
private String name;
public ConcretePrototypeA() {
}
public ConcretePrototypeA(String name) {
this.name = name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public Prototype clone() {
ConcretePrototypeA prototype = new ConcretePrototypeA();
prototype.setName(this.name);
return prototype;
}
@Override
public String toString() {
return "ConcretePrototypeA [name=" + name + "]";
}
}</span>
public class TestPrototype {
public static void main(String[] args) {
ConcretePrototypeA prototypeA = new ConcretePrototypeA("jimmy");
ConcretePrototypeA prototypeA2 = (ConcretePrototypeA) prototypeA
.clone();
System.out.println(prototypeA);
System.out.println(prototypeA2);
}
}
ConcretePrototypeA [name=jimmy]
ConcretePrototypeA [name=jimmy]
????6????ó???
??????????????????????????????????????????????????????????????????????????????????????????????? ???????????£???????????????????????????????и???Ч????????????????????????????????????
????1>????????????????????????????????磬??????????
????2>?????????????????????????е???????????
????3>??????????????????м????????????е???????
????4>???????????????????????????????ú????????????????????????Щ??
????????????????????????????????????????Щ??Ч????????????????????????????????????????????????????κθ???????????????????е???????????????????????????????????????????????????????????????????
????7?????????????
????1>java?и???????????????????????????????????????????????????????????????е??????????????????????????????????
????2>???clone??????????????????????????????????????????????????????????????????
????8?????????"?????"??"????"
?????????: ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????????: ??????????????????????????????????????????????????????????(??Щ???????????????????????????????????????????е???Щ??????????)??????????????????????????????????????飬????????????????????????????????
????<span style="font-size:14px"></span>