【发布时间】:2012-11-13 19:13:24
【问题描述】:
可能重复:
What’s the difference between calling MyClass.class and MyClass.getClass()
想要有一个Class<T> 对象,这两种方法有什么区别?
Test ob = new Test();
ob.getclass();
或
Test.class
【问题讨论】:
标签: java
可能重复:
What’s the difference between calling MyClass.class and MyClass.getClass()
想要有一个Class<T> 对象,这两种方法有什么区别?
Test ob = new Test();
ob.getclass();
或
Test.class
【问题讨论】:
标签: java
【讨论】:
void 的类文字。这些对于反射很有用,例如 Method.getReturnType() 对于返回原语或 void 的方法。
getClass() 方法在java.lang.Object 中定义,因此可以在任何对象引用上调用。
它获取与引用指向的对象的运行时类型关联的 Class 对象。 .
getClass() 方法根本不类似于 .class。更接近的是 Class.forName(String),它为 String 命名的类获取一个 Class 对象。
在可以使用其中任何一个的情况下,请使用.class,因为它更有效。
【讨论】: