--------- ---------
| | * * | |
| Person |________________| Company |
| | | | |
--------- | ---------
|
______|______
| |
| |
| Employment |
| |
|_____________|
我尝试在JAVA中设计一个带有关联类的类,但我不了解如何在实现中使用关联类。 但我理解它的好处。
这是我尝试做的事情:
public class person {
protected employment emp;
.
.
}
public class company {
protected employment emp;
.
.
}
public class employment {
protected person p[];
protected company c[];
.
.
}
关系和实施是否正确?
答案 0 :(得分:2)
不,多重性是对employment
的引用。
请注意,Java命名约定适用于以大写字母开头的类名。
Java数组是固定大小的,因此最好使用List
,这样您就可以轻松添加更多的工作。
通常,字段应为private
。
public class Person {
private List<Employment> employments;
.
.
}
public class Company {
private List<Employment> employments;
.
.
}
public class Employment {
private Person person;
private Company company;
.
.
}
答案 1 :(得分:0)
您如何看待:
public class person {
private employment emp;
.
.
}
public class employment {
private person p;
private company c;
.
.
}
public class company {
private employment[] emp;
.
.
}