本文介绍了IS-A和HAS-A关系在OOPS中同时出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,每个类都是Object的子级.因此,是一个IS-A类对象.现在,如果它包含对象类型的数据成员(在这种情况下实际上是任何类型),那么它将是HAS-A关系.与上述陈述有关,我有以下疑问.1.这种关系是否有名字,IS-A和HAS-A在一起?2.是否有现实世界的例子?3.从OOPS的角度来看,可以建立这种关系还是应该避免这种关系?

In Java every class is a child of Object. So a class IS-A Object. Now if it contains a data member of Object type (actually any type in this case), then it would be HAS-A relationship.Related to above statement I have following doubts.1. Is there a name for this relationship, IS-A and HAS-A coming together?2. Are there any real world example for it?3. From OOPS perspective, is it okay to have this relationship or should be avoided?

推荐答案

  1. 这种关系是否有名字,IS-A和HAS-A在一起?

否.

  1. 有没有现实世界的例子?
List<Set> aListThatContainsSets

列表和集合都是集合.在此示例中,列表具有一堆集合.

List and Set are both Collections. In this example a List has a bunch of Sets.

  1. 从OOPS的角度来看,可以保持这种关系还是应该避免这种关系?

是的,很好.您甚至可以自己拥有HAS-A.

Yes its fine. You can even HAS-A yourself.

public class MyClass{

     MyClass me;

}

这篇关于IS-A和HAS-A关系在OOPS中同时出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 02:59