public static void main(String[] args) {
new StringTest().show();
}
private void show(){
System.out.println(super.getClass().getName());
}
由以上代码组成的测试案例:
1、预计运行结果:Date
2、实际运行结果:StringTest
3、运行结果分析:
/**
* 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.
*
* <p><b>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.</b> For
* example, no cast is required in this code fragment:</p>
*
* <p>
* {@code Number n = 0; }<br>
* {@code Class<? extends Number> c = n.getClass(); }
* </p>
*
* @return The {@code Class} object that represents the runtime
* class of this object.
* @see <a href="http://java.sun.com/docs/books/jls/">The Java
* Language Specification, Third Edition (15.8.2 Class
* Literals)</a>
*/
public final native Class<?> getClass();
以上是Object类中getClass()方法及注释,由于getClass方法被定义为final,所以该方法不可继承(重写)。
由Object类的getClass()方法注释可知,getClass()方法返回的是运行时主调类的类名。