Object as data attribute of class in Class diagram

2019-08-01 05:26发布

问题:

Is the following format wrong if I add a pointer to an object of a class, as data attribute of class in Class diagram in UML?

回答1:

could not find anything about using objects in class diagram, is underlining the object correct within the class attributes?

I think you may be mis-understanding classes, objects and attributes. Apologies if it's me doing the mis-understanding. So. Here's the short answer:

  • it's absolutely fine and normal for the type of an attribute to be a Class. In other words, you're not restricted to using primitive types like int, long, char etc.
  • The consequence is, as you say, that the values of those attributes at run time will themselves be objects. Specifically, instances of the classes Ability, Move and See.
  • More specifically, each instance of Agent (i.e. each Agent object) will hold references - or more precisely pointers - to 3 other objects: one instance each of Ability, Move and See.

So, assuming that's right, what you have is correct - except for the underlining.

Underlining an attribute or operation says it sits at the class level - not the instance level. It's the equivalent of static in java. Think of declaring constants in class scope, or constructors.

If I understand your model that's not what you want. You want each instance of Agent to hold (a pointer to) its own instances of Ability, Move and See. You don't want all the Agent objects to share the same 3 instances. Assuming so, you don't need the underline.

Hope I understood and that helps.



标签: class object uml