[英]Some of my class fields is required. How can i know it was initialized?
Ok guys. 好了朋友们。 Im writing code and have one problem.
我在写代码,有一个问题。 Language is C# but it doesnot matter.
语言是C#,但没关系。 For example: I have class Point
例如:我有Point类
Class Point
{
private _x;
private _y;
//getter and setters
}
So, if i want to use object of this class i need to totally know that all fields, like _x and _y was initialized. 因此,如果我想使用此类的对象,则需要完全知道所有字段,例如_x和_y都已初始化。 Default values is not solving.
默认值无法解决。 My solutions:
我的解决方案:
But if i have 7 required fields. 但是,如果我有7个必填字段。
Class Point
{
private _x; //required it must not be null
private _y; //required it must not be null
private _z; //required it must not be null
private _b; //required it must not be null
private _a; //required it must not be null
private _f; //required it must not be null
private _h; //required it must not be null
//getter and setters
}
How i can be sure that all of them was initialized? 我如何确定所有这些都已初始化? Passing this count of paramaters in constructor is really hard and ugly.
在构造函数中传递这些参数实在是很困难和丑陋。 If i will use builder pattern, it actually not make me sure.
如果我将使用构建器模式,则实际上并不能确定。 Sorry for bad english.
对不起,英语不好。
As people have commented, you can ensure that the fields are initialized by either setting them to always have a default value 正如人们评论的那样,您可以通过将字段设置为始终具有默认值来确保对字段进行初始化
Class Point
{
private int j = 42;
}
or have your constructor always define a default value for each field 或者让您的构造函数始终为每个字段定义默认值
Class Point
{
private int j;
public Point (int jVal)
{
j = -1;
}
}
This will ensure that whenever a new instance of the class is created, the fields will always be initialized. 这将确保无论何时创建该类的新实例,都将始终初始化字段。
If you have references that need to point to objects, you can do it the same way, but just change the value (such as 42 in the previous example) to the language's equivalent of C#'s new Object()
. 如果您有需要指向对象的引用,则可以以相同的方式进行操作,只是将值(例如上例中的42)更改为与C#的
new Object()
语言等效。
For knowing if the field was initialised you are force to using a flag. 为了知道该字段是否已初始化,您必须使用标志。
Make it explicit bool _xIsSet;
使它显式
bool _xIsSet;
or if _x
is an int
then make the type int?
或者,如果
_x
是一个int
则将其_x
int
类型int?
and check for the value being null. 并检查该值为空。
The former does not throw any exception but if you are not careful you will work with non-initialised values. 前者不会引发任何异常,但是如果您不小心,将使用未初始化的值。
The later way throws errors; 后一种方法会引发错误; possibly better but still at run time.
可能更好,但仍在运行时。
You could decide for properties instead and wrap the fields with checks for null but the result would be the same; 您可以决定是否使用属性,并通过检查是否为空来包装字段,但结果将是相同的。 any error would surface run time.
任何错误都会影响运行时间。 (or not at all)
(或完全没有)
The method I prefer since 20? 从20开始我更喜欢的方法? years is to have a constructor that takes all parameters and even make sure there is no default constructor.
年是要有一个接受所有参数的构造函数,甚至要确保没有默认的构造函数。
var myPoint = new Point( valueX, valueY, valueK );
With later C# versions one can name the arguments so the code becomes quite easy to read. 在更高版本的C#版本中,可以命名参数,因此代码变得非常易于阅读。
var myPoint = new Point( X = valueX, Y = valueY, L = valueK // <-- see here! it is quite easy to spot a possible error );
My explanation for using this method, except for the one you state, is that "It is very seldom you want a Customer
that is not set fully.". 我对使用此方法的解释是,“除了您声明的方法之外”,这是“很少有人希望
Customer
没有完全设置好”。 Then... if there are several ways to set a Customer
fully I create more constructors. 然后...如果有几种方法可以完全设置一个
Customer
我会创建更多的构造函数。
I have taken this further and avoid null as much as I can, so in my code there is never a Customer
that is only half set and only rarely a Customer
that is null. 我采取这种进一步避免null作为尽我所能,让我的代码中从来就没有
Customer
是只有一半的设置,只有很少是Customer
是空。
Lately I have started experimenting with using static constructors instead; 最近,我开始尝试使用静态构造函数。 the call is then
var customer = Customer.Create(...)
. 则呼叫为
var customer = Customer.Create(...)
。 This has the taste of DDD as Create
can express the intention , can express why a class is created. 作为
Create
可以表达意图 ,可以表达为什么创建类具有DDD的味道。 var customer = Customer.CreateFromEntranceScan(...)
or var customer = Customer.CreateFromTaxDeduction(...)
. var customer = Customer.CreateFromEntranceScan(...)
或var customer = Customer.CreateFromTaxDeduction(...)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.