gpt4 book ai didi

java - 用java代码实现UML三元关联

转载 作者:搜寻专家 更新时间:2023-11-01 03:15:54 25 4
gpt4 key购买 nike

我目前在代码中实现三元关联时遇到一些问题。我得到二进制的,但我不确定三元协会。

这是大学里的典型场景。

讲师可以向一个或多个学生教授一门学科
学生只能从一位讲师那里教授一门科目
讲师只能教一个学生一门学科

这三个类之间存在三元关联。

这三个类之间的关系如下UML类图所示,还有多重性

enter image description here

我已经在互联网上阅读了有关此问题的不同来源,但找不到解决方案

如何实现这三个类之间的关联?或者,一般而言,实现类之间关联的可能方法有哪些(在 java 中)?

最佳答案

一如既往,这取决于。

实现关联类的常用方法是使用关联数组。在您的示例中,您(最终)将拥有 Lecturer/Subject 的组合来访问 Student 的列表。如果您的要求不同,您可以在提供 Teacher 时返回 Subject/Student 的列表。

当使用数据库时,您将在表 Teaching 中拥有 Lecturer/Subject/Student 的主键>。这允许像上面提到的那样进行单独选择。

一些伪代码:

class Teaching {
private Hash lectRef; // assoc. array

public void addTeaching(Lecturer lect, Student stud, Subject subj) {
if lectRef[lect.hash] == None { lectRef[lect.hash] = []; }
// if none, default an empty array here
// lect.hash is a unique hash code for the Lecturer object

lectRef[lect.hash].append((stud, subj);
// tuple of student/subject referenced

// if you need other fast results (e.g. subjects per student or the like) you need to hash them here too
}

public [(Stud, Subj)] studSubj (Lecturer lect) {
return lectRef[lect.hash];
// returns the array of student/subject tuples
}

// add other result operations like subjects per student as needed
}

关于java - 用java代码实现UML三元关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52812860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com