java.lang.class_关于Java.lang.Class的一些疑问
User.class可以在編譯時(shí)就確定下來(lái)Class的泛型,而new User().getClass()實(shí)際上是運(yùn)行時(shí)才能確定下來(lái)實(shí)際是什么泛型。舉個(gè)例子:
public class User{
}
public class Student extends User{
public static void main(String[] args) {
User user1 = new User();
User user2 = new Student();
//輸出User
System.out.println(user1.getClass());
//輸出Student
System.out.println(user2.getClass());
}
}
由此可見(jiàn),Object.getClass()方法返回的Class泛型是運(yùn)行時(shí)才能確定的,所以返回的類型是Class>,順帶貼下jdk源碼上的注釋
/**
* Returns the runtime class of this {@code Object}. The returned
* {@code Class} object is the object that is locked by {@code
* static synchronized} methods of the represented class.
*
*
The actual result type is {@code Class extends |X|>}
* where {@code |X|} is the erasure of the static type of the
* expression on which {@code getClass} is called. For
* example, no cast is required in this code fragment:
*
*
* {@code Number n = 0; }
* {@code Class extends Number> c = n.getClass(); }
*
*
* @return The {@code Class} object that represents the runtime
* class of this object.
* @see Class Literals, section 15.8.2 of
* The Java? Language Specification.
*/
public final native Class> getClass();
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java.lang.class_关于Java.lang.Class的一些疑问的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 谷歌成苹果默认搜索引擎 向苹果支付了数
- 下一篇: 新三板是什么 新三板上市所需条件