简体   繁体   English

类图中的聚合关系?

[英]Aggregation relationship in class diagram?

I want to ask a question about aggregation relationship in the world of object oriented programming. 我想问一个关于面向对象编程世界中的聚合关系的问题。

Now i have a class that composed of 2 classes. 现在我有一个由2个班级组成的班级。

We know in aggregation relationship that the main class has a reference to the aggregate class as an attribute 我们知道在聚合关系中,主类将聚合类作为属性的引用

But the main class doesn't contain any attributes or methods except the aggregate classes. 但是主类除聚合类外不包含任何属性或方法。

Is this correct in the world of object oriented programming 这在面向对象编程的世界中正确吗?

for example: 例如:

main class called knight: 主班叫骑士:

the others are horse and sword. 其他的是马和剑。

So the knight class are composed of horse and sword. 因此,骑士阶层由马和剑组成。

this is an aggregation relationship... 这是一个聚合关系...

public class knight{
private horse h;
private sword s;

// no others attributes and methods
}

my question: is leaving the class knight without any attributes and methods as above correct ??? 我的问题:让骑士团没有上面正确的任何属性和方法吗?

Yes, it is correct, but useless. 是的,这是正确的,但没有用。

You have forgotten, that you have to expose somehow the knight's properties. 您已经忘记了,您必须以某种方式公开骑士的属性。 Fields or some methods working with them should be public. 与它们一起使用的字段或某些方法应该是公共的。 Or your knight is not a knight, but a hermit :-) 或者您的骑士不是骑士,而是隐士:-)

For example: 例如:

public boolean putSwordOn(Sword sword){}; //sets current sword
public boolean putSwordInSheath(){};  // uses sword
public boolean pullSwordOut(){};      // uses sword
public boolean attack(Creature target){};   // uses sword
public void kissSwordAndSwearToServe(Knight senior); // uses sword
public boolean climbHorse(Horse horse){};   // sets current horse
public boolean gallop(Point direction){};   // uses horse

Also, you have a problem in terminology. 另外,您在术语方面也有问题。 Such connection (has a) is shown by association . 这样的连接(具有a)由association And that is shown on the class diagram by a solid line, probably with points or a diamond or an arrowhead. 这在类图上以实线显示,可能带有点或菱形或箭头。 An association has some aggregation . 一个association有一些aggregation It is about how many instances of that type belong to that field and how do they belong. 它涉及该类型的实例属于该字段的数量以及它们如何属于。 aggregation can be composition , shared or none . aggregation可以是compositionsharednone We can say that this association has aggregation of type none , but to name "aggregation" something, that has none aggregation is obviously incorrect. 我们可以说,这个association已经aggregation型的none ,而是命名为“聚合”的东西,有none聚集明显不正确。 So, you are talking about association relationship. 因此,您正在谈论association关系。

Yes, why wouldn't it? 是的,为什么不呢?

There's absolutely no reason why that would not be correct. 绝对没有理由不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM