调用父类的方法从父类中调用超类的方法。如何从接口中调用相同的方法?

huangapple go评论89阅读模式
英文:

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();
    }
}

huangapple
  • 本文由 发表于 2023年5月14日 23:27:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定