当前位置: 技术问答>java相关
关于多重继承的问题?
来源: 互联网 发布时间:2015-06-12
本文导语: 请问怎样在JAVA中,用接口(Interface)实现多重继承呢?请举例说明。 如: class A 中有属性int num;方法String toString();和void print(); class B 中有属性byte bin;方法String toString();和void out(); class C 中有属性long l...
请问怎样在JAVA中,用接口(Interface)实现多重继承呢?请举例说明。
如:
class A 中有属性int num;方法String toString();和void print();
class B 中有属性byte bin;方法String toString();和void out();
class C 中有属性long len;方法String toString();和void read();
现在要求class C 继承 class A 和 class B?
如:
class A 中有属性int num;方法String toString();和void print();
class B 中有属性byte bin;方法String toString();和void out();
class C 中有属性long len;方法String toString();和void read();
现在要求class C 继承 class A 和 class B?
|
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;
.....
}
|
public class A
{
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
{
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
|
interface a {
String tostring()
void print()
}
interface b {
String toString();
void out
}
class ab implement a,b {
String toString(){
}
void print(){
}
void out(){
//do you self
}
}
String tostring()
void print()
}
interface b {
String toString();
void out
}
class ab implement a,b {
String toString(){
}
void print(){
}
void out(){
//do you self
}
}
|
在Java中实现多重继承,其实我的感觉是没有实现,所以使用Interface,来了个折衷的办法,达到了多重继承的效果.
想想看: 一个class 只能extends另一个class,但是它能implements多个interface.
想想看: 一个class 只能extends另一个class,但是它能implements多个interface.