3

我正在学习getClass它是如何工作的。

我读到: http ://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

但我真的不明白为什么会失败:

boolean b;
Class c = b.getClass();

谁能向我解释为什么它给我一个错误?

4

6 回答 6

3

boolean b;是原始数据类型,您不能使用. 运算符,试试Boolean bBoolean是原始的包装类boolean

试试这个:

Boolean b = null;
Class c = b.getClass();

或更好

Boolean b = null;
Class<? extends Boolean>  c = b.getClass();
于 2013-04-18T12:31:46.103 回答
0

您的变量b是 type boolean,这是一种原始类型。原始类型不是对象,因此,您无法获取它们的类。

于 2013-04-18T12:32:18.247 回答
0

因为booleans 不是对象。不能调用任何方法boolean

于 2013-04-18T12:32:19.387 回答
0

b - 是 primite,你不能调用 getClass 。它不是一个对象

于 2013-04-18T12:32:22.590 回答
0

因为您使用的是原始boolean. 对象不会发生Boolean

于 2013-04-18T12:32:27.320 回答
0

您不能对原始数据类型调用 getClass() 方法。这将为您解决问题

Boolean b;//Change it to Boolean wrapper class
Class c = b.getClass();
于 2013-04-18T12:32:56.807 回答