有没有办法拒绝在构造方法中创建对象?
我的观点是设置一个与条件相关的额外参数。例如:
public Test(Rule rule){
if(rule == Rule.Custom)
//DO NOT CREATE
else{
this.rule = rule;
}
}
//WORK IF rule IS Custom
//I want to pass this information to user
public Test(Rule rule, string extra){
if(rule != Rule.Custom)
//DO NOT CREATE
else{
this.rule = rule;
this.extra = extra;
}
}
也许我可以制作工厂方法,但我对解决方法感到好奇。