相关疑难解决方法(0)

为什么this()和super()必须是构造函数中的第一个语句?

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

例如:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

Sun编译器说"调用super必须是构造函数中的第一个语句".Eclipse编译器说"构造函数调用必须是构造函数中的第一个语句".

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

public class MySubClass extends MyClass {
    public MySubClass(int a, int b) {
        super(a + b);  // OK
    }
}
Run Code Online (Sandbox Code Playgroud)

这是另一个例子:

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

public class MySubClassA extends MyClass {
    public MySubClassA(Object item) {
        // Create a list …
Run Code Online (Sandbox Code Playgroud)

java constructor

572
推荐指数
8
解决办法
21万
查看次数

在使用Java运行构造函数代码之前是否初始化了字段?

任何人都可以解释以下程序的输出吗?我认为构造函数在实例变量之前被初始化.所以我期待输出为"XZYY".

class X {
    Y b = new Y();

    X() {
        System.out.print("X");
    }
}

class Y {
    Y() {
        System.out.print("Y");
    }
}

public class Z extends X {
    Y y = new Y();

    Z() {
        System.out.print("Z");
    }

    public static void main(String[] args) {
        new Z();
    }
}
Run Code Online (Sandbox Code Playgroud)

java constructor initialization

65
推荐指数
3
解决办法
2万
查看次数

为什么我不能在显式调用构造函数时引用实例方法?

有谁知道为什么你可以static使用this()super()但不是非静态方法引用构造函数的第一行中的方法?

考虑以下工作:

public class TestWorking{
    private A a = null;
    public TestWorking(A aParam){
       this.a = aParam;
    }

    public TestWorking(B bParam)
    {
        this(TestWorking.getAFromB(bParam));
    }

    //It works because its marked static.
    private static A getAFromB(B param){
        A a = new A();
        a.setName(param.getName());
        return a;
    }
}
Run Code Online (Sandbox Code Playgroud)

以下非工作示例:

public class TestNotWorking{
    private A a = null;
    public TestNotWorking(A aParam){
       this.a = aParam;
    }

    public TestNotWorking(B bParam)
    {
        this(this.getAFromB(bParam));
    }

    //This does not work. WHY???
    private A getAFromB(B …
Run Code Online (Sandbox Code Playgroud)

java constructor initialization

36
推荐指数
2
解决办法
2万
查看次数

何时初始化实例变量并分配值?

当doees实例变量初始化时?是在构造函数块完成之后还是之前?

考虑这个例子:

public abstract class Parent {

 public Parent(){
   System.out.println("Parent Constructor");
   init();
 }

 public void init(){
   System.out.println("parent Init()");
 }
}
Run Code Online (Sandbox Code Playgroud)
public class Child extends Parent {

private Integer attribute1;
private Integer attribute2 = null;

public Child(){
    super();
    System.out.println("Child Constructor");
}

public void init(){
    System.out.println("Child init()");
    super.init();
    attribute1 = new Integer(100);
    attribute2 = new Integer(200);
}

public void print(){
    System.out.println("attribute 1 : " +attribute1);
    System.out.println("attribute 2 : " +attribute2);
}
}
Run Code Online (Sandbox Code Playgroud)
public class Tester {

public static void main(String[] args) { …
Run Code Online (Sandbox Code Playgroud)

java inheritance abstract-class object instance

9
推荐指数
1
解决办法
3644
查看次数