Java中多重继承的替代方案

发布于 2024-07-25 04:01:14 字数 793 浏览 5 评论 0原文

我已经创建了两个 bean

class BackPageBean{
   String backPage = null;
  :
  :
  :
}


class InformationMessageBean{
   String informationMessage = null;
  :
  :
  :
}

现在,如果一个类是 backpage 感知的,那么它将扩展 backPageBean,或者如果它需要显示某种消息,那么它将扩展 InformationMessageBean。

class BackPageAware extends backPageBean{
}

class InfoMessAware extends InformationMessageBean{
}



    someFunction () {
       if ( theObject instanceOf backPageBean ) {
              prepareTheBackPage ( theObject.getBackPage() );
       }

       if ( theObject instanceOf InformationMessageBean ) {
              showtheInformation ( theObject.getMessage() );
       }

   }

现在的问题是,如果我想要一个既是 BackPageAware 又是 InformationAware 的 bean,那么,由于我们没有多重继承,应该采取什么方法呢?

I have created two beans

class BackPageBean{
   String backPage = null;
  :
  :
  :
}


class InformationMessageBean{
   String informationMessage = null;
  :
  :
  :
}

Now, if a class is backpage aware then it will extend backPageBean, or if it need to show some kind of message then it extends InformationMessageBean.

class BackPageAware extends backPageBean{
}

class InfoMessAware extends InformationMessageBean{
}



    someFunction () {
       if ( theObject instanceOf backPageBean ) {
              prepareTheBackPage ( theObject.getBackPage() );
       }

       if ( theObject instanceOf InformationMessageBean ) {
              showtheInformation ( theObject.getMessage() );
       }

   }

Now the problem is, if i want to have a bean which is both BackPageAware as well as InformationAware then, as we don't have multiple inheritance, what should be the approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

留一抹残留的笑 2024-08-01 04:01:14

只是为了澄清我的评论。

就像达斯·埃鲁说的,你创造了
两个接口和两个默认值
实施。 当你有一颗豆子时
这需要你的两种行为
让该类实现这两个
接口,但您也可以创建
默认变量
实施。 这样你还是
不需要重复任何代码。

    interface InfoMessAware {

         String getMessage();
    }

    interface BackPageAware {

         String getBackPage();
    }

class DefaultInfoMessAware {
         String getMessage() {
             return "message";
         }
}

class DefaultBackPageAware {
         String getBackPage() {
             return "backPage";
         }
}

    class MyBean implements InfoMessAware, BackPageAware {
         private InfoMessAware messAware = new DefaultInfoMessAware();
         private BackPageAware backPageAware = new DefaultBackPageAware();

         String getMessage() {
             return messAware.getMessage();
         }

         String getBackPage() {
             return backPageAware.getBackPage();
         }
    }

Just to clarify my comment.

Just like Darth Eru says you create
the two interfaces and the two default
implementations. When you have a bean
that needs both of the behaviours you
have that class implement the two
interfaces but you also create
variables of the default
implementations. This way you still
dont need to duplicate any code.

    interface InfoMessAware {

         String getMessage();
    }

    interface BackPageAware {

         String getBackPage();
    }

class DefaultInfoMessAware {
         String getMessage() {
             return "message";
         }
}

class DefaultBackPageAware {
         String getBackPage() {
             return "backPage";
         }
}

    class MyBean implements InfoMessAware, BackPageAware {
         private InfoMessAware messAware = new DefaultInfoMessAware();
         private BackPageAware backPageAware = new DefaultBackPageAware();

         String getMessage() {
             return messAware.getMessage();
         }

         String getBackPage() {
             return backPageAware.getBackPage();
         }
    }
风尘浪孓 2024-08-01 04:01:14

使用接口:

interface InfoMessAware {

     String getMessage();
}

interface BackPageAware {

     String getBackPage();
}

class MyBean implements InfoMessAware, BackPageAware {

     String getMessage() {
         return "message";
     }

     String getBackPage() {
         return "backPage";
     }
}

然后用标准方法调用替换 instanceof

use interfaces:

interface InfoMessAware {

     String getMessage();
}

interface BackPageAware {

     String getBackPage();
}

class MyBean implements InfoMessAware, BackPageAware {

     String getMessage() {
         return "message";
     }

     String getBackPage() {
         return "backPage";
     }
}

then replace instanceof with standard method calls.

执手闯天涯 2024-08-01 04:01:14

您所描述的问题需要使用组合,而不是继承。 BackPageAware 类意味着它知道该类/功能。 继承意味着它一个BackPage。 您描述了一种HAS关系。

正如已经多次说过的那样,使用接口来定义用于检索对象拥有信息的契约。

The problem you are describing begs the usage of composition, not inheritance. The class being BackPageAware means it knows about that class/functionality. Inheritance means it IS a BackPage. You have described a HAS A relationship.

As has been said many times now, use interfaces to define the contracts for retrieving the information that the object HAS.

救赎№ 2024-08-01 04:01:14

您的两个原始类应该是接口,每个类都有一个方法,用于检索实现应返回的信息。

public interface BackPageBean {
   public String getBackPage();
}


public interface InformationMessageBean {
   public String getInformationMessage();   
}

如果您希望一个类同时实现 BackPageBean 和 InformationMessageBean,您只需这样做:

public MyBean implements BackPageBean, InformationMessageBean {

  public String getBackPage() {
    return null;
  }

  public String getInformationMessage() {
    return null;
  }
}

一般来说,您应该尽可能避免扩展非抽象类,这会导致各种问题。 相反,在涉及具体类的情况下,尝试使用组合而不是继承,否则,尝试坚持使用接口和偶尔的抽象类。

Your two original classes should be Interfaces, each with a method on them which retrieves the information that implementations should return.

public interface BackPageBean {
   public String getBackPage();
}


public interface InformationMessageBean {
   public String getInformationMessage();   
}

If you want a class to implement both BackPageBean and InformationMessageBean you simply do this:

public MyBean implements BackPageBean, InformationMessageBean {

  public String getBackPage() {
    return null;
  }

  public String getInformationMessage() {
    return null;
  }
}

Generally speaking, you should avoid extending non-abstract classes whenever you can, it leads to all sorts of problems. Instead, try using composition instead of inheritance where concrete classes are involved, and otherwise, try and stick to interfaces and the occasional Abstract class.

空城缀染半城烟沙 2024-08-01 04:01:14

正如其他答案中所讨论的,可以使用接口和组合来模拟多重继承,但代价是必须编写大量样板代码。 然而,有许多项目可以在编译时(通过预处理器)或运行时自动执行此操作,例如 jmixin

As discussed in other answers multiple inheritance can be simulated using interfaces and composition, at the expense of having to write a lot of boilerplate code. There are however a number of projects around that can automate this either at compile time (via a pre-processor) or at runtime eg jmixin.

╭ゆ眷念 2024-08-01 04:01:14

您可以将 BackPageAwareInformationAware 作为接口而不是类。 如果您引用的“bean”是 BackPageAwareInformationAware,这使您能够从两个接口获取消息。

public class Name implements BackPageAware, InformationAware {}

You can have BackPageAware and InformationAware as interfaces rather than classes. This enables you to then fetch the messages from both interfaces in the case that the 'bean' you are referring to is of BackPageAware and InformationAware.

public class Name implements BackPageAware, InformationAware {}
从﹋此江山别 2024-08-01 04:01:14

将 BackPageAware 和 InformationAware 声明为接口,创建实现这些接口的抽象类以提供默认功能。 任何只需两个接口之一的类都可以继承相应的抽象类。 需要两者的类可以继承其中一个抽象类并实现另一个接口,或者实现两个接口,等等。 如果没有默认功能,那么您甚至不需要抽象类。

Declare BackPageAware and InformationAware as interfaces, create abstract classes that implement those interfaces to provide the default functionality. Any class that needs to be only one of the two interfaces can inherit the corresponding abstract class. A class that needs to be both can inherit one of the abstract classes and implement the other interface, or implement both interfaces, whatever. If there is no default functionality, then you don't even need the abstract classes.

疑心病 2024-08-01 04:01:14

您不能扩展多个类,但可以实现多个接口。 请记住,当您将接口应用于类时,该类的对象与该接口具有 IS-A 关系。

You can't extend multiple classes but you can implement multiple interfaces. Remember that when you apply an interface to a class, the object of the class has a IS-A relationship with the interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文