名为anthor的java构造函数重写了one';正在实施的计划

名为anthor的java构造函数重写了one';正在实施的计划,java,android,Java,Android,下面的代码无法工作,我不知道原因,为什么会这样?如果我能找到在另一个重写的构造函数中调用构造函数的方法 尽管如此,我知道可以有一个解决方案,将公共init代码放在类函数中。所以,请原谅我这个罪恶的问题 Public AClass extends BClass { AClass(Context c) { super(c); //below has common init code //init data and some operations

下面的代码无法工作,我不知道原因,为什么会这样?如果我能找到在另一个重写的构造函数中调用构造函数的方法

尽管如此,我知道可以有一个解决方案,将公共init代码放在类函数中。所以,请原谅我这个罪恶的问题

Public AClass extends BClass {
    AClass(Context c) {
       super(c);
       //below has common init code
       //init data and some operations
      // other operations

    }

    AClass(Context c, Attr a) {
       super(c, a);
       this(c)  // error
    }
}
而且,下面也不行

AClass(Context c, Attr a) : this(c) {
   super(c, a);
}

非常感谢您的帮助。

您不能在同一个构造函数定义中同时调用
super()
(超级类构造函数)和
this()
(重载构造函数)

在java语言规范中,必须在构造函数定义的第一行调用超类构造函数或调用其他重载构造函数。这清楚地表明你不能一个接一个地给他们两个打电话

理想情况下,在这种情况下,您必须调用重载构造函数,这最终会调用超类构造函数


有关详细信息,请参阅。

在构造函数的同一定义中,不能同时调用
super()
(超级类构造函数)和
this()
(重载构造函数)

在java语言规范中,必须在构造函数定义的第一行调用超类构造函数或调用其他重载构造函数。这清楚地表明你不能一个接一个地给他们两个打电话

理想情况下,在这种情况下,您必须调用重载构造函数,这最终会调用超类构造函数


有关详细信息,请参阅。

根据B类的构造函数所做的工作,以下操作可能会起作用:

public AClass extends BClass {
    AClass(Context c) {
        this(c, (Attr) null);
        // below has common init code
        // init data and some operations
        // other operations
    }

    AClass(Context c, Attr a) {
        super(c, a);
    }
}

一个构造函数只能调用一个构造函数。您不能调用
this
(另一个构造函数)和
super
(超类的构造函数),也不能多次调用
this
super
。您只能将它们作为构造函数的第一条语句调用。

根据类的构造函数所做的工作,以下操作可能会起作用:

public AClass extends BClass {
    AClass(Context c) {
        this(c, (Attr) null);
        // below has common init code
        // init data and some operations
        // other operations
    }

    AClass(Context c, Attr a) {
        super(c, a);
    }
}
一个构造函数只能调用一个构造函数。您不能调用
this
(另一个构造函数)和
super
(超类的构造函数),也不能多次调用
this
super
。您只能将它们作为构造函数的第一个语句调用