313

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

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

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?
    }
}

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

4

6 回答 6

427

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

A.super.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
    }
}

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

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

于 2013-11-14T11:35:37.050 回答
19

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

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

输出:

B.foo
A.foo
于 2013-11-14T11:40:14.073 回答
16

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

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

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

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

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

public class Example extends ParentClass implements InterfaceFoo, InterfaceBar {
    public void hello() {
        super.hello(); // (note: ParentClass.super could not be used)
        InterfaceFoo.super.hello();
        InterfaceBar.super.hello();
    }
    
    public static void main(String[] args) {
        new Example().hello();
    }
}

输出:

家长班您好!
你好InterfaceFoo!
界面栏你好!

于 2018-05-20T17:49:50.847 回答
3

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

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

输出:

于 2013-11-14T11:33:05.400 回答
0

是否要覆盖接口的默认方法取决于您的选择。因为 default 类似于类的实例方法,可以直接在实现类对象上调用。(简而言之,接口的默认方法由实现类继承)

于 2021-01-02T07:12:43.987 回答
0

考虑以下示例:

interface I{
    default void print(){
    System.out.println("Interface");
    }
}

abstract class Abs{
    public void print(){
        System.out.println("Abstract");
    }

}

public class Test extends Abs implements I{

    public static void main(String[] args) throws ExecutionException, InterruptedException 
    {

        Test t = new Test();
        t.print();// calls Abstract's print method and How to call interface's defaut method?
     }
}
于 2021-04-24T01:33:54.700 回答