Java:多重继承的替代方案 [英] Java: Alternative to Multiple inheritance

查看:189
本文介绍了Java:多重继承的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个A类,必须扩展B类和C类,它们都是由第三方提供的,我没有它们的控制权。由于Java不支持多重继承。使用接口在我的情况下没有帮助,因为我想继承基类的实现,我不想将它们的代码复制到我的类,或者我需要知道它们即将发生的所有更改。 / p>

满足这种需求的最佳方法是什么?



所有建议都将非常感谢!

解决方案

  A 

类B2延伸B

继承B的东西

类C2扩展C

继承C的东西

B2 b2 = new B2();
C2 c2 = new C2();
//或使用匿名类

A可以访问B2和C2中的所有内容,
因此A可以访问B和C的可继承资产。


I have a class A that has to extend class B and C which are both provided by 3rd party and I don't have a control of them. Since Java doesn't support multiple inheritance. Using interface wouldn't help in my case since I do want to inherit the base classes' implementation and I don't want to copy their code over to my class, or I'll need to be aware of all their coming changes.

What would be the best way to accomplish this need?

All suggestion will be much appreciated!

解决方案

class A

    class B2 extends B

        inherit B's stuff

    class C2 extends C

        inherit C's stuff

    B2 b2 = new B2();
    C2 c2 = new C2();
    // or use anonymous classes

    A has access to everything in B2 and C2,
    therefore A has access to B and C's inheritable assets.

这篇关于Java:多重继承的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆