小编use*_*758的帖子

构造函数可以不返回新实例吗?

是否有可能让构造函数根据参数决定不创建新实例?例如:

public class Foo{

    public Foo(int n){

        if(n<0){
            //DO NOT make this 
        }
        else{
            //Go ahead and make this instance 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道不可能这样做:

 public class Foo{

     public Foo(int n){

         if(n<0){
             //DO NOT make this 
             this = null; 
         }
         else{
             //Go ahead and make this instance 
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法正确地做同样的事情?

java instantiation

2
推荐指数
1
解决办法
77
查看次数

标签 统计

instantiation ×1

java ×1