[英]Aggregation relationship in class diagram?
我想问一个关于面向对象编程世界中的聚合关系的问题。
现在我有一个由2个班级组成的班级。
我们知道在聚合关系中,主类将聚合类作为属性的引用
但是主类除聚合类外不包含任何属性或方法。
这在面向对象编程的世界中正确吗?
例如:
主班叫骑士:
其他的是马和剑。
因此,骑士阶层由马和剑组成。
这是一个聚合关系...
public class knight{
private horse h;
private sword s;
// no others attributes and methods
}
我的问题:让骑士团没有上面正确的任何属性和方法吗?
是的,这是正确的,但没有用。
您已经忘记了,您必须以某种方式公开骑士的属性。 与它们一起使用的字段或某些方法应该是公共的。 或者您的骑士不是骑士,而是隐士:-)
例如:
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
另外,您在术语方面也有问题。 这样的连接(具有a)由association
。 这在类图上以实线显示,可能带有点或菱形或箭头。 一个association
有一些aggregation
。 它涉及该类型的实例属于该字段的数量以及它们如何属于。 aggregation
可以是composition
, shared
或none
。 我们可以说,这个association
已经aggregation
型的none
,而是命名为“聚合”的东西,有none
聚集明显不正确。 因此,您正在谈论association
关系。
是的,为什么不呢?
绝对没有理由不正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.