Java术语,用于区分运行时类型与编译时类型 [英] Java terminology for differentiating runtime type from compile-time type
问题描述
在Java中,Object
可以具有运行时类型(即其创建时的类型)和强制类型(您将其强制转换为的类型).
In Java, an Object
can have a runtime type (which is what it was created as) and a casted type (the type you have casted it to be).
我想知道这些类型的适当名称是什么.例如
I'm wondering what are the proper name for these types. For instance
class A {
}
class B extends A {
}
A a = new B();
a被创建为B
,但是被声明为A
.使用每个透视图引用a
类型的正确方法是什么?
a was created as a B
however it was declared as an A
. What is the proper way of referring to the type of a
using each perspective?
推荐答案
我认为区分 object (在执行时存在,并且只有其执行时间类型)与具有编译时类型的 expression (例如变量).
I think it's important to distinguish between the object (which exists at execution time, and just has its execution time type) and an expression (such as a variable) which has a compile-time type.
所以在这种情况下:
A a = new B();
a
是类型为A
的变量.它在执行时的值是对B
类型的对象的引用.
a
is a variable, of type A
. Its value at execution time is a reference to an object of type B
.
The Java language specification uses "run-time class" (e.g. for the purpose of overriding, as in section 15.12.4.4) for the type of an object. Elsewhere I think it just uses "type" for the type of an expression, meaning the compile-time type.
这篇关于Java术语,用于区分运行时类型与编译时类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!