[英]Something weird is happening to the Person
In the following java code 在以下java代码中
public class Person {
int age = 18;
}
class Student extends Person {
public Student() {
this.age = 22;
}
public static void main(String[] args) {
Student student = new Student();
student.doSomthing();
}
void doSomthing() {
System.out.println(this.age);
System.out.println(super.age);// Here is something weird, at least for me till rightNow()
}
}
Why the super.age value is 22 , the same value as the sub-class's age value, Isn't it supposed to be 18; 为什么super.age值为22,与子类的年龄值相同,是不是应该是18;
Any help is appreciated. 任何帮助表示赞赏。
Thanks in advance. 提前致谢。
Age is a field in the super class. 年龄是超类中的一个领域。 In the constructor of the subclass, when you say this.age = 22, you are updating the instance variable in the super class. 在子类的构造函数中,当你说this.age = 22时,你正在更新超类中的实例变量。
Try the following ... I dont have a compiler handy but i think it might do what you are expecting. 尝试以下...我没有一个方便的编译器,但我认为它可能会做你期望的。
public class Person {
int age = 18;
}
class Student extends Person {
int age; // Hides the super variable
public Student() {
this.age = 22;
}
public static void main(String[] args) {
Student student = new Student();
student.doSomthing();
}
void doSomthing() {
System.out.println(this.age);
System.out.println(super.age);
}
}
this is behaving as you would expect. 这表现得像你期望的那样。 You haven't declared an 'age' member of Student, so this.age naturally references 'age' defined in the superclass. 你还没有宣布学生的“年龄”成员,所以this.age自然地引用了超类中定义的“年龄”。
The code below will provide the behaviour you are expecting (although shadowing variables like that is often a very bad idea). 下面的代码将提供您期望的行为(尽管像这样的阴影变量通常是一个非常糟糕的主意)。
public static class Person {
int age = 18;
}
public static class Student extends Person {
int age = 18;
public Student() {
this.age = 22;
}
void doSomthing() {
System.out.println(this.age);
System.out.println(super.age);
}
}
No, that is correct. 不,那是对的。 In the constructor, you are overriding the super class's age. 在构造函数中,您将覆盖超类的年龄。 You could instead do something like this: 你可以做这样的事情:
public class Person {
public int getAge() {
return 18;
}
}
class Student extends Person {
public Student() {
}
@Override
public int getAge() {
return 22;
}
public static void main(String[] args) {
Student student = new Student();
student.doSomthing();
}
void doSomthing() {
System.out.println(this.getAge()); //22
System.out.println(super.getAge()); //18
}
}
学生从父母那里继承了年龄,所以年龄和超级之间没有区别
No, what is happening is correct. 不,发生了什么是正确的。 When you create a subclass (Student is a subclass of Person), that subclass inherits all of the fields (variables) from the superclass. 当您创建子类(Student是Person的子类)时,该子类将继承超类中的所有字段(变量)。 However, there is only one set of variables: there is only one value for age, even though it is inherited. 但是,只有一组变量:年龄只有一个值,即使它是继承的。 In other words, when a class inherits a field, it doesn't create a new copy of it - there is only one copy per student. 换句话说,当一个类继承一个字段时,它不会创建一个新的副本 - 每个学生只有一个副本。
In this source, this
and super
are the same instance variable because you define it in the super class an inherited in the subclass. 在这个源代码中, this
和super
是相同的实例变量,因为你在子类中继承的超类中定义它。
When you create your Student you initilize it to 22 and that's it. 当你创建学生时,你将它初始化为22,就是这样。
Nothing strange, it's behaving correctly. 没什么奇怪的,它的行为正确。 Class Student
doesn't have a private variable age
, which would overwrite parents variable. Class Student
没有私有变量age
,它会覆盖parent变量。
You're setting age
in your Student
class, but the parent is the one declaring age
and they share the same variable - therefore, it makes sense that the value was modified. 您在Student
类中设置age
,但父项是声明age
的父项,它们共享相同的变量 - 因此,修改该值是有道理的。 Overriden methods would be different, however. 然而,Overriden方法会有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.