简体   繁体   English

Class 图 OOP

[英]Class Diagram OOP

If two classes are associated regularly, do they need to contain each other's objects as attributes?如果两个类有规律地关联,它们是否需要包含彼此的对象作为属性? For example, a Post class and a Comment class on a social media platform.例如,社交媒体平台上的一条帖子 class 和一条评论 class。 Should the Post class contain an array of comment objects as an attribute? Post class 是否应该包含一组评论对象作为属性? And should the Comment class contain a post object as an attribute?评论 class 是否应该包含帖子 object 作为属性?

This is a common mistake of redundant information.这是冗余信息的常见错误。 It does not need to be shown in the class diagram as a field because it is derived from the association.它不需要作为字段显示在 class 图中,因为它是从关联派生的。
This is a class diagram that covers what you have mentioned in its simplest form.这是一个 class 图,以最简单的形式涵盖了您提到的内容。 在此处输入图像描述

As you can see, objects for each class do not need to be added as fields.正如您所看到的,每个 class 的对象不需要添加为字段。

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

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