[英]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. 我有几个问题。
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?
或两个,我们可以访问的子类对象和一个超类对象,其存在对我们来说是隐藏的?
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? 隐藏的超类对象还是子类对象?
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
内部。
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";
}
}
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. 它调用超级构造函数,进行字段初始化,并执行其余的构造函数代码。
this
always is the sole object, possibly of some child class. this
始终是唯一的对象,可能是某些子类的唯一对象。 So a Person.this might actually be an Employee. 因此,Person.This实际上可能是雇员。
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.