是否有可能让构造函数根据参数决定不创建新实例?例如:
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)
有没有办法正确地做同样的事情?