先来代码
结果贴出来:
A
A
Object
原因很简单,就是因为getClass()方法返回的是运行时的类名。一下是getClass()源码的部分注释:
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.
package test;
public class TestGetClass {
public static void main(String[] args) {
A a1 = new A();
Object o1 = new A();
System.out.println(a1.getClass().getSimpleName());
System.out.println(o1.getClass().getSimpleName());
System.out.println(o1.getClass().getSuperclass().getSimpleName());
}
}
class A {
}结果贴出来:
A
A
Object
原因很简单,就是因为getClass()方法返回的是运行时的类名。一下是getClass()源码的部分注释:
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.
本文详细解读了Java中的getClass()方法,解释了其返回运行时类名的原理,并通过示例展示了如何使用该方法获取类名和超类名。
212

被折叠的 条评论
为什么被折叠?



