在 Java 中显式调用默认方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19976487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:01:03  来源:igfitidea点击:

Explicitly calling a default method in Java

javainheritanceinterfacejava-8default-method

提问by GOTO 0

Java 8 introduces default methodsto provide the ability to extend interfaces without the need to modify existing implementations.

Java 8 引入了默认方法来提供无需修改现有实现即可扩展接口的能力。

I wonder if it's possible to explicitly invoke the default implementation of a method when that method has been overridden or is not available because of conflicting default implementations in different interfaces.

我想知道当该方法已被覆盖或由于不同接口中的默认实现冲突而无法使用时,是否可以显式调用该方法的默认实现。

interface A {
    default void foo() {
        System.out.println("A.foo");
    }
}

class B implements A {
    @Override
    public void foo() {
        System.out.println("B.foo");
    }
    public void afoo() {
        // how to invoke A.foo() here?
    }
}

Considering the code above, how would you call A.foo()from a method of class B?

考虑到上面的代码,您将如何A.foo()从 B 类的方法中调用?

采纳答案by Richard Tingle

As per this articleyou access default method in interface Ausing

根据本文,您可以A使用接口访问默认方法

A.super.foo();

This could be used as follows (assuming interfaces Aand Cboth have default methods foo())

这可用于如下(假设接口AC两个有默认的方法foo()

public class ChildClass implements A, C {
    @Override    
    public void foo() {
       //you could completely override the default implementations
       doSomethingElse();
       //or manage conflicts between the same method foo() in both A and C
       A.super.foo();
    }
    public void bah() {
       A.super.foo(); //original foo() from A accessed
       C.super.foo(); //original foo() from C accessed
    }
}

Aand Ccan both have .foo()methods and the specific default implementation can be chosen or you can use one (or both) as part of your new foo()method. You can also use the same syntax to access the default versions in other methods in your implementing class.

A并且C都可以有.foo()方法并且可以选择特定的默认实现,或者您可以使用一个(或两个)作为新foo()方法的一部分。您还可以使用相同的语法来访问实现类中其他方法中的默认版本。

Formal description of the method invocation syntax can be found in the chapter 15 of the JLS.

方法调用语法的正式描述可以在 JLS第 15 章中找到。

回答by Masudul

You don't need to override the default method of an interface. Just call it like the following:

您不需要覆盖接口的默认方法。只需像下面这样调用它:

public class B implements A {

    @Override
    public void foo() {
        System.out.println("B.foo");
    }

    public void afoo() {
        A.super.foo();
    }

    public static void main(String[] args) {
       B b=new B();
       b.afoo();
    }
}

Output:

输出:

A.foo

回答by Abhijith Nagarajan

The code below should work.

下面的代码应该可以工作。

public class B implements A {
    @Override
    public void foo() {
        System.out.println("B.foo");
    }

    void aFoo() {
        A.super.foo();
    }

    public static void main(String[] args) {
        B b = new B();
        b.foo();
        b.aFoo();
    }
}

interface A {
    default void foo() {
        System.out.println("A.foo");
    }
}

Output:

输出:

B.foo
A.foo

回答by Dávid Horváth

This answer is written mainly for users who are coming from question 45047550which is closed.

此答案主要是为来自已关闭的问题45047550 的用户编写的。

Java 8 interfaces introduce some aspects of multiple inheritance. Default methods has an implemented function body. To call a method from the super class you can use the keyword super, but if you want to make this with a super interface it's required to name it explicitly.

Java 8 接口引入了多重继承的某些方面。默认方法具有已实现的函数体。要从超类调用方法,您可以使用关键字super,但是如果您想使用超接口来实现它,则需要显式命名它。

class ParentClass {
    public void hello() {
        System.out.println("Hello ParentClass!");
    }
}

interface InterfaceFoo {
    default public void hello() {
        System.out.println("Hello InterfaceFoo!");
    }
}

interface InterfaceBar {
    default public void hello() {
        System.out.println("Hello InterfaceBar!");
    }
}

public class Example extends ParentClass implements InterfaceFoo, InterfaceBar {
    public void hello() {
        super.hello(); // (note: ParentClass.super is wrong!)
        InterfaceFoo.super.hello();
        InterfaceBar.super.hello();
    }

    public static void main(String[] args) {
        new Example().hello();
    }
}

Output:

输出:

Hello ParentClass!
Hello InterfaceFoo!
Hello InterfaceBar!

家长班您好!
你好InterfaceFoo!
你好接口栏!