对象类型和引用类型的区别 [英] Difference Between Object Type and Reference Type
问题描述
我从Head First Java"开始学习多态,然后想到了这个概念.谁能用例子解释一下?
I was studying Polymorphism from "Head First Java" and came to this concept. Can anyone explain it please with an example?
编译器检查引用类型的类——而不是对象类型.
Compiler checks the class of reference type -- not the Object type.
那么引用类型和对象类型有什么区别?
So what's the difference between Reference Type and Object Type?
推荐答案
我不认为他们对对象类型"和引用类型"的使用是标准化的,但这是我的解释.
I don't think their use of "object type" and "reference type" is standardized, but here's my interpretation.
考虑这个代码:
Object o = new Integer(3);
reference o
属于 Object
类型.它引用的对象是Integer
类型.
The reference o
is of type Object
. The object that it references is of type Integer
.
所以引用类型"将是 Object
而对象类型"将是 Integer
.
So the "reference type" would be Object
and the "object type" would be Integer
.
令人困惑的是,有(标准化的、官方的)术语引用类型"封装了可以被引用.在 Java 中,包括所有类、枚举、接口、数组.它只排除原始类型 (int
, ...).
What makes this confusing is that there's the (standardized, official) term "reference type" that encapsulates types that can be referenced. In Java that includes all classes, enums, interfaces, arrays. It excludes only the primitive types (int
, ...).
这篇关于对象类型和引用类型的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!