super.getClass().getName()

本文通过一个具体的Java代码示例,解析了Object类中的getClass()方法,并解释了其返回值为何是运行时对象的类名而非父类名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class StringTest extends Date{
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()方法返回的是运行时主调类的类名。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值