简体   繁体   English

为什么我们可以在子类构造函数中调用超类方法?

[英]Why can we call super class methods in a subclass constructor?

I need a better explanation for this scenario.对于这种情况,我需要更好的解释。 I do not know whether this is a feature provided by Java language itself不知道是不是Java语言本身提供的特性

//super class
public class Student {
    public Student(){}

    public void display(){
        System.out.println("Hello from Student");
    }       

}

//Subclass
public class SeniorStudent extends Student {

    public SeniorStudent(){
        super();
        display();
    }

    public static void main(String[] args) {
        SeniorStudent st=new SeniorStudent();
    }
}

When I run the program, display() method is invoked.当我运行程序时,将调用display()方法。 What is the logic going on here?这里发生的逻辑是什么?

You call display method in the child class, but if you don't rewrite the display() method it automatically call the parent one.您在子类中调用 display 方法,但如果您不重写 display() 方法,它会自动调用父类。 The construct of the class is the first portion being executed in a class.类的构造是在类中执行的第一部分。 The logic under your piece of software is this:您的软件下的逻辑是这样的:

  • Start executing main method where you create a new instance of seniorStudent开始执行 main 方法,在其中创建一个 SeniorStudent 的新实例
  • SeniorStudent constructor has been called and start to execute SeniorStudent 构造函数已被调用并开始执行
  • The first thing the constructor of SeniorStudent do is to call the parent constructor (which do nothing) SeniorStudent 的构造函数做的第一件事就是调用父构造函数(它什么都不做)
  • Then after have executed whole parent constructor resume to execute SeniorStudent constructor where you call display method but SeniorStudent class doesn't have any display method so call it's parent display method.然后在执行了整个父构造函数之后恢复执行 SeniorStudent 构造函数,您在其中调用 display 方法但是 SeniorStudent 类没有任何 display 方法所以调用它的父 display 方法。

So if you want to print in console "Hello from senior student" you have to change the senior student class like this所以如果你想在控制台中打印“Hello from Senior student”,你必须像这样改变高年级学生班级

    public class SeniorStudent extends Student {

    public SeniorStudent(){
        super();
        display();
    }
    public void display(){
      system.out.println("Hello from senior Student");
    }

    public static void main(String[] args) {
        SeniorStudent st=new SeniorStudent();
    }
}

And if you put super.display() in the display method from SeniorStudent class you call parent display method and then execute senior student display method so the output would be something like如果您将 super.display() 放在 SeniorStudent 类的显示方法中,您将调用父显示方法,然后执行高年级学生显示方法,因此输出将类似于

"Hello from Student"
"Hello from Senior Student"

Hope I was enought clear, but if you have some problem i'm glad to help you希望我说得够清楚了,但是如果您有问题,我很乐意为您提供帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我们不能使用父类的引用变量来访问其子类的方法(方法在父类中不可用)? - why cant we use a reference variable of super class to access methods of its subclass(methods not available in super class)? 对于Serializable超类,如果我们对子类进行序列化,为什么调用super构造函数 - For a Serializable super class, if we serialize the subclass, why does the super constructor is invoked 为什么我们在构造函数中调用super() - Why do we call super() in constructor 为什么子类必须在超类中调用no args构造函数? - why does the subclass have to invoke the no args constructor in the super class? 为什么子类对象不能拥有超类构造函数 - why cant subclass objects hold super class constructor 我们可以在Java多级继承中从第二个子类调用超类构造函数吗 - Can we call a super class constructor from 2nd sub class in multilevel inheritance in java 为什么在构造函数中调用 super()? - Why call super() in a constructor? 子类的Java解决方法被强制在构造函数中调用super()? - Java workaround for subclass forced to call super() in constructor? 为什么我们需要 super 关键字来调用默认的接口方法? - Why do we need super keyword to call default interface methods? 从子类调用超类构造函数 - Calling super class constructor from subclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM