简体   繁体   English

在JAVA中是否有在超类的构造函数之前执行子类的构造函数的方法?

[英]Is there anyway in JAVA to execute Sub Class's Constructor before the Super Class's Constructor?

class Super
{
    Super()
    {
        System.out.println("This is Super Constructor");
    }
}
class Sub extends Super
{
    Sub()
    {
        //super() is automatically added by the compiler here!
        System.out.println("This is Sub Constructor");
        //super(); I can't define it here coz it needs to be the first statement!
    }
}
class Test
{
    public static void main(String...args)
    {
        Sub s2=new Sub();
    }
}  

Output : 输出:
This is Super Constructor 这是超级构造函数
This is Sub Constructor 这是子构造函数

Anyway to do so? 反正这样做吗?
Or you can't access Sub() before Super()? 还是您不能在Super()之前访问Sub()?
I know Super Class or Inherited Classes are initialized first then the sub classes, just doing this for learning purposes only! 我知道超级类或继承的类首先被初始化,然后是子类,仅出于学习目的而这样做!

In a constructor, the compiler will always add a call to super() for you if you didn't provide this call by yourself. 在构造函数中,如果您自己未提供此调用,则编译器将始终为您添加对super()的调用。

If you look with a decompiler, your Sub constructor will look like this: 如果使用反编译器,则Sub构造函数将如下所示:

Sub()
{
    super();
    System.out.println("This is Sub Constructor");
}

So no, it is not possible. 所以不,这是不可能的。

It will always call base class constructor ( parent ) first and then the initiating class ( child ). 它将始终始终先调用基类的构造函数(parent),然后再调用发起的类(child)。 In your case super() will execute first. 在您的情况下, super()将首先执行。

good read: Which executed first, The parent or the child constructor? 好读: 父或子构造函数是第一个执行的?

As other answers say not possible. 正如其他答案所说的那样。 Not sure why you are trying to do this but better rethink your implementation, as you always would want to make sure inherited field are initialised first as child class implementation could depend on it. 不确定为什么要这样做,但最好重新考虑一下实现,因为您总是希望确保继承的字段首先被初始化,因为子类实现可能依赖于它。

As said, the super constructor is always called. 如前所述,超级构造函数总是被调用。 Maybe you should rewrite your questions and explain, why you need that. 也许您应该重写您的问题并说明原因。

Heres a simple answer. 这是一个简单的答案。 The concept of Inheritance is about the IS-A relationship. 继承的概念是关于IS-A关系的。 So technically, Dog class is a child of Animal Class . 因此从技术上讲,Dog类是Animal Class的孩子。 . . upon creating the object, Dog class is first an ANIMAL before it becomes classified as a DOG 创建对象后,Dog类首先是一种动物,然后才被归类为DOG

... hope this clearifies things . ...希望这样可以弄清楚事情。 . so its not possible to do it the way you asked 所以不可能按照你的要求去做

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

相关问题 超类子类构造函数java - Super class Sub class Constructor java 编译器在调用基类构造函数之前会在子类构造函数中寻找** super **关键字吗? - Does the compiler look for a **super** keyword in child class constructor before calling the it's base class constructor? 线程初学者:在子类(扩展Thread类)构造函数中使用super()方法有什么意义? - Threads Beginner: What's the point in using a super() method within the sub class's(extending Thread class) constructor? 如何在Java中从超级类继承子类的构造方法 - how to inherit Part of Constructor from super class to sub class in java Java NullPointerException在构造函数的类中 - Java NullPointerException In the constructor's class Java (Selenium) - 用于初始化子类的超类构造函数 - Java (Selenium) - super class constructor to initialise sub classes Java中超类的构造方法 - Relating Constructor of Super Class in java Java:继承的类构造函数正在调用Super类 - Java: Inherited class constructor is calling Super class 无法将参数传递给super();在类的构造函数中扩展RealmBaseAdapter(Realm) - Cannot pass parameters to super(); in a class's constructor that extends RealmBaseAdapter (Realm) 为什么在超类的构造函数之后初始化成员对象? - Why are member objects initialized after the super class's constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM