UML:如何在Java中实现关联类 [英] UML: how to implement Association class in Java
问题描述
我有这样的UML关联类。需要注意的是:水平行是实线,而垂直线是虚线
I have this UML Association class. Note that: horizontal line is a solid line and the vertical line is a dashed line.
--------- ---------
| |*(a) *(b)| |
| CLASS |________________| CLASS |
|STUDENT | | | COURSE |
--------- | ---------
|*(c)
______|______
| |
| |
| CLASS |
| TRANSCRIPT |
|_____________|
我理解这种关系,但我已经遇到了一些问题的时候实现这个UML为code。我可以落实code类学生
和类课程
的关系。这里是我的code:
I understand this relationship but I have met some problems when implement this UML to code. I can implement relation between class Student
and class Course
to code. Here is my code:
class Student {
Vector<Course> b;
}
class Course {
Vector<Student> a;
}
不过,在上课抄本
,我不明白这么多,如何使用这个类code。它是这两个阶级的财产学生
和课程
。所以,如果这是真的,那么在code将是:
But, at class Transcript
, I don't understand so much, how to use this class in code. Is it the property of both class Student
and Course
. So, if that's true then the code will be:
class Student {
Vector<Course> b;
Vector<Transcript> c;
}
class Course {
Vector<Student> a;
Vector<Transcript> c;
}
是真的吗?如果这是错误的,请教我如何实现此UML。
Is it true? If this is wrong, please teach me how to implement this UML.
感谢:)
推荐答案
首先,不要用向量,因为它不应再被使用超过10年的老类。使用一个设置
或列表
。
First of all, don't use Vector, as it's an old class that shouldn't be used anymore for more than 10 years. Use either a Set
or a List
.
如果在抄本
类包含学生参加课程的方式的信息(例如,它的订阅课程的日期),你可以实现像这样
If the Transcript
class contains information about the way a student attends a course (for example, the date of its subscription to the course), you could implement it like this:
class Student {
Set<Transcript> transcripts;
}
class Transcript {
Student student;
Course course;
Date subscriptionDate;
}
class Course {
Set<Transcript> transcripts;
}
这并不prevent您提供的学生一个方法,返回他的所有课程:&NBSP;
That doesn't prevent you from providing a method in Student that returns all his courses:
public Set<Course> getCourses() {
Set<Course> result = new HashSet<Course>();
for (Transcript transcript : transcripts) {
result.add(transcript.getCourse());
}
return result;
}
如果抄本
不包含任何信息,那么它可能在那里模型是如何使用这些类的数据库表,其中只有这样才能有一个多对一映射两个表之间一对多的关联是使用一个连接表抱着两个相关联的表的ID。
If Transcript
doesn't contain any information, then it's probably there to model how these classes would be mapped in database tables, where the only way to have a many-to-many association between two tables is to use a join table holding the IDs of the two associated tables.
这篇关于UML:如何在Java中实现关联类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!