为什么this()和super()必须是构造函数中的第一个语句? [英] Why does this() and super() have to be the first statement in a constructor?

查看:267
本文介绍了为什么this()和super()必须是构造函数中的第一个语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java要求如果在构造函数中调用this()或super(),它必须是第一个语句。为什么?

Java requires that if you call this() or super() in a constructor, it must be the first statement. Why?

例如:

public class MyClass {
    public MyClass(int x) {}
}

public class MySubClass extends MyClass {
    public MySubClass(int a, int b) {
        int c = a + b;
        super(c);  // COMPILE ERROR
    }
}

Sun编译器说超级必须是构造函数中的第一个语句。 Eclipse编译器说构造函数调用必须是构造函数中的第一个语句。

The Sun compiler says "call to super must be first statement in constructor". The Eclipse compiler says "Constructor call must be the first statement in a constructor".

然而,你可以通过重新安排代码来解决这个问题: / p>

However, you can get around this by re-arranging the code a little bit:

public class MySubClass extends MyClass {
    public MySubClass(int a, int b) {
        super(a + b);  // OK
    }
}

这是另一个例子:

public class MyClass {
    public MyClass(List list) {}
}

public class MySubClassA extends MyClass {
    public MySubClassA(Object item) {
        // Create a list that contains the item, and pass the list to super
        List list = new ArrayList();
        list.add(item);
        super(list);  // COMPILE ERROR
    }
}

public class MySubClassB extends MyClass {
    public MySubClassB(Object item) {
        // Create a list that contains the item, and pass the list to super
        super(Arrays.asList(new Object[] { item }));  // OK
    }
}

在调用super之前停止执行逻辑。

So, it is not stopping you from executing logic before the call to super. It is just stopping you from executing logic that you can't fit into a single expression.

有类似的规则调用 this()

There are similar rules for calling this(). The compiler says "call to this must be first statement in constructor".

为什么编译器有这些限制?你可以给一个代码示例,如果编译器没有这个限制,会发生什么不好?

Why does the compiler have these restrictions? Can you give a code example where, if the compiler did not have this restriction, something bad would happen?

推荐答案

父类'构造函数需要在子类'构造函数之前调用。这将确保如果您在构造函数中对父类调用任何方法,父类已经正确设置。

The parent class' constructor needs to be called before the subclass' constructor. This will ensure that if you call any methods on the parent class in your constructor, the parent class has already been set up correctly.

您尝试做什么,通过args到超级构造函数是完全合法的,你只需要在你做的时候构造这些参数,或者将它们传递给你的构造函数,然后将它们传递给 super

What you are trying to do, pass args to the super constructor is perfectly legal, you just need to construct those args inline as you are doing, or pass them in to your constructor and then pass them to super:

public MySubClassB extends MyClass {
        public MySubClassB(Object[] myArray) {
                super(myArray);
        }
}



如果编译器没有强制这样做,你可以这样做:

If the compiler did not enforce this you could do this:

public MySubClassB extends MyClass {
        public MySubClassB(Object[] myArray) {
                someMethodOnSuper(); //ERROR super not yet constructed
                super(myArray);
        }
}

类具有默认构造函数,通过编译器自动插入对super的调用。因为Java中的每个类都继承自 Object ,对象构造函数必须以某种方式调用,并且必须首先执行。编译器自动插入super()允许这样做。强制执行super以首先出现的构造函数体将按照正确的顺序执行:Object - > Parent - > Child - > ChildOfChild - > SoOnSoForth

In cases where a parent class has a default constructor the call to super is inserted for you automatically by the compiler. Since every class in Java inherits from Object, objects constructor must be called somehow and it must be executed first. The automatic insertion of super() by the compiler allows this. Enforcing super to appear first, enforces that constructor bodies are executed in the correct order which would be: Object -> Parent -> Child -> ChildOfChild -> SoOnSoForth

这篇关于为什么this()和super()必须是构造函数中的第一个语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆