Java如何调用对象的功能? [英] How does Java call object's function?

查看:64
本文介绍了Java如何调用对象的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我之前所读的内容,在将.java文件编译为.class文件之后,擦除后每个对象都只是Object.如

From what I read before, after .java file compiles to .class files, every object is simply Object after erasure. Such as

Foo f = new Foo();

编译为.class文件,然后反编译为:

Compiles to .class file, and decompile, it becomes:

Object f = new Foo();

那么JRE在运行时如何调用对象的功能?函数存储在内存中的什么位置?在物体里面?还是具有类结构的层次结构,是否可以查找层次结构?

So how does JRE call the function of an object when running? Where is the function stored in the memory? Inside the object? Or with a hierarchy of class structure and does lookup up the hierarchy?

推荐答案

根据维基百科

Java类文件结构有10个基本部分:

There are 10 basic sections to the Java Class File structure:

  • 幻数:0xCAFEBABE
  • 类文件格式的版本:类文件的次要版本和主要版本
  • 常量池:类的常量池
  • 访问标志:例如,该类是否为抽象类,静态类等.
  • 该课程:当前课程的名称
  • 超级类:超级类的名称
  • 接口:类中的任何接口
  • 字段:班级中的所有字段
  • 方法:该类中的任何方法
  • 属性:类的任何属性(例如源文件的名称等)
  • Magic Number: 0xCAFEBABE
  • Version of Class File Format: the minor and major versions of the class file
  • Constant Pool: Pool of constants for the class
  • Access Flags: for example whether the class is abstract, static, etc.
  • This Class: The name of the current class
  • Super Class: The name of the super class
  • Interfaces: Any interfaces in the class
  • Fields: Any fields in the class
  • Methods: Any methods in the class
  • Attributes: Any attributes of the class (for example the name of the sourcefile, etc.)

在运行时,将检索对象的类型,其类文件(或虚拟方法表)检查所调用方法的实现.如果该类没有这样的实现,则检查父类(从超级类条目中检索),依此类推,如果找不到任何子类,最终将失败.

At run time, the type of the object is retrieved, its class file (or rather the virtual method table) is checked for an implementation of the method invoked. If that class doesn't have such an implementation, the parent class is checked (retrieved from super class entry), and so on, eventually failing if none is found.

这篇关于Java如何调用对象的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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