继承与对象创造,理论与现实 [英] Inheritance and Object creation, Theoretically and in Real

查看:139
本文介绍了继承与对象创造,理论与现实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个班级 A.java

当我执行A的构造函数方法时,它将创建一个 xyz 对象的内存空间。

When I will execute a constructor method of A, it will create a memory space for xyz Object.

A xyz = new A();

对内存的引用可能类似于,

The reference to memory may be something like,

[xyz] ---> '0x34524'

这是OOP的基础知识。很简单!

Thats basics of OOP. Simple enough!

如果一个类继承自不同的父类会怎样?将在内存中创建多少个对象空间?

What happens if a class is inheriting from different parent classes? How many object space will be created in memory?

假设我们有,

然后我们创建一个类<$的对象c $ c> D.java ,

D omg = new D();

这里我们知道D的对象将调用C.java的构造,依此类推,直到A.java 。这是否意味着在内存中我们有4个不同的内存引用,因为我们实例化了所有这四个对象(一个是直接的,另一个是3个间接的)?

Here as we know that D's object will call construct of C.java and so on until A.java. Does this mean that in memory we are having 4 different memory reference, because we are instantiating all of the four objects (one directly and another 3 indirectly)?

[omg] ---> '0x34525'
[C]   ---> '0x34526'
[B]   ---> '0x34527'
[A]   ---> '0x34528'






注意:


  1. 这不是作业问题,这只是一个好奇心问题。

  2. 我知道如果我们在A.java中有一个实例变量,那么我们不会只创建对象A,但只要我们点击 new keyword。

  1. This isn't homework question, this is just a curiosity question.
  2. I am aware of the fact that if we have a instance variable inside an A.java then we will not create only object A but we will be creating other internal object whenever we hit new keyword.


推荐答案

首先,tid位...调用对象​​的构造函数不分配它。在字节码中,初始化 new Object()表示为...的效果。

First, a tid bit... calling the constructor of an object does not allocate it. In bytecode, the initialization new Object() is expressed as something to the effect of...

new java/lang/Object
invokespecial java/lang/Object <init>()V

new 指令负责分配空间并获取对未初始化对象的引用,而 invokespecial 处理调用构造函数本身(在内部编译为 void 名为的方法< init> ,因此描述符 < INIT> ()V )。

The new instruction takes care of allocating the space and acquiring a reference to the yet uninitialized object, while the invokespecial handles calling the constructor itself (which is internally compiled to a void method named <init>, hence the descriptor <init>()V).

继续,对象分配的内部和堆上的表示完全是JVM特定的。但是,据我所知,每个分配的对象只有一个分配的对象,无论它的超类数量如何。内存中的对象本身具有用于其自己的类及其超类的实例字段的空间。它还必须有空间用于 虚拟方法表 ,以便在执行虚方法调用时执行虚拟调度(例如,通过 invokevirtual )。

Moving on, internals of object allocation and representation on the heap are entirely JVM specific. However, as far as I know, there is only one allocated object per allocated object, no matter its number of super classes. The object itself in memory has space for the instance fields of both its own class and its super classes. It must also have space for a virtual method table, in order to do virtual dispatch when performing virtual method calls (e.g. via invokevirtual) for the object.

Oracle内部HotSpot JVM管理名为 oops 或普通对象指针的东西。您可以在此处了解有关HotSpot内存布局的更多信息。随意浏览HotSpot源代码库

Internally, the Oracle HotSpot JVM manages things called oops, or ordinary object pointers. You can read more about the HotSpot memory layout here. Feel free to browse the HotSpot source repository.

这篇关于继承与对象创造,理论与现实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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