java - Multiple Inheritance using Anonymous class in Java
问题描述
I know that multiple inheritance is not supported in java. I wrote the code as shown below.
abstract class abc {
public abstract void print();
}
abstract class xyz {
public abstract void print();
}
public class Test {
public static void main(String[] args) {
abc obj1 = new abc() {
public void print() {
System.out.println("abc");
}
};
xyz obj2 = new xyz() {
public void print() {
System.out.println("xyz");
}
};
obj1.print();
obj2.print();
}
}
The output produced is:
abc
xyz
My question is, here I am using two abstract classes with a concrete class. Isn't that an implementation of multiple inheritance? And I intend to implement the code using classes, not interfaces.
解决方案
我正在使用两个抽象类和一个具体类。这不是多重继承的实现吗?
不,您正在main
方法中创建两个匿名类的实例。Test
该类与创建的两个匿名类实例中的任何一个之间没有继承关系。
abc
类与扩展它的匿名类之间存在单一继承关系。
xyz
在类和扩展它的匿名类之间还有另一个单一的继承关系。
推荐阅读
- html - 文本颜色中断 CSS 过渡
- java - Lowagie iText ClassNotFoundException
- rpm - 使用不同名称安装的 RPM
- jenkins - 如何在 Jenkins 中触发工作以在特定时间开始
- javascript - 是的,使用新的验证字段更新架构
- android - android 2-way data-binding kotlin mthod没有被调用
- c# - 使用原始请求标头 C# 在请求正文中发送参数和文件
- sql-server - T-SQL 将 XML 数据解析为单行
- ios - 即使正确设置,也隐藏在 ViewController 中的大标题
- intellij-idea - 如何通过右键单击在 Intellij 中运行 .sh?