对象类型和引用类型之间的区别 [英] Difference Between Object Type and Reference Type

查看:626
本文介绍了对象类型和引用类型之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究"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);

引用 o的类型为Object.它引用的 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屋!

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