Java 这个人身上发生了奇怪的事情

Java 这个人身上发生了奇怪的事情,java,oop,inheritance,subclass,superclass,Java,Oop,Inheritance,Subclass,Superclass,在下面的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.doSomthin

在下面的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
感谢您的帮助。

提前感谢。

学生从父母那里继承年龄,所以年龄和超级之间没有区别。年龄学生从父母那里继承年龄,所以年龄和超级之间没有区别。年龄不,发生的事情是正确的。当您创建一个子类(Student是Person的子类)时,该子类从超类继承所有字段(变量)。但是,只有一组变量:年龄只有一个值,即使它是继承的。换句话说,当一个类继承一个字段时,它不会创建它的新副本-每个学生只有一个副本。

不,发生的事情是正确的。当您创建一个子类(Student是Person的子类)时,该子类从超类继承所有字段(变量)。但是,只有一组变量:年龄只有一个值,即使它是继承的。换句话说,当一个类继承一个字段时,它不会创建它的新副本-每个学生只有一个副本。

在这个源代码中,
this
super
是同一个实例变量,因为您在子类中继承的超类中定义了它


当你创建你的学生时,你将其初始化为22,就是这样

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


当你创建你的学生时,你将其初始化为22,就是这样

您正在
学生
类中设置
年龄
,但家长是声明
年龄
的人,他们共享相同的变量-因此,修改该值是有意义的。但是,重写的方法会有所不同。

您正在
Student
类中设置
age
,但是父对象是声明
age
的对象,并且它们共享相同的变量-因此,修改该值是有意义的。但是,重写的方法会有所不同。

这是您所期望的。您尚未声明Student的“age”成员,因此this.age自然引用在超类中定义的“age”

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


这是你所期望的。您尚未声明Student的“age”成员,因此this.age自然引用在超类中定义的“age”

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


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

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

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

年龄是超级阶级的一个领域。在子类的构造函数中,当您说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);
    }
}  

不,那是正确的。在构造函数中,您正在重写超类的年龄。您可以这样做:

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

不,那是正确的。在构造函数中,您正在重写超类的年龄。您可以这样做:

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