是否可以调用superinterface的默认方法? [英] Is calling a superinterface's default method possible?

查看:509
本文介绍了是否可以调用superinterface的默认方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个课程, A B

Say I have two classes, A and B:

class A
{
    void method()
    {
        System.out.println("a.method");
    }
}





class B extends A
{
    @Override
    void method()
    {
        System.out.println("b.method");
    }
}

实例化 B as b ,我可以调用 B 的方法,如 b。方法()。我还可以使用 super.method进行 B 的方法调用 A 的方法)。但是如果 A 是一个界面怎么办:

After instantiating B as b, I can call B's method like b.method(). I can also make B's method call A's method with super.method(). But what if A is an interface:

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





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

我有什么方法可以赚 B 的方法调用 A 的方法?

Is there any way I can make B's method call A's method?

推荐答案

是的,你可以。使用

A.super.method();

JLS 状态


如果表单是 TypeName。超级[TypeArguments] Identifier ,然后:

If the form is TypeName . super . [TypeArguments] Identifier, then:

如果 TypeName

It is a compile-time error if TypeName denotes neither a class nor an interface.

如果 TypeName 表示一个类C,那么要搜索的类是C的超类。

If TypeName denote a class, C, then the class to search is the superclass of C.

如果C不是当前类的词法封闭类型
声明,则是编译时错误,或者如果C是类Object。

It is a compile-time error if C is not a lexically enclosing type declaration of the current class, or if C is the class Object.

T 成为立即封闭方法的类型声明
调用。如果 T 是类Object,则为编译时错误。

Let T be the type declaration immediately enclosing the method invocation. It is a compile-time error if T is the class Object.

否则, TypeName 表示要搜索的界面,

Otherwise, TypeName denotes the interface to be searched, I.

这篇关于是否可以调用superinterface的默认方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆