java实现多重继承,如何在Java中实现多重继承

I'm working with a certain API library in Java. It has a base class A, as well as B and C which both extend A. B & C provide similar but distinct functionality, all three classes are in the library.

public abstract class A

{

virtual foo();

}

public class B extends A {}

public class C extends A {}

How do I get elements of A, B, and C in my class? If I use interfaces to implement the classes, there is a lot of duplicate code, and inner classes will not allow me to override existing methods so that the calling interfaces of A, B, and C are preserved.

How do I implement multiple inheritence in Java?

EDIT:

Thanks for edit George, its more clear now, forgot to mention one critical requirement: my classes must have A as a base so that they can be managed by platform API.

解决方案

To recap, you have:

class A

{

public void foo() {}

}

class B extends A

{

public specificAMethod() {}

}

class C extends A

{

public specificCMethod() {}

}

The above classes are in a library that you can't access or modify.

You want to get the behaviour of both B and C in a third class D, as if it were possible to write:

class D extends B, C

{

}

Right?

What about using B and C instead of inheriting? Do you really need inheritance? You want to call private B and C methods?

class D

{

private B b;

private C c;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值