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

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

问题描述



类层次结构:

  class Alpha {} 

class Beta扩展Alpha {}

class Gamma extends Alpha { }

class Epsilon扩展Alpha {}

class Fruit extends Gamma {}

class Golf extends Beta {}

class Orange extends Fruit {}

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

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

我的答案/问题
我明白 Fruit f = new Fruit()将是静态和动态类型的Fruit。 a
Alpha a = f; 在编译时(静态)为Alpha类型,并在运行时(动态)键入Fruit。
$
Gamma g = f; 将在编译时(静态)使用Gamma类型,并在运行时(动态)键入Fruit。


但是我不知道其他两个答案。 Beta b = f是同一个超类的两个子类彼此分配的一个实例,所以我不确定它是否是Beta类型,或者在编译时输入Alpha(static)。而a = b是声明后的任务,所以我不知道那是什么答案。有人请帮助我,谢谢!

解决方案

我正在打字,所以请原谅任何打字错误(我会我们明白Fruit f = new Fruit()将是静态和动态类型。水果。


我认为你对静态和动态类型的术语感到困惑编译时和运行时类型(或者当您将类型A的对象的地址分配给类型B的指针时,C中的B为B的父类)。



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



发生的是你是混淆对象引用(a,b, c,f)在堆中实例化的实例(任何使用新建创建的对象)。



在Java中, code> f 是一个对象引用,而不是对象本身。此外,参考类型 f 它的水果和子类。您分配给它的对象( new Fruit())恰好是类型 Fruit



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



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


Alpha a = f;在编译时(静态)将类型为Alpha,并在运行时(动态)键入Fruit。


a是类型A和子类的引用类型。
f的类型是引用类型为Fruit和子类。



对象f指向Fruit类型。当你说'a = f'时,你不会将'f'分配给'a'。你正在说'现在将会引用那个f正在引用'。



所以在这个赋值之后,什么是 a 引用? c $ c> c> c c c b

记住,a,b,g,f,它们不是对象。它们是使用运算符创建的对象的引用或句柄。



一个引用变量,如a,b或f是使用 new 创建的对象的不同的野兽。但是,只有这样才能发现,前者可以指向后者。



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


Gamma g = f;在编译时(静态)将类型为Gamma,并在运行时(动态)键入Fruit。


与上述相同。变量 g 是类型为的引用类型为Gamma和子类的对象引用。在此作业中, g 指向与 f 指向的同一个对象。该对象的类型是什么?在编译时同样给出:水果。


但是我不知道其他两个答案。 Beta b = f是一个实例
,其中同一个超类的两个子类分配给一个
另一个,所以我不知道它是否为Beta类型,或者键入Alpha
编译时(静态)。


b是类型为$ code的引用类型为Beta及其子类。在分配之后它指向的对象 b = f 是类型为$ code> Fruit ,它是编译时的类型。


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


  2. 使用新建也在编译时确定。它们也是静态类型的,在运行时不要更改。


  3. 对象引用a,b,g和f指向在运行时,这是由编译器发现有效的语句决定的。分配可以改变,但这与对象引用或对象本身是静态还是动态键入无关。


如果要在动态和静态类型之间看到明确的区别,请考虑以下内容:

  //静态类型的Java。 
int x = 3;
x = 5; // good
x =嗨; //编译器错误

## Ruby,动态类型
x = 3#ok
x = 5#ok
x =hi#still ok

然后,强类型和弱/鸭型语言之间有区别(两者都可以动态地键入)



希望有帮助。


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

Class hierarchy:

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;

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).

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

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.)

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.

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.)

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.

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.

Keep this in mind because it is very important.

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

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

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'.

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

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 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.

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

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

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.

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 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. 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.

  2. 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.

  3. 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.

Hope it helps.

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

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