简体   繁体   English

从子类对象使用超类的getClass方法

[英]Using a Superclass's getClass method from a Subclass object

I have a total of three classes. 我一共有三节课。 Main, Animal (Superclass) and Cat (Subclass). 主要,动物(超类)和猫(子类)。 When I tried to call the whatIsTheClass() method, I didn't understand the output "Cat". 当我尝试调用whatIsTheClass()方法时,我不理解输出“ Cat”。 I was expecting to see the output "Animal". 我期待看到输出“动物”。 The reason why I expected the output "Animal" is because I believe the method whatIsTheClass() is called from an Animal object and not a Cat object as the Subclass does not contain this method. 之所以期望输出“动物”,是因为我相信从动物对象而不是猫对象调用方法whatIsTheClass(),因为子类不包含此方法。

Main class: 主班:

package com.example.java;

public class Main {

 public static void main(String[] args) {
     Animal cat = new Cat();
 }

}

Cat class: 猫类:

package com.example.java;

public class Cat extends Animal {

 public Cat() {
     super();
     whatIsTheClass();
 }

}

Animal class: 动物类:

package com.example.java;

public class Animal {

 public void whatIsTheClass() {
     System.out.println(this.getClass().getSimpleName());
 }

}

My understanding is that in inheritance, the Subclass does not copy the methods from the Superclass. 我的理解是,在继承中,子类不会从超类复制方法。 If the called to method is not defined in the Subclass object, it will look for the called to method in the Superclass object. 如果在Subclass对象中未定义被调用的方法,它将在Superclass对象中查找被调用的方法。 And if the called to method is defined in the Superclass object, it will be called from there. 并且,如果在Superclass对象中定义了to方法的调用,则会从那里调用它。 In this case, because the Subclass does not define it's own whatIsTheClass() method, it must have used the whatIsTheClass() method that is defined in the Superclass object. 在这种情况下,因为子类没有定义它自己的whatIsTheClass()方法,所以它必须使用在超类对象中定义的whatIsTheClass()方法。 But if whatIsTheClass() was called from the Superclass object, why did it return the name of the Subclass rather than the name of the Superclass? 但是,如果从超类对象调用whatIsTheClass(),为什么它返回子类的名称而不是超类的名称?

The method whatIsTheClass may be defined in Animal , but in which class's code getClass() is called makes no difference. 方法whatIsTheClass可以在Animal中定义,但是调用类的代码getClass()则没有区别。 It's all about the runtime type of the object, as stated in the Javadocs for getClass() : Javadocs中对getClass()所述,这与对象的运行时类型有关:

Returns the runtime class of this Object . 返回此Object的运行时类。

The runtime type of the object specified by cat is Cat . cat指定的对象的运行时类型为Cat

This is also why no class has to override getClass() just to return the proper Class object. 这也是为什么没有任何类仅为了返回正确的Class对象而必须重写getClass() (The method getClass() is final anyway.) (方法getClass()仍然是final的。)

When an object of Cat class is created, a copy of the all methods and fields of the superclass acquire memory in this object. 创建Cat类的对象时,超类的所有方法和字段的副本将获取该对象中的内存。 That is why, by using the object of the subclass you can also access the members of a superclass. 这就是为什么通过使用子类的对象,您还可以访问超类的成员。 Please note that during inheritance only object of subclass is created, not the superclass. 请注意,在继承期间仅创建子类的对象,而不创建超类的对象。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM