typical misunderstanding. interface cannot do multi-inheritance. it should be called muti-subtyping.
interface does not have any implementation, what can you inherit anyway?There is a recommended replacement for multi-inheritance, but that's not interface. Use aggregation.
It's like
class C
{
A a;
B b;
.....
}
interface does not have any implementation, what can you inherit anyway?There is a recommended replacement for multi-inheritance, but that's not interface. Use aggregation.
It's like
class C
{
A a;
B b;
.....
}
我能理解你所说的意思,但我只是想问利用接口,联合等方式,
在JAVA中怎样实现类似于C++的多重继承。
String tostring()
void print()
}
interface b {
String toString();
void out
}
class ab implement a,b {
String toString(){
}
void print(){ }
void out(){
//do you self
}
}
若有高手能用其它方式实现的话,请留言给我,我另开贴放分。
谢谢!
不好意思,更新太慢,没看到你的贴,我想请教你一下:
在class a 和class b 中的属性 num 和 bin 怎样处理?
Even in C++, you should avoid inheritance and use aggregate.
{
public String toString()
{
String Cstring = " A toString() Method";
System.out.println(Cstring);
return Cstring;
}
public void Print()
{
System.out.println(" A print() Method");
}
}public interface B
{
String toString();
void Out();
}public class C extends A implements B
{
public String toString()
{
String Cstring = " C toSting() Method";
System.out.println(Cstring);
return Cstring;
}
public void Out()
{
System.out.println(" C Out() Method");
}
static public void main(String argv[])
{
C c_Object = new C();
c_Object.toString();
c_Object.Out();
c_Object.Print();
return ;
}
}输出结果:C toString() Method
C Out() Method
A Pring() Method
class B implements IB{void b();}
class AB implements IA, IB
{
public void a(){_a.a();}
public void b(){_b.b();}
private IA _a;
private IB _b;
}
is there anything we can't solve here?
to all:不知各位高手是否还有其它的方法,请指教!
这个贴今晚才会封贴,请大家多多参加讨论。
想想看: 一个class 只能extends另一个class,但是它能implements多个interface.
can you give an example where you think aggregation cannot solve it?
我在此谢谢各位参加讨论,我现在就去放分(希望可以),
我还是那句话,若有高手能用其它方式实现的话,请留言给我,我另开贴放分。