如何在 Java Class 关联中实现 "include"
How do I implement an "include" in Java Class Association
我了解继承在 Java Class 关联中的作用,即扩展功能。但我不知道 include 是什么意思,我研究过,只能找到工具,但不确定这是否是 include 的意思。
这是 UML 设计。
StudentRecord (1) ---- 包括 ---- (1) Student
FullTimeStudent (1) ----继承----> (1) Student
这是技术上的区别:
第一个:StudentRecord (1) ---- 包括 ---- (1) Student
这意味着 StudentRecord 包含一个 Student 类型的成员变量,类似于
public class StudentRecord {
private Student student;
// and other member variables and functions
}
第二:FullTimeStudent(1)----继承---->(1)Student
这意味着 FullTimeStudent 是一个学生。喜欢:
public class FullTimeStudent extends Student {
// override stuff, add new members
}
看出区别了吗?一个说 "contains",而另一个说 "is a"。
即你可以这样写:
Student s = new FullTimeStudent();
和
StudentRecord sr = new StudentRecord(student);
// given you have such a constructor, or:
studentRecord.setStudent(s);
我了解继承在 Java Class 关联中的作用,即扩展功能。但我不知道 include 是什么意思,我研究过,只能找到工具,但不确定这是否是 include 的意思。
这是 UML 设计。
StudentRecord (1) ---- 包括 ---- (1) Student FullTimeStudent (1) ----继承----> (1) Student
这是技术上的区别:
第一个:StudentRecord (1) ---- 包括 ---- (1) Student 这意味着 StudentRecord 包含一个 Student 类型的成员变量,类似于
public class StudentRecord {
private Student student;
// and other member variables and functions
}
第二:FullTimeStudent(1)----继承---->(1)Student 这意味着 FullTimeStudent 是一个学生。喜欢:
public class FullTimeStudent extends Student {
// override stuff, add new members
}
看出区别了吗?一个说 "contains",而另一个说 "is a"。 即你可以这样写:
Student s = new FullTimeStudent();
和
StudentRecord sr = new StudentRecord(student);
// given you have such a constructor, or:
studentRecord.setStudent(s);