英文:
How to call a method of super class from parent class. How to call the same method from interface?
问题
以下是您要翻译的内容:
我正在学习Java,并尝试重新创建一个场景,其中一个类扩展了一个抽象类并实现了一个接口。每个抽象类和接口都在其中定义了一个相同的方法。现在,我想从扩展和实现的类中调用两个父类的实现。我尝试按照以下方式重新创建此场景,但出现了错误:不是封闭类:A
interface C {
default void m1(){
System.out.println("C");
}
}
abstract class A{
abstract void display();
void m1(){
System.out.println("A");
}
}
class B extends A implements C{
public void m1(){
}
void display() {
C.super.m1();
A.super.m1(); // error: not an enclosing class: A
}
}
public class Main {
public static void main(String[] args) {
A obj = new B();
obj.display();
}
}
英文:
I am learning java and I am trying to recreate a scenario where A class is extending an abstract class and also implementing an interface. Each abstract class and interface have a same method defined in them. Now from the class which is extending and implementing, I want to call the implementations of both the parents. I tried to recreate this scenario as below but it gave me the error: not an enclosing class: A
interface C {
default void m1(){
System.out.println("C");
}
}
abstract class A{
abstract void display();
void m1(){
System.out.println("A");
}
}
class B extends A implements C{
public void m1(){
}
void display() {
C.super.m1();
A.super.m1(); // error: not an enclosing class: A
}
}
public class Main {
public static void main(String[] args) {
A obj = new B();
obj.display();
}
}
答案1
得分: 1
以下是您要翻译的内容:
不需要显式命名超类以调用其方法,因为Java类只能扩展一个超类,所以MyClass.super.method()
是明确的。事实上,您甚至不需要完全命名子类:super.method()
也是明确的,因为Java采用单继承模型。
需要显式命名超接口以调用其方法,因为Java类可以实现多个接口,所以MyInterface.super.method()
是必要的,以确定正在调用哪个接口。
interface MyInterface {
default void m1(){
System.out.println("MyInterface");
}
}
abstract class MyAbstractClass {
public void m1(){
System.out.println("MyAbstractClass");
}
}
public class MyClass extends MyAbstractClass implements MyInterface {
@Override
public void m1() {
super.m1(); // MyAbstractClass
MyClass.super.m1(); // MyAbstractClass
MyInterface.super.m1(); // MyInterface
}
public static void main(String... args) {
new MyClass().m1();
}
}
英文:
You don't need to explicitly name a superclass to invoke its methods, because a Java class can only extend one superclass, so MyClass.super.method()
is unambiguous. In fact, you needn't even name the child class at all: super.method()
is also unambiguous due to Java's single inheritance model.
You do need to explicitly name a super-interface to invoke its methods, because a Java class can implement multiple interfaces, so MyInterface.super.method()
is necessary to identify which interface is being invoked.
interface MyInterface {
default void m1(){
System.out.println("MyInterface");
}
}
abstract class MyAbstractClass {
public void m1(){
System.out.println("MyAbstractClass");
}
}
public class MyClass extends MyAbstractClass implements MyInterface {
@Override
public void m1() {
super.m1(); // MyAbstractClass
MyClass.super.m1(); // MyAbstractClass
MyInterface.super.m1(); // MyInterface
}
public static void main(String... args) {
new MyClass().m1();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论