简体   繁体   English

“ this.getClass()。getSuperclass()”的意外输出

[英]Unexpected output of “this.getClass().getSuperclass()”

Consider below two cases 考虑以下两种情况

Case I 案例一

I have a simple Java class which has a no-arguments constructor. 我有一个简单的Java类,它具有无参数的构造函数。 Below is the code 下面是代码

public class TestClassOne {

      public TestClassOne() {
           System.out.println("Parent class of TestClassOne is :" + this.getClass().getSuperclass());
    }
}

Object is the super class of all Java classes. 对象是所有Java类的超类。 So when I am creating an object of TestClassOne in my main method and run it, it is giving me the desire output which is 因此,当我在主要方法中创建TestClassOne的对象并运行它时,它为我提供了所需的输出 ,即

Parent class of TestClassOne is :class java.lang.Object

Case II 案例二

Now I have another class, named as TestClassTwo which extends TestClassOne. 现在,我有另一个名为TestClassTwo的类,它扩展了TestClassOne。 Below is the code 下面是代码

public class TestClassTwo extends TestClassOne {
}

Now when I am creating object of TestClassTwo in my main method and run it, TestClassOne's no-arguments constructor is also getting called implicitly as TestClassOne is super class of TestClassTwo and prints the output in the console. 现在,当我在主方法中创建TestClassTwo的对象并运行它时, 由于TestClassOne是TestClassTwo的超类并在控制台中输出输出,因此TestClassOne的无参数构造函数也被隐式调用 I was expecting the output will be same with the case I. But it is not. 我曾期望输出与情况I相同。但是事实并非如此。 The output is 输出是

Parent class of TestClassOne is :class org.test.TestClassOne

Why not the above output is same with Case I ? 为什么以上输出与案例I相同?

Could someone explain me why the outputs are different in Case I and Case II? 有人可以解释一下为什么案例一和案例二的输出不同吗?

You're calling getClass() on this . 您正在this调用getClass() The output is different because: 输出不同是因为:

  • In case 1, this refers to an instance of TestClassOne . 在情况1中, this指的是TestClassOne的实例。
  • In case 2, this refers to an instance of TestClassTwo . 在情况2中, this指的是TestClassTwo的实例。

Simple answer: Because this is the superclass. 简单的答案:因为这是超类。

public class TestClassOne {
    public TestClassOne() {
        System.out.println("Parent class of TestClassOne is :" + this.getClass().getSuperclass());
    }
}

This class extends java.lang.Object (even if it is not explicitly named). 此类extends java.lang.Object (即使未显式命名)。

So this references to an object of the class TestClassOne and the superclass is java.lang.Object . 因此, this引用了TestClassOne类的对象,而超类是java.lang.Object

public class TestClassTwo extends TestClassOne {
}

This class extends TestClassOne . 此类extends TestClassOne

So this references to an object of the class TestClassTwo and the superclass is TestClassOne . 因此, this引用了TestClassTwo类的对象,而超类是TestClassOne

For this always the current object is used and never the class where it is defined. 对于this总是使用当前对象 ,从来没有在那里它被定义的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 this.getClass()。getClassLoader()和ClassLoader - this.getClass().getClassLoader() and ClassLoader this.getClass()。getClassLoader()。getResource()==异常 - this.getClass().getClassLoader().getResource() == Exception 为什么在Java中这是可能的:this.getClass()。getClass()。getClass()...等 - Why is this possible in Java: this.getClass().getClass().getClass()…etc Object.getClass()。getSuperclass()不返回null? - Object.getClass().getSuperclass() not returning null? 使用this.getClass()。getDeclaredField方法时发生NullPointerException - NullPointerException while using this.getClass().getDeclaredField method 如何使用this.getClass()。getResource(String)? - How to use this.getClass().getResource(String)? 是否可以在调用super()之前访问this.getClass() - Is it possible to acces this.getClass() before calling super() Openshift this.getClass()。getResource()路径可能不正确 - Openshift this.getClass().getResource() path probably not correct 如何获得超类的this.getClass()。getConstructor? - How to get this.getClass().getConstructor of a super class? this.getClass()。getClassLoader()。getResourceAsStream()在RCP产品中不起作用 - this.getClass().getClassLoader().getResourceAsStream()is not working in RCP product
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM