此人发生了一些奇怪的事情

发布于 2024-11-15 17:44:39 字数 540 浏览 4 评论 0原文

下面的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

最终幸福 2024-11-22 17:44:39

Age是超类中的一个字段。在子类的构造函数中,当你说 this.age = 22 时,你正在更新超类中的实例变量。

尝试以下操作...我手边没有编译器,但我认为它可能会达到您的预期。

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);
    }
}  

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.

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);
    }
}  
木有鱼丸 2024-11-22 17:44:39

这正如你所期望的那样。您还没有声明 Student 的“age”成员,因此 this.age 自然引用超类中定义的“age”。

下面的代码将提供您所期望的行为(尽管像这样隐藏变量通常是一个非常糟糕的主意)。

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);
    }
}

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).

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);
    }
}
如歌彻婉言 2024-11-22 17:44:39

不,这是正确的。在构造函数中,您将覆盖超类的年龄。你可以这样做:

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, 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
    }
}  
盗心人 2024-11-22 17:44:39

学生从父母继承年龄,因此年龄和超级年龄之间没有区别

Student inherits age from parent, so there is no difference between age and super.age

倾城泪 2024-11-22 17:44:39

不,正在发生的事情是正确的。当您创建子类(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.

靑春怀旧 2024-11-22 17:44:39

在此源代码中,thissuper 是相同的实例变量,因为您在超类中定义它并在子类中继承。

当您创建 Student 时,您将其初始化为 22,仅此而已。

In this source, this and super 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.

紫竹語嫣☆ 2024-11-22 17:44:39

没什么奇怪的,它的行为正确。 Student 类没有私有变量 age,这会覆盖parents 变量。

Nothing strange, it's behaving correctly. Class Student doesn't have a private variable age, which would overwrite parents variable.

还如梦归 2024-11-22 17:44:39

您在 Student 类中设置 age,但父类是声明 age 的类,并且它们共享相同的变量 - 因此,它使得感觉该值已被修改。然而,重写方法会有所不同。

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. Overriden methods would be different, however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文