How to draw UML diagrams passing parameters through super constructor

时间:2019-04-17 01:09:37

标签: uml

I have a superclass called A and a subclass called B that inherits from A. The superclass's constructor looks like this:

A(String name, char displayChar, int hitPoints, Behaviour behaviour) 

{
 this.name = name;
 this.displayChar = displayChar;
 this.hitPoints = hitPoints
 addBehaviour(behaviour);
}

A has attributes of name, displayChar, hitPoints, behaviour and has a method that calls addBehaviour which adds the behaviour to the object.

The subclass, B's constructor looks like this:

B(String name) {super(name, char 'b', 10, new WalkBehaviour()); }

Now my question is, does subclass B have an attribute of WalkBehaviour?

How would the UML diagram look like for this scenario? I know B inherits from A and A has Behaviour but does B has WalkBehaviour in this case? Since B doesn't have an instance variable of type WalkBehaviour in its class but only passes WalkBehaviour through its superclass's constructor.

1 个答案:

答案 0 :(得分:2)

  

子类B是否具有WalkBehaviour的属性?

不。没有声明。该超类将对该新对象执行某些操作,但显然将其隐藏在其实现的迷雾中。

继承不涉及创建多个对象。您的B实例只是一个实例,它确实具有其父类 like 的属性和操作。

enter image description here

因此,在SD中,您只会看到B的一条生命线:

enter image description here

您可以看到B实例将向超类的构造函数发出自调用。

注意:正如@AxelScheithauer在评论中指出的那样,超类将调用addBehavior,它可以(但不能)显示在SD中:

enter image description here