Java - 用不同的方法实现两个接口
Java - Implement two interfaces with different methods
假设有两个接口,它们有不同的方法。我在 class 中实现这两个接口并使用这两个接口的方法。
interface A { void show1(); }
interface B { void show(); }
public class test implements A, B{
@Override
void show1(){
System.out.println("show1");
}
@Override
void show(){
System.out.println("show");
}
}
多重继承的定义:
多重继承是一些面向对象的计算机编程语言的一个特征,其中一个对象或 class 可以从多个父对象或父对象 class.
问题
我可以说我在程序中所做的是多重继承吗?如果不是,为什么?
备注
我正在使用 Java 7.
是的,你在你的程序中所做的就是多重继承。
The Java programming language supports multiple inheritance of type, which is the ability of a class to implement more than one interface. An object can have multiple types: the type of its own class and the types of all the interfaces that the class implements.
https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html
我觉得实现多个接口不是多重继承
您的对象可能有多个接口的实现。例如,您的 Car 对象可能具有 BreakStatus、FuelTank 接口,但它没有将 Car 定义为sub-type/derived-type BreakStatus、FuelTank。
继承是:一个对象 extends
其他对象(父)属性 and/or 行为。
Java 对象不支持多重继承(扩展多个对象)类似 class Child extends Parent1, Parent2 {}
N.B. Java接口支持扩展多个接口,例如interface I3 extends I1, I2 {}
是的,您正在使用多个接口实现多重继承,您可以在其中继承两个不同接口的定义。
在java中,多重继承是使用Multiple接口实现的。默认情况下,接口中的方法始终是抽象的,这不允许它们在接口本身中给出它们的实现(或方法定义)。
没有。因为接口的主要目的是抽象,这意味着对外部隐藏实现细节。因此,如果您实现一个或多个接口,则该关系是 like a
关系而不是 is a
关系。
使用接口的另一个目的是 类 之间的松散耦合。 (抽象的副作用)。
假设有两个接口,它们有不同的方法。我在 class 中实现这两个接口并使用这两个接口的方法。
interface A { void show1(); }
interface B { void show(); }
public class test implements A, B{
@Override
void show1(){
System.out.println("show1");
}
@Override
void show(){
System.out.println("show");
}
}
多重继承的定义:
多重继承是一些面向对象的计算机编程语言的一个特征,其中一个对象或 class 可以从多个父对象或父对象 class.
问题
我可以说我在程序中所做的是多重继承吗?如果不是,为什么?
备注
我正在使用 Java 7.
是的,你在你的程序中所做的就是多重继承。
The Java programming language supports multiple inheritance of type, which is the ability of a class to implement more than one interface. An object can have multiple types: the type of its own class and the types of all the interfaces that the class implements.
https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html
我觉得实现多个接口不是多重继承
您的对象可能有多个接口的实现。例如,您的 Car 对象可能具有 BreakStatus、FuelTank 接口,但它没有将 Car 定义为sub-type/derived-type BreakStatus、FuelTank。
继承是:一个对象 extends
其他对象(父)属性 and/or 行为。
Java 对象不支持多重继承(扩展多个对象)类似 class Child extends Parent1, Parent2 {}
N.B. Java接口支持扩展多个接口,例如interface I3 extends I1, I2 {}
是的,您正在使用多个接口实现多重继承,您可以在其中继承两个不同接口的定义。
在java中,多重继承是使用Multiple接口实现的。默认情况下,接口中的方法始终是抽象的,这不允许它们在接口本身中给出它们的实现(或方法定义)。
没有。因为接口的主要目的是抽象,这意味着对外部隐藏实现细节。因此,如果您实现一个或多个接口,则该关系是 like a
关系而不是 is a
关系。
使用接口的另一个目的是 类 之间的松散耦合。 (抽象的副作用)。