简体   繁体   English

这是指在子类对象上调用非重写方法时的情况?

[英]this refers to what when a non-overridden method is invoked on a subclass object?

Consider the following code: 考虑以下代码:

class Person {
    String className = "Person";

    void printClassName () {
        System.out.println("I am " + this.className);
        System.out.println("I am " + this.getClass().getSimpleName());
    }
}

class Employee extends Person {
    // intentionally hiding this field
    String className = "Employee";
}

public class App {
    public static void main(String[] args) {
        Employee raghu = new Employee ();
        raghu.printClassName();
    }

}

I have a few questions. 我有几个问题。

  1. When an object of subclass is created, how many objects are actually created? 创建子类的对象时,实际上创建了多少个对象? Only one, extending the properties of superclass by introducing new properties defined in subclass? 通过引入子类中定义的新属性来扩展超类的属性? Or two, the subclass object that we have access to and a superclass object, whose existence is hidden from us? 或两个,我们可以访问的子类对象和一个超类对象,其存在对我们来说是隐藏的?

  2. If two objects are created, which object is in charge when a non-overridden method is invoked on the subclass object? 如果创建了两个对象,则在子类对象上调用一个非重写方法时,哪个对象负责? In other words, what does this refer to inside a non-overridden method? 换句话说,在非重写方法中this指的是什么? The hidden superclass object or the subclass object? 隐藏的超类对象还是子类对象?

  3. If your answer for #2 is the hidden object of superclass, then why the code above prints "I am Employee" for System.out.println("I am " + getClass().getSimpleName()); 如果您对#2的答案是超类的隐藏对象,那么上面的代码为什么为System.out.println("I am " + getClass().getSimpleName());打印"I am Employee" System.out.println("I am " + getClass().getSimpleName()); inside printClassName . printClassName内部。

  4. If your answer for #2 is the object of subclass, then why the first line inside printClassName prints "I am Person" ? 如果您对#2的答案是子类的对象,那么为什么printClassName的第一行printClassName "I am Person"

You declare the variable as type Employee 您将变量声明为Employee类型

this.className

refers to the className in that class. 引用该类中的className

this.getClass().getSimpleName()

this.getClass() returns the class Employee , since that's how you declared the variable, which is why the getSimpleName() returns "Employee" this.getClass()返回Employee类,因为这是您声明变量的方式,因此getSimpleName()返回“ Employee”的原因

The field className in the child class Employee is an extra second field having the same name as the field className in Person; 子类Employee中的className字段是第二个额外字段,其名称与Person中的className相同; this is called shadowing . 这称为阴影 There is no inheritance for fields. 没有字段的继承。

(Probably known.) (可能是已知的。)

class Employee extends Person { // Manner to change the super field
    Employee() {
        className = "Employee";
    }
}
  1. new Employee() creates one object holding two className fields. new Employee()创建一个包含两个className字段的对象。 It calls the super constructor, does the field initialisations, and executes the rest of the constructor code. 它调用超级构造函数,进行字段初始化,并执行其余的构造函数代码。

  2. this always is the sole object, possibly of some child class. this始终是唯一的对象,可能是某些子类的唯一对象。 So a Person.this might actually be an Employee. 因此,Person.This实际上可能是雇员。

  3. and 4. this.className in Person for an Employee object simply will access the Person field, as there is no inheritance for fields. 和4. Employee对象的Person中的this.className仅将访问Person字段,因为这些字段没有继承。 In contrast the method xxx() will call the child most method. 相反,方法xxx()将调用子级大多数方法。

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

相关问题 调用自己的非覆盖方法 - Call own, non-overridden method 访问非重写的超类方法时使用'super'关键字 - Use of 'super' keyword when accessing non-overridden superclass methods 有没有办法在匿名类的定义之外调用未覆盖或未实现的方法? - Is there a way to invoke a non-overridden or non-implemented method in an anonymous class outside of its definition? 使用指向派生对象的基本引用调用的重写方法,在运行时将调用什么? - Overridden method called using a base reference that points to a derived object, what will be invoked at run-time? 如何访问子类中抽象类的重写的非静态方法 - how to access overridden, non static method of abstract class in subclass 非重写方法不显示子类字段 - Non overridden method doesn't show subclass field 当使用空对象引用调用静态方法时会发生什么? - What happens when a static method is invoked using a null object reference? 使用子类中的重写方法 - Use overridden method from subclass JVM JIT可以专门化子类中的非重写方法吗? - Can the JVM JIT specialize non-overridden methods in sub-classes? Tomcat:@Overriden日志过滤器将输出与其非重写变体混合 - Tomcat: @Overriden log filter mixing outputs with its non-overridden variant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM