Java中的多态性,数据类型和内存

时间:2022-01-09 16:23:18

I have a question regarding Polymorphic definition and initialization. To my understanding, data types are used to reserve a specific amount of memory.

我有一个关于多态定义和初始化的问题。据我所知,数据类型用于保留特定数量的内存。

Base b = new SubClass();

If I have a base class which only has 1 member (int x), and my subclass has an additional member (int y), then how can a variable defined as a Base (4 bytes) contain 2 int members?(8 bytes) Is it because Java dynamically allocates memory?

如果我有一个只有1个成员(int x)的基类,并且我的子类有一个额外的成员(int y),那么定义为Base(4个字节)的变量如何包含2个int成员?(8个字节)是因为Java动态分配内存吗?

1 个解决方案

#1


3  

(This answer is mostly for Java. The main thrust of it should be true for C++ as well, but that's outside my area of expertise.)

(这个答案主要是针对Java的。对于C ++来说,它的主要推动力应该是正确的,但这超出了我的专业领域。)

The variable doesn't contain the object. The variable contains an object reference, which (as the name implies) is a reference to the object, which exists elsewhere in memory:

该变量不包含该对象。该变量包含一个对象引用,(顾名思义)是对象的引用,它存在于内存的其他地方:

                           +−−−−−−−−−−−−−−−−−−−−−+
[b (Base): Ref12315]−−−−−−>| (SubClass instance) |
                           +−−−−−−−−−−−−−−−−−−−−−+
                           | x (int): 42         |
                           | y (int): 27         |
                           +−−−−−−−−−−−−−−−−−−−−−+

There I'm using Ref12315 as a notional value of an object reference. We never actually see the raw value of an object reference in our code (they're opaque data structures).

在那里我使用Ref12315作为对象引用的名义值。我们实际上从未在代码中看到对象引用的原始值(它们是不透明的数据结构)。

That's part of why if we do this:

如果我们这样做,这就是原因的一部分:

Base c = b;

...we don't get a copy of the object, just two references to it (modulo C++ copying semantics that can be applied, I think(?), via operator overloading):

...我们没有得到对象的副本,只有两个对它的引用(模拟C ++复制语义可以应用,我认为(?),通过运算符重载):


[b (Base): Ref12315]−−−−+
                        |  +−−−−−−−−−−−−−−−−−−−−−+
                        +−>| (SubClass instance) |
                        |  +−−−−−−−−−−−−−−−−−−−−−+
[c (Base): Ref12315]−−−−+  | x (int): 42         |
                           | y (int): 27         |
                           +−−−−−−−−−−−−−−−−−−−−−+

All object references are the same size, regardless of type. Objects vary in size.

无论类型如何,所有对象引用都具有相同的大小。物体大小不一。

#1


3  

(This answer is mostly for Java. The main thrust of it should be true for C++ as well, but that's outside my area of expertise.)

(这个答案主要是针对Java的。对于C ++来说,它的主要推动力应该是正确的,但这超出了我的专业领域。)

The variable doesn't contain the object. The variable contains an object reference, which (as the name implies) is a reference to the object, which exists elsewhere in memory:

该变量不包含该对象。该变量包含一个对象引用,(顾名思义)是对象的引用,它存在于内存的其他地方:

                           +−−−−−−−−−−−−−−−−−−−−−+
[b (Base): Ref12315]−−−−−−>| (SubClass instance) |
                           +−−−−−−−−−−−−−−−−−−−−−+
                           | x (int): 42         |
                           | y (int): 27         |
                           +−−−−−−−−−−−−−−−−−−−−−+

There I'm using Ref12315 as a notional value of an object reference. We never actually see the raw value of an object reference in our code (they're opaque data structures).

在那里我使用Ref12315作为对象引用的名义值。我们实际上从未在代码中看到对象引用的原始值(它们是不透明的数据结构)。

That's part of why if we do this:

如果我们这样做,这就是原因的一部分:

Base c = b;

...we don't get a copy of the object, just two references to it (modulo C++ copying semantics that can be applied, I think(?), via operator overloading):

...我们没有得到对象的副本,只有两个对它的引用(模拟C ++复制语义可以应用,我认为(?),通过运算符重载):


[b (Base): Ref12315]−−−−+
                        |  +−−−−−−−−−−−−−−−−−−−−−+
                        +−>| (SubClass instance) |
                        |  +−−−−−−−−−−−−−−−−−−−−−+
[c (Base): Ref12315]−−−−+  | x (int): 42         |
                           | y (int): 27         |
                           +−−−−−−−−−−−−−−−−−−−−−+

All object references are the same size, regardless of type. Objects vary in size.

无论类型如何,所有对象引用都具有相同的大小。物体大小不一。