此人发生了一些奇怪的事情
下面的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()
}
}
为什么super.age的值为22,和子类的age值一样,不是应该是18吗;
如有任何帮助,我们将不胜感激。
提前致谢。
In the following java code
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;
Any help is appreciated.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Age是超类中的一个字段。在子类的构造函数中,当你说 this.age = 22 时,你正在更新超类中的实例变量。
尝试以下操作...我手边没有编译器,但我认为它可能会达到您的预期。
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.
Try the following ... I dont have a compiler handy but i think it might do what you are expecting.
这正如你所期望的那样。您还没有声明 Student 的“age”成员,因此 this.age 自然引用超类中定义的“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.
The code below will provide the behaviour you are expecting (although shadowing variables like that is often a very bad idea).
不,这是正确的。在构造函数中,您将覆盖超类的年龄。你可以这样做:
No, that is correct. In the constructor, you are overriding the super class's age. You could instead do something like this:
学生从父母继承年龄,因此年龄和超级年龄之间没有区别
Student inherits age from parent, so there is no difference between age and super.age
不,正在发生的事情是正确的。当您创建子类(Student 是 Person 的子类)时,该子类将从超类继承所有字段(变量)。然而,只有一组变量:年龄只有一个值,即使它是继承的。换句话说,当一个类继承一个字段时,它不会创建它的新副本 - 每个学生只有一个副本。
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. 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.
在此源代码中,
this
和super
是相同的实例变量,因为您在超类中定义它并在子类中继承。当您创建 Student 时,您将其初始化为 22,仅此而已。
In this source,
this
andsuper
are the same instance variable because you define it in the super class an inherited in the subclass.When you create your Student you initilize it to 22 and that's it.
没什么奇怪的,它的行为正确。
Student
类没有私有变量age
,这会覆盖parents 变量。Nothing strange, it's behaving correctly. Class
Student
doesn't have a private variableage
, which would overwrite parents variable.您在
Student
类中设置age
,但父类是声明age
的类,并且它们共享相同的变量 - 因此,它使得感觉该值已被修改。然而,重写方法会有所不同。You're setting
age
in yourStudent
class, but the parent is the one declaringage
and they share the same variable - therefore, it makes sense that the value was modified. Overriden methods would be different, however.