简体   繁体   English

一个奇怪的事情发生在人身上

[英]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. 在这个源代码中, thissuper是相同的实例变量,因为你在子类中继承的超类中定义它。

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.

相关问题 Android - 从资产中绘制图像,发生了一些奇怪的事情 - Android - Drawing an image from assets, something weird is happening 奇怪的事情正在发生。 每次在笔记本电脑上启动Jenkins时,笔记本电脑中的Java 8 JDK文件夹都会消失。 就是我吗 - Something weird is happening. Everytime I start Jenkins on my laptop, the Java 8 JDK folder in my laptop vanishes. Is that just me? ScheduledExecutorService有点奇怪 - Something weird with ScheduledExecutorService 第一人称摄影机在动眼时出现怪异行为 - First person camera weird behaviour on moving the eye 与 @OneToMany 关系在 Java Spring 中发生的奇怪的开始 - Weird inception happening in Java Spring with @OneToMany relation 第一行发生了一些事情 - 选项卡功能 - Something happening with first line - tab function 比较两个列表时发生奇怪的情况(常规) - Weird occurrence happening when comparing two list (groovy) 有没有更有效的方法来使某件事发生的可能性增加x%? - Is there a more efficient way to create an x% chance of something happening? JPanel和java g.fillOval()方法发生了一些奇怪的事情 - Something strange happening with JPanel and java g.fillOval() method 索引排序。 确实很奇怪 - Index sort. Something really weird is going on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM