Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从另一个构造函数调用空构造函数?_Java_Constructor - Fatal编程技术网

Java 如何从另一个构造函数调用空构造函数?

Java 如何从另一个构造函数调用空构造函数?,java,constructor,Java,Constructor,我有一些这样的代码: public class Foo { private int x; public Foo() { } public Foo(int x) { try { //do some initialisation stuff like: this.x = x; } catch(Exception ge){ //call empty

我有一些这样的代码:

public class Foo {
    private int x;

    public Foo() {
    }

    public Foo(int x) {
        try {
            //do some initialisation stuff like:
            this.x = x;
        }
        catch(Exception ge){
            //call empty constructor not possible
            //this();
            //this.EMPTY();
            //Foo();
        }
    }

    public static final Foo EMPTY = new Foo();
}
我想知道是否有可能实现这样的目标(我知道调用另一个构造函数必须是构造函数中的第一条语句)。
我在这里四处查看了一下,但没有发现任何类似的东西,这让我相信,也许,我应该处理实例化方法中的错误逻辑。

一般来说,调用代码不是很好的做法,而调用代码可能会更糟,甚至会完全抑制调用方的异常。但是,您可以做的是重构代码,以便将无参数构造函数的“默认”初始化移动到助手方法中,然后可以从第二个构造函数中的异常处理程序调用该方法:

public class Foo {
    private int x;

    public Foo() {
      doDefaultInitialize();
    }

    public Foo(int x) {
        try {
          // dodgy code which could throw
        }
        catch(Exception ge){
          doDefaultInitialize();        
        }
    }

    private void doDefaultInitialize() {
       // Fallback initialization goes here
       x = 42;
    }
}        

只需在构造函数的catch块中执行任何操作。它应该能按你的要求工作。 然而,一定要看看这一点,以选择正确的方法来解决您的问题


此外,如果您正在进行任何默认初始化,则遵循@StuartLC所述的方法,只需更改执行顺序:

public class Foo {

    Integer i;
    public Foo() {
        System.out.println("Empty constructor invoked");
    }

    public Foo(Integer i) {

        this(); //can be omitted

        try {
            System.out.println("i initialized to : "+i.toString());

        } catch (Exception ex) {

            System.out.println("i NOT initialized ");
        }
    }


    public static void main(String[] args) {

        new Foo(); //prints: Empty constructor invoked

        new Foo(5);//prints: Empty constructor invoked
                   //prints: i initialized to : 5

        new Foo(null);//prints: Empty constructor invoked
                   //prints: i NOT initialized 
    }
}
正如你所说

调用另一个构造函数必须是 建造师

当我需要这种行为时,我通常使用两种解决方案:

  • 创建一个init函数并从两个位置调用它:

    public class Foo {
        private int x;
    
        public Foo() {
            init();
        }
    
        public Foo(int x) {
            try {
                //do some initialisation stuff like:
                this.x = x;
            }
            catch(Exception ge){
                init();
            }
        }
    
        private init() {
            //Do the default initialization here...
        }
    
        public static final Foo EMPTY = new Foo();
    }
    
  • 创建一个静态函数来初始化对象并返回它

    public class Foo {
        private int x;
    
        private Foo() {
            this.x = 42;
        }
    
        private Foo(int x) throws Exception {
            //do some initialization stuff like:
            this.x = x;
        }
    
        public static Foo getNewInstance(int x) {
            try {
                return new Foo(x);
            } catch (Exception e) {
                return new Foo();
            }
        }
    
        public static final Foo EMPTY = getNewInstance();
    }
    

  • 你想达到什么目标?i、 e.为什么要调用一个空构造函数?不,您只能调用另一个构造函数作为构造函数中的第一个调用。@WillemVanOnsem,尽管非常感谢。@OliverCharlesworth如果对象初始化失败,我想返回一个空对象。这样我可以简化初始化中的错误检查代码method@DarkStar1:你所说的空对象是什么意思?他们为什么要这么做?他们已经得到了他们想要的东西,而没有做任何该死的事情。哈?如果他们有他们所需要的,他们就不必在这里询问。我想你或我都不明白这个问题。这种方法唯一的问题是,如果在
    try
    部分,他会初始化2个或更多的成员。异常可以发生在中间,留下一些具有默认值的成员和一些具有重写值的成员。