super.getClass().getName()方法调用返回的是谁?

来源:互联网 发布:mac开机头像怎么设置 编辑:程序博客网 时间:2024/04/29 23:58
package testKeywords;import java.util.Date;public  class TestSuper extends Date{    public static void main(String[] args) {        new TestSuper().test();    }        public void test(){        System.out.println(super.getClass().getName());    }}
复制代码

 

第一:不管是TestSuper的getClass()还是Date的getClass(),他们都是非覆盖式的从Object继承来的。

第二:Object的getClass()方法的释义是:返回此 Object 的运行时类。返回的 Class 对象是由所表示类的 static synchronized 方法锁定的对象。

    /**
     * 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.
     **/

    释义指出,要返回此Object运行时类,这外当然不可能指Object自己了,否则所有类调用getClass()方法都返回Object.class了。

    那到底谁是Object的运行时类呢,不是Object自己,当然就只能是他的儿子、或各种孙子了,到底是谁呢,举个例子:
    Date作为直接继承Object的类,作为Object的儿子,如果调用Date.getClass(),返回的是什么呢?在Date中,无论是this.getClass()还是super.getClass(),毫无疑问都指向了Object.getClass()。根据上一段解释,Object.getClass()返回不可能是Object.class,那没得说,只能是Date.class了。

    根据上段:new Date()时,Date是Object的运行时类。

    同理:根据代码,new TestSuper()时,运行是类不可能是TestSuper他老子Date,更不会他各种老老子(如Object),只能是自己。

    再回到Object.getClass()释义,返回的只能是TestSuper.class

第三:如果想要从TestSuper中得到Date.class,可以用TestSuper.getClass().getSuperCl
0 0
原创粉丝点击