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

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

How to call a method of super class from parent class. How to call the same method from interface?

问题

以下是您要翻译的内容:

  1. 我正在学习Java并尝试重新创建一个场景其中一个类扩展了一个抽象类并实现了一个接口每个抽象类和接口都在其中定义了一个相同的方法现在我想从扩展和实现的类中调用两个父类的实现我尝试按照以下方式重新创建此场景但出现了错误不是封闭类A
  2. interface C {
  3. default void m1(){
  4. System.out.println("C");
  5. }
  6. }
  7. abstract class A{
  8. abstract void display();
  9. void m1(){
  10. System.out.println("A");
  11. }
  12. }
  13. class B extends A implements C{
  14. public void m1(){
  15. }
  16. void display() {
  17. C.super.m1();
  18. A.super.m1(); // error: not an enclosing class: A
  19. }
  20. }
  21. public class Main {
  22. public static void main(String[] args) {
  23. A obj = new B();
  24. obj.display();
  25. }
  26. }
英文:

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

  1. interface C {
  2. default void m1(){
  3. System.out.println("C");
  4. }
  5. }
  6. abstract class A{
  7. abstract void display();
  8. void m1(){
  9. System.out.println("A");
  10. }
  11. }
  12. class B extends A implements C{
  13. public void m1(){
  14. }
  15. void display() {
  16. C.super.m1();
  17. A.super.m1(); // error: not an enclosing class: A
  18. }
  19. }
  20. public class Main {
  21. public static void main(String[] args) {
  22. A obj = new B();
  23. obj.display();
  24. }
  25. }

答案1

得分: 1

以下是您要翻译的内容:

不需要显式命名超类以调用其方法,因为Java类只能扩展一个超类,所以MyClass.super.method()是明确的。事实上,您甚至不需要完全命名子类:super.method()也是明确的,因为Java采用单继承模型。

需要显式命名超接口以调用其方法,因为Java类可以实现多个接口,所以MyInterface.super.method()是必要的,以确定正在调用哪个接口。

  1. interface MyInterface {
  2. default void m1(){
  3. System.out.println("MyInterface");
  4. }
  5. }
  6. abstract class MyAbstractClass {
  7. public void m1(){
  8. System.out.println("MyAbstractClass");
  9. }
  10. }
  11. public class MyClass extends MyAbstractClass implements MyInterface {
  12. @Override
  13. public void m1() {
  14. super.m1(); // MyAbstractClass
  15. MyClass.super.m1(); // MyAbstractClass
  16. MyInterface.super.m1(); // MyInterface
  17. }
  18. public static void main(String... args) {
  19. new MyClass().m1();
  20. }
  21. }
英文:

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.

  1. interface MyInterface {
  2. default void m1(){
  3. System.out.println("MyInterface");
  4. }
  5. }
  6. abstract class MyAbstractClass {
  7. public void m1(){
  8. System.out.println("MyAbstractClass");
  9. }
  10. }
  11. public class MyClass extends MyAbstractClass implements MyInterface {
  12. @Override
  13. public void m1() {
  14. super.m1(); // MyAbstractClass
  15. MyClass.super.m1(); // MyAbstractClass
  16. MyInterface.super.m1(); // MyInterface
  17. }
  18. public static void main(String... args) {
  19. new MyClass().m1();
  20. }
  21. }

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:

确定