为什么接口根据类文件格式扩展Object? [英] Why do interfaces extend Object, according to the class file format?

查看:192
本文介绍了为什么接口根据类文件格式扩展Object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么JVM规范声明接口必须具有 super_class java / lang / Object ,即使接口不扩展 java / lang / Object

Why does the JVM specification state that interfaces must have a super_class of java/lang/Object, even though interfaces do not extend java/lang/Object?

我特指的是§4.1,其中说:

I'm specifically referring to §4.1 of the JVM spec, where it says:


对于接口,super_class项的值必须始终是constant_pool表的有效索引。该索引处的constant_pool条目必须是表示类Object的CONSTANT_Class_info结构。

For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object.

还在§9.2,它说接口不扩展Object 。而是声明一个隐式创建的抽象方法,它匹配Object类中的每个公共方法:

yet in §9.2 of the JLS, it says that interfaces do not extend Object. Instead a implicitly created abstract method is declared which matches each public method in the Object class:


如果一个接口没有直接的超级接口,那么interface隐式声明一个公共抽象成员方法m,其中包含签名s,返回类型r和throws子句t,对应于每个公共实例方法m,其中包含签名s,返回类型r和在Object中声明的throws子句t,除非具有相同的方法签名,相同的返回类型和兼容的throws子句由接口显式声明。

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.


推荐答案

§9.2:


如果接口没有直接的超接口,则接口
隐式声明带有签名$的公共抽象成员方法m b $ bs,返回类型r和throws子句t correspo使用签名s返回每个公共
实例方法m,返回类型r和在Object中声明的throws子句t
,除非具有相同签名的方法,相同的
返回类型和兼容throws子句由
接口显式声明。

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.

因此,我们看到,虽然没有直接超接口的接口也没有显式扩展 Object 但内部仍然有一个带有 Object 类的链接,因为它被编译器用来插入具有相同签名和返回类型以及throws子句的抽象方法,与接口中 Object 类中的公共方法相同。这就是为什么对于接口,super_class项的值必须始终是constant_pool表的有效索引。该索引处的constant_pool条目必须是表示类 Object 的CONSTANT_Class_info结构。这就是接口引用变量可以成功调用公共实例方法的原因,例如 toString() Object 的方法。例如,考虑下面给出的代码:

Hence , we see that , Although an interface having no direct superinterface doesn't explicitly extends Object but still it has a link with Object class internally as it is used by the compiler to insert abstract methods with same signature and return type and throws clause as that of public methods in Object class, within the interface. That's why For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object. This is the reason that an interface reference variable can successfully call public instance methods for example toString() method of Object . For example, consider the code given below:

interface MyInterface
{}
public class InterfaceTest implements MyInterface
{
    public static void main(String[] args) 
    {
        MyInterface mInterface = new InterfaceTest();
        System.out.println(mInterface.toString());//Compiles successfully. Although toString() is not declared within MyInterface
    }
}

以上即使 toString()方法( Object 的方法)未在<$ c中声明,代码也能成功编译$ C> MyInterface的。以上代码在我的系统上提供以下输出:

The above code compiles successfully even though toString() method (Which is the method of Object) is not declared within MyInterface. Above code is providing following output on my System:

InterfaceTest@1ba34f2

输出可能因系统而异。

这篇关于为什么接口根据类文件格式扩展Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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