forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
56 lines (45 loc) · 1.25 KB
/
Student.java
File metadata and controls
56 lines (45 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @program JavaBooks
* @description: Student
* @author: mf
* @create: 2020/02/10 21:22
*/
package com.copy;
public class Student implements Cloneable{
// 对象的引用
private Subject subject;
private String name;
public Student(Subject s, String name) {
this.subject = s;
this.name = name;
}
public Subject getSubject() {
return subject;
}
public String getName() {
return name;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"subject=" + subject +
", name='" + name + '\'' +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
// 浅拷贝
// return super.clone();
// 深拷贝
Student student = new Student(new Subject(subject.getName()), name);
return student;
// 因为它是深拷贝,所以你需要创建拷贝类的一个对象。
// 因为在Student类中有对象引用,所以需要在Student类中实现Cloneable接口并且重写clone方法。
}
}