我可以在另一个构造函数中调用具有不同参数的2个构造函数

时间:2014-07-30 12:55:48

标签: java constructor

我需要在另一个构造函数中调用构造函数2次。 例如:

public class SpinnerUtilityAssociationComparison extends SpinnerUtilityBase {
    static String adminType = "Association";

    public SpinnerUtilityAssociationComparison() {
    }

    public SpinnerUtilityAssociationComparison(ArrayList sourceList, ArrayList asisList, 
            ArrayList targetList, ArrayList columnListSchema_1, ArrayList columnListSchema_2, 
            ArrayList columnListSchema_3, String foldername) throws Exception {
        this(sourceList, asisList, columnListSchema_1, columnListSchema_2, foldername, "Schema1_Schema2");
        this(asisList, targetList, columnListSchema_2, columnListSchema_3, foldername, "Schema1_Schema3");

        GenerateCompareReport(sourceList, asisList, targetList, str + "Spinner" + adminType + "Data_CompareReport.xls");
    }

    public SpinnerUtilityAssociationComparison(ArrayList sourceList, ArrayList asisList, 
            ArrayList columnListSchema_1, ArrayList columnListSchema_2, String foldername, 
            String packageName) throws Exception {

        // String str = foldername+"/Spinner" + adminType +"DataFiles/";
        String str1 = foldername + "/Spinner" + adminType + "DataFiles/Spinner" + adminType + "Data_" + packageName + "/";
        new File(str1).mkdirs();

        String unique1 = "Spinner" + adminType + "Data_Unique1.xls";
        String unique2 = "Spinner" + adminType + "Data_Unique2.xls";
        String delta = "Spinner" + adminType + "Data_Delta.xls";
        String match = "Spinner" + adminType + "Data_Match.xls";
        GenerateUniqueData_1(sourceList, asisList, columnListSchema_1, columnListSchema_2, str1 + unique1);

        GenerateUniqueData_2(sourceList, asisList, columnListSchema_1, columnListSchema_2, str1 + unique2);

        GenerateDeltaData(sourceList, asisList, str1 + delta);

        GenerateMatchData(sourceList, asisList, str1 + match);
    }
}

我必须使用不同的参数调用单个构造函数。 在eclipse中,这给出了错误:构造函数调用必须是构造函数中的第一个语句。 我怎么能做到这一点?

3 个答案:

答案 0 :(得分:4)

  

所以我怎么能做到这一点。

你基本上不能 - 不是你尝试的方式。每个构造函数都链接到同一个类或直接超类中的另一个构造函数。

我建议你创建一个" primary"构造函数 all 您需要的初始化,然后可能间接地链接到它 - 来自所有其他构造函数。如果你在构造函数中进行非初始化工作,那本身就有一种设计气味,你应该重新审视你的设计。

强烈建议您开始遵循Java命名约定。

答案 1 :(得分:0)

编译器回答了这个问题:你不能用这种方式调用两个构造函数。请给客户一个。

我讨厌看到什么都不做的默认构造函数。要么在默认的ctor中做一些合理的事情,要么不提供。

答案 2 :(得分:0)

你不能。编译器只接受一个构造函数调用,它是每个构造函数中的第一个调用。但是,在大多数情况下,您没有看到,但编译器会添加它。

public class MyClass { // is effectively 'public class MyClass extends Object'
    private String value;

    public MyClass() { // default constructor. Is it empty? Then don't add one
         this("hello"); // constructor chaining is possible though. 
    }

    public MyClass(String value) { //
        super(); // can be omitted, for it is a super() call to constructor of Object
        this.value = value; 
    }        
}

public class WrongClass {
    public WrongClass() {
        System.out.println("hello");
        this("hello"); // even this is impossible. It's not the first thing in your constructor
    }

    public WrongClass(String s) {
        super(); // implicitely called
    }
}

但是,在一个构造函数中调用两个构造函数不是主要问题。看起来你只想用一堆(原始类型)ArrayLists做一堆事情。为什么不简单地制作方法呢?这在语义上也更好,因为你没有构造一个对象,但你做了所有这些对象的一些东西。如果你只是调用一个方法,它就会编译并运行:

public SpinnerUtilityAssociationComparison(ArrayList sourceList, ArrayList asisList, 
        ArrayList targetList, ArrayList columnListSchema_1, ArrayList columnListSchema_2, 
        ArrayList columnListSchema_3, String foldername) throws Exception {
    doSomething(sourceList, asisList, columnListSchema_1, columnListSchema_2, foldername, "Schema1_Schema2");
    doSomething(asisList, targetList, columnListSchema_2, columnListSchema_3, foldername, "Schema1_Schema3");

    GenerateCompareReport(sourceList, asisList, targetList, str + "Spinner" + adminType + "Data_CompareReport.xls");
}

public doSomething(ArrayList sourceList, ArrayList asisList, 
        ArrayList columnListSchema_1, ArrayList columnListSchema_2, String foldername, 
        String packageName) throws Exception {
    /* do something with all those lists */
}

然后,你不应该使用构造函数来做事情。您可以使用它们来构造对象,设置和初始化某些值,然后调用这些对象上的方法来实际执行操作。

SpinnerUtilityAssociationComparison utilObject = new SpinnerUtilityAssociationComparison();
doSomething(sourceList, asisList, columnListSchema_1, columnListSchema_2, foldername, "Schema1_Schema2");