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

查看:147
本文介绍了为什么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编译器说呼叫super必须是构造函数中的第一个语句。 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".

但是,您可以通过重新安排代码来解决这个问题:

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()有类似的规则/ code>。编译器说调用它必须是构造函数中的第一个语句。

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到超级构造函数是完全合法的,你只需要像你一样构建那些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);
        }
}

如果父母 class有一个默认构造函数,由编译器自动为你插入对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天全站免登陆