Java中动态和静态类型赋值的区别 [英] Difference between Dynamic and Static type assignments in Java

查看:30
本文介绍了Java中动态和静态类型赋值的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下类层次结构,以下语句的动态和静态类型是什么?

Given the follow class hierarchy what are the dynamic and static types for the following statements?

类层次结构:

class Alpha {}

class Beta extends Alpha {}

class Gamma extends Alpha {}

class Epsilon extends Alpha{}

class Fruit extends Gamma{}

class Golf extends Beta {}

class Orange extends Fruit{}

对于以下每个语句,静态类型?动态类型?:

For each of the following statements, Static Type? Dynamic Type?:

Fruit f = new Fruit();
Alpha a = f;
Beta b = f;
a = b;
Gamma g = f;

我的回答/问题
我知道 Fruit f = new Fruit() 将是静态和动态类型 Fruit.
Alpha a = f; 在编译时为 Alpha 类型(静态),在运行时为 Fruit 类型(动态).
Gamma g = f; 在编译时为 Gamma 类型(静态),在运行时为 Fruit 类型(动态).
但是我不知道其他两个答案.Beta b = f 是一个实例,其中同一个超类的两个子类相互分配,所以我不确定它在编译时是 Beta 类型还是 Alpha 类型(静态).a = b 是声明后的赋值,所以我不确定答案是什么.有人请帮帮我谢谢!

My answers/questions
I understand that Fruit f = new Fruit() will be of both static and dynamic type Fruit.
Alpha a = f; Will be of type Alpha at at compile time (static) and type Fruit at runtime (dynamic).
Gamma g = f; Will be of type Gamma at compile time (static) and type Fruit at runtime (dynamic).
However I do not know the other two answers. Beta b = f is an instance in which two subclasses of the same super class are assigned to one another so I'm not sure if it would be of type Beta or type Alpha at compile time (static). And a = b is an assignment after declaration so I'm not sure what the answer for that would be. Someone please help me out thanks!

推荐答案

我打字很匆忙,所以请原谅任何错别字(我稍后有机会时会更正).

I'm typing this in a hurry, so pls excuse any typos (I'll fix those later when I get a chance).

我知道 Fruit f = new Fruit() 将是静态和动态类型 Fruit.

I understand that Fruit f = new Fruit() will be of both static and dynamic type Fruit.

我认为您有点混淆了static 和 dynamic 类型与编译时和运行时类型(或在 C++ 中,当您将类型 A 的对象的地址分配给 aB 类型的指针,B 是 A 的父类.)

I think you are confusing a bit the terms static and dynamic types with compile-time and run-time types (or as in C++ when you assign the address of a object of type A to a pointer of type B with B being the parent class of A.)

除了反射技巧,Java 中没有动态类型.一切都是在编译时静态输入的.运行时对象的类型与其编译后的类型相同.

Barring reflection tricks, there is no dynamic typing in Java. Everything is statically typed at compile time. The type of an object at run-time is the same as the one it got compiled to.

发生的事情是您将对象引用(a、b、c、f)与在堆中实例化的实际对象(任何使用 new 创建的对象)混淆.

What is happening is that you are confusing object references (a, b, c, f) with actual objects instantiated in the heap (anything created with new.)

在 Java 中,f 是一个对象引用,而不是对象本身.而且,f 的引用类型是Fruit 及其子类.您分配给它的对象 (new Fruit()) 恰好是 Fruit 类型.

In Java, f is an object reference, not the object itself. Moreover, the reference type of f is Fruit and sub-classes of it. The object (new Fruit()) that you assign to it happens to be of type Fruit.

现在示例代码中的所有其他引用,a 属于 对 A 及其子类的引用;b 是类型 对 B 及其子类的引用;等等等等

Now all the other references in your sample code, a is of type reference to A and sub-classes of it; b is of type reference to B and sub-classes of it; etc, etc.

记住这一点,因为它非常重要.

阿尔法 a = f;在编译时为 Alpha 类型(静态),在运行时为 Fruit 类型(动态).

Alpha a = f; Will be of type Alpha at at compile time (static) and type Fruit at runtime (dynamic).

a 的类型是对类型 A 和子类的引用".f 属于对 Fruit 类型和子类的引用"类型.

a is of type 'reference to type A and sub-classes'. f is of type 'reference to type Fruit and sub-classes'.

f 指向的对象是水果"类型.当您说 'a = f' 时,您并没有将 'f' 分配给 'a'.您是在说a 现在将引用 f 当前正在引用的事物".

The object f points to is of type 'Fruit'. When you say 'a = f' you are not assigning 'f' to 'a'. You are saying 'a now will reference that thing f is currently referencing to'.

那么在分配之后,什么是 a 引用?类型为Fruit的对象,对象引用f在赋值时指向.

So after that assignment, what is a referencing? The object of type Fruit the object reference f pointed to at the time of assignment.

记住,a、b、g、f,它们不是对象.它们是对使用 new 运算符以一种或另一种方式创建的对象的引用或句柄.

Remember, a, b, g, f, they are not objects. They are references or handles to objects created one way or another with the new operator.

a、b 或 f 等引用变量与使用 new 创建的对象不同.但碰巧前者可以指向后者.

A reference variable such as a, b or f are different beasts from the objects created with new. But it just so happen that the former can point to the later.

运行时用new创建的对象类型与编译时确定的对象类型相同.

The type of the object created with new at run-time is the same as the one determined at compile time.

伽玛 g = f;在编译时为 Gamma 类型(静态),在运行时为 Fruit 类型(动态).

Gamma g = f; Will be of type Gamma at compile time (static) and type Fruit at runtime (dynamic).

同上.变量 g 是一个类型为 reference to type Gamma and sub-classes 的对象引用.在这个赋值中,g 指向由 f 指向的同一个对象.该对象的类型是什么?编译时给出的相同:Fruit.

Same as above. The variable g is an object reference of type reference to type Gamma and sub-classes. In this assignment, g is made to point to the same object pointed by f. What is the type of that object? The same given at compile time: Fruit.

不过我不知道另外两个答案.Beta b = f 是一个实例其中同一个超类的两个子类被分配给一个另一个所以我不确定它是 Beta 类型还是 Alpha 类型编译时间(静态).

However I do not know the other two answers. Beta b = f is an instance in which two subclasses of the same super class are assigned to one another so I'm not sure if it would be of type Beta or type Alpha at compile time (static).

b 是类型 对 Beta 类型及其子类的引用.它在赋值 b = f 之后指向的对象是 Fruit 类型,它在编译时的类型.

b is of type reference to type Beta and sub-classes of it. The object it points to after the assignment b = f is of type Fruit, the type it had at compile time.

  1. 对象引用 a、b、g 和 f 的类型是在编译时确定的.它们是静态类型的,不会在运行时更改.

  1. The type of object references a, b, g, and f is determined at compile time. They are statically typed and do not change at run-time.

使用new 创建的对象的类型也在编译时确定.它们也是静态类型的,不会在运行时更改.

The type of an object created with new is also determined at compile time. They are also statically typed and do not change at run-time.

对象,stuff 对象引用 a、b、g 和 f 指向运行时,这取决于编译器是否发现语句有效.赋值可以更改,但这与对象引用或对象本身是静态类型还是动态类型无关.

The objects, the stuff object references a, b, g and f point to at run-time, that is determined by whether the statements are found valid by the compiler. The assignments can change, but that has nothing do with whether the object references or the object themselves are statically or dynamically typed.

如果您想清楚区分动态类型和静态类型,请考虑以下事项:

If you want to see a clear distinction between dynamic and static typing consider the following:

// Java, statically typed.
int x = 3;
x = 5; // good
x = "hi"; // compiler error

## Ruby, dynamically typed
x = 3 # ok
x = 5 # ok
x = "hi" # still ok

然后是强类型语言和弱/鸭子类型语言(两者都可以动态类型)之间的区别.有很多关于这个主题的文献.

Then there is the distinction between strongly typed and weakly/duck typed languages (both of which can be dynamically typed.) There is plenty of literature on this subject out there.

希望有帮助.

这篇关于Java中动态和静态类型赋值的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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