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

查看:41
本文介绍了为什么 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() 也有类似的规则.编译器说调用 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 传递给超级构造函数是完全合法的,你只需要在你正在做的时候内联构造这些 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 首先出现,强制构造函数体以正确的顺序执行,即:对象 ->父级 ->孩子 ->ChildOfChild ->不久之后

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天全站免登陆