简体   繁体   English

在Java中,super.getClass()打印“Child”而不是“Parent” - 为什么会这样?

[英]In Java super.getClass() prints “Child” not “Parent” - why is that?

In Java classes and objects, we use "this" keyword to reference to the current object within the class. 在Java类和对象中,我们使用“this”关键字来引用类中的当前对象。 In some sense, I believe "this" actually returns the object of itself. 从某种意义上说,我相信“这个”实际上会回归自身的对象。

Example for this: 示例:

class Lion
{
    public void Test()
    {
        System.out.println(this);  //prints itself (A Lion object)
    }
}

In the scenario of a superclass and subclass. 在超类和子类的场景中。 I thought that "super" keyword would return the object of the superclass. 我认为“超级”关键字会返回超类的对象。 However it seems that I got it wrong this time: 但是这次我似乎弄错了:

Example: 例:

class Parent
{
    public Parent(){
    }
}

class Child extends Parent
{
    public Child(){
        System.out.println(super.getClass()); //returns Child. Why?
    }
}

My Quesiton: In the above example, I was expecting the compiler to print out class Parent , however it prints out class Child . 我的问题:在上面的例子中,我期望编译器打印出class Parent ,但它打印出class Child Why is this so? 为什么会这样? What super actually returns? 什么超级实际返回?

A method call using super just ignores any overrides in the current class. 使用super的方法调用只会忽略当前类中的任何覆盖。 For example: 例如:

class Parent {
    @Override public String toString() {
        return "Parent";
    }
}

class Child extends Parent {
    @Override public String toString() {
        return "Child";
    }

    public void callToString() {
        System.out.println(toString()); // "Child"
        System.out.println(super.toString()); // "Parent"
    }
}

In the case of a call to getClass() , that's a method which returns the class it's called on, and can't be overridden - so while I can see why you'd possibly expect it to return Parent.class , it's still using the same implementation as normal, returning Child . 在调用getClass()的情况下,这是一个返回调用它的类的方法,并且不能被覆盖 - 所以当我看到为什么你可能期望它返回Parent.class ,它仍然在使用与正常相同的实现 ,返回Child (If you actually want the parent class, you should look at the Class API.) (如果您确实需要父类,则应该查看Class API。)

This is often used as part of an override, in fact. 事实上,这经常被用作覆盖的一部分。 For example: 例如:

@Override public void validate() {
    // Allow the parent class to validate first...
    super.validate();
    // ... then perform child-specific validation
    if (someChildField == 0) {
        throw new SomeValidationException("...");
    }
}

The getClass() method returns the class of the object. getClass()方法返回对象的类。 Super & this reference the same object. Super&this引用相同的对象。 So it is not because you reference your object with super that suddenly the object changes class, it remains an instance of the class that was used to instantiate it, hence the getClass() will always return the same Class whether you call it from this or from super. 所以这不是因为你用super引用你的对象,突然对象改变了类,它仍然是用于实例化它的类的实例,因此getClass()将始终返回相同的Class,无论你是从这个调用它还是从超级。

Super is meant to be used to reference an implementation of a method as defined in a super class, it is typically used in those methods that are being overridden in a sub class to call the original behaviour. Super用于引用超类中定义的方法的实现,它通常用在那些在子类中被重写以调用原始行为的方法中。

The super keyword will call an overridden method in the parent. super关键字将在父级中调用重写方法。 Child did not override that method. 孩子没有覆盖那种方法。 So it will faithfully return the correct class which is Child. 所以它将忠实地返回正确的类,即Child。 You do not get an instance of the Parent class this way. 您没有这样获得Parent类的实例。

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

相关问题 Java:从子类调用super.getClass() - Java: Calling super.getClass() from subclass 如何获取父基类对象super.getClass() - How to get the parent base class object super.getClass() 为什么子类中的super.getClass()返回子类名 - why super.getClass() in a subclass returns subclass name Java getClass和超类 - Java getClass and super classes 为什么在Java中这是可能的:this.getClass()。getClass()。getClass()...等 - Why is this possible in Java: this.getClass().getClass().getClass()…etc Java类中的super关键字是否可以编译为this.getClass.getSuperClass()…? - Does super keyword in Java class compiles to this.getClass.getSuperClass()…? 为什么可以在Java中将超类初始化为子类? - Why can a super class be initialized as a child class in Java? 为什么下面给出的代码打印出父类而不是子类的实例变量的值? - Why does the code given below prints value of instance variable of parent class instead of that of child class? 在 Java 中,是否可以使用超级关键字从子 ZA2F2ED4F8EBC2CBB4C21A2 引用父 class 的 static 变量? - In Java , is it possible to refer static variable of parent class from child class using super keyword? 是否可以让子类方法/变量通过父(超级)类运行? (爪哇) - Is it possible to make child class method/variable to run through a parent(Super) Class? (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM