Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中显式调用默认方法_Java_Inheritance_Interface_Java 8_Default Method - Fatal编程技术网

在Java中显式调用默认方法

在Java中显式调用默认方法,java,inheritance,interface,java-8,default-method,Java,Inheritance,Interface,Java 8,Default Method,Java8引入了扩展接口的功能,无需修改现有的实现 我想知道,当某个方法被重写或由于不同接口中的默认实现冲突而不可用时,是否可以显式调用该方法的默认实现 interface A { default void foo() { System.out.println("A.foo"); } } class B implements A { @Override public void foo() { System.out.println(

Java8引入了扩展接口的功能,无需修改现有的实现

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

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

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

您不需要重写接口的默认方法。你可以这样称呼它:

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

阿福

根据,您可以使用访问界面
A
中的默认方法

A.super.foo();
这可以按如下方式使用(假设接口
A
C
都有默认方法
foo()

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


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

下面的代码应该可以工作

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

这个答案主要是为来自已关闭的问题的用户编写的

Java8接口介绍了多重继承的一些方面。默认方法有一个实现的函数体。要从super类调用方法,可以使用关键字
super
,但如果要使用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();
    }
}
输出:

家长们好
你好
你好,我的朋友


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

考虑以下示例:

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

OP说:“[是否]可以在方法被重写时显式调用该方法的默认实现”您能告诉我为什么在接口a中有foo()方法的实现吗???@MaciejCygan在Java 8中是允许的。另外请注意,如果
a扩展了SomeOtherInterface
,而
SomeOtherInterface
具有
默认类型method()
,那么您不能从ChildClass调用
SomeOtherInterface.super.method()
。您只能调用
ChildClass
implements
子句中枚举的接口的默认方法,而不能调用它们的父接口的方法。@Suseika很好,这与没有
super.super.someMethod()一样
(因为那会很可怕)@gvlasov很好,但如何从子接口访问父接口的默认方法,可能吗??更新。。。。。。。。。。是的,可能,这里更具体的解释@RichardTingle完美的答案!
Interface.super.method()
背后的基本原理是什么?为什么不干脆
Interface.method()
?我认为这是描述上述问题的最好例子。谢谢
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?
     }
}