编程语言
首页 > 编程语言> > 在Java代码中使用UML三元协会

在Java代码中使用UML三元协会

作者:互联网

我目前在Code中实现三元关联时遇到了一些麻烦.
我得到二进制的,
但我不确定三元协会.

这是大学里的典型情景.

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

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

下面的UML类图中显示的这三个类之间的关系以及多重性都存在

enter image description here

我已经在互联网上阅读了关于这一点的不同来源,并且找不到解决方案

如何实现这三个类之间的关联?要么,
一般来说,在类之间实现关联的可能方法是什么(在java中)?

解决方法:

一如既往,这取决于.

实现关联类的常用方法是使用关联数组.在您的示例中,您(最终)有一个Lecturer / Subject的组合来访问学生列表.如果您的要求不同,您可以在提供教师时返回主题/学生列表.

使用数据库时,您将在表格教学中获得讲师/主题/学生的主键.这允许个人选择,如上所述.

我的一些伪代码:

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,oop,uml,class-diagram,ooad
来源: https://codeday.me/bug/20190701/1346216.html