我们可以中断在构造函数中创建对象吗
你能帮我一下吗? 我有一个想法,但不知道如何实现。
所以问题是: 我们可以中断在构造函数中创建对象吗 即
//Code
SomeClass someClass = new SomeClass(someCriteria);
,如果 someCriteria 不满足我们的要求,我们不应该创建对象,而应该返回 null,而不是新对象。
可以用C#实现吗?
Could you help me please.
I have one idea but don't know how can I implement it.
So the question is:
Can we interrupt creating an object in constructor
i.e.
//Code
SomeClass someClass = new SomeClass(someCriteria);
So if someCriteria doesn't answer on our requirements we shouldn't create an object and should return null, instead of new object.
Is it possible to implement it in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最好的方法是工厂类,但如果你的类很小,你可以使用这个:
Best way is a factory class but if your class is so small you can use this:
您可能想要使用一个工厂类,该工厂类创建 SomeClass 的实例,如果 someCriteria 无效,则返回 null。
You may want to use a factory class that creates instances of SomeClass returning null if the someCriteria is invalid.
检查构造函数参数是否有效是很常见的。如果没有,您通常会抛出异常。
我还阅读了一个很好的建议,提供用于验证构造函数参数的静态方法。这使得类的用户能够检查他要在构造函数中传递的内容是否会成功。那些确定参数没问题的人(他们进行了某种验证)将直接使用构造函数。
还要考虑类的用户可能会使用 null 而不是对象(如果使用了某种工厂类)来做什么。这通常会导致下一行出现 NullPointerException 吗?尽快停止错误的事情通常是个好主意,在这种情况下抛出异常并完成。这是比返回 null 更干净的解决方案,如果有人真的想要(这绝对不是最佳实践),他仍然可以捕获此异常......
It is quite usual that you check the constructor parameters if they are valid. If not, you usually throw an exception.
I also read a nice advice to provide static methods for validating constructor parameters. This enables the user of your class to check if whatever he is going to pass in the constructor will succeed or not. Those who are certain the parameters are ok (they made some sort of their validation) will go directly with the constructor.
Also consider what the user of your class will possibly do with null instead of the object (if some sort of factory class was used). Would this typically result in an NullPointerException on the next line? It's usually good idea to stop the wrong things as soon as possible, in this case throw the exception and finish. It is cleaner solution than returning null and if someone really wants to (this definetly won't be best practise) he can still catch this exception ...
如果构造函数的参数无效,请考虑抛出 ArgumentException 或其后代类之一(例如ArgumentOutOfRangeException)。
If the parameters to your constructor are invalid, consider throwing ArgumentException or one of its descendant classes (e.g.
ArgumentOutOfRangeException
).new
构造保证将返回一个对象(或抛出一个异常)。因此,正如 bleeeah 所建议的,工厂或类似的概念将允许您应用您的逻辑。The
new
construct guarantees that an object will be returned (or an exception thrown). So as bleeeah recommended, a factory or similar concept will allow you to apply your logic.那是可能的。另一种方法是在创建对象之前进行检查。就像这样
希望这有帮助。
That would be possible. Another way would be to put your checking in before you create the object. Like so
Hope this helps.
不,这是不可能直接进行的。
您可以引发异常并添加所需的代码来检查异常并将 null 分配给您的变量。
更好的解决方案是使用一个工厂,如果某些条件失败,该工厂将返回 null。
No it is not possible directly.
You could throw an exception and add the required code to check for the exception and assign null to you variable.
A better solution would be to use a Factory that would return null if some condition fail.