简体   繁体   English

我的某些班级字段是必需的。 我怎么知道它已初始化?

[英]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: 我的解决方案:

  • pass all required paramaters through constructor and initialize 通过构造函数传递所有必需的参数并进行初始化
  • Always check if my field is not null (i think this is the bad way) 始终检查我的字段是否不为空(我认为这是不好的方法)

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.

相关问题 我如何知道是否存在非必需的 RenderSection? - How can I know if a non-required RenderSection exists? UNITY C# - 如何获取标有自定义属性(字段)的 class 实例的所有字段? - UNITY C# - How can I get all the fields of the an instance of a class marked with my custom atribute(the fields)? 如何实现多种引力源,特别是所需的触发。 (C#)VS 2010 - How do I implement multiple sources of gravity, specifically the trig required. (C#) VS 2010 初始化Web API时如何调用方法? - How can I call a method when my web API is initialized? C# - 使用对基类的引用,如果我知道我的引用是对继承类的引用,我如何访问继承类的属性? - C# - with a reference to a base class, how can I access a property of the inherited class if I know that my reference is to the inherited class? 如何修改 LabelFor 以在必填字段上显示星号? - How can I modify LabelFor to display an asterisk on required fields? 如何在C#、. net中找到JObject的必填字段? - How can I find the required fields of a JObject in C#, .net? 我怎么知道一个类是否可以用作静态类? - How do I know if a class can be used as a static class? 我怎么知道 class 都来自同一个通用 class - How can I know both class are from same Generic class 如何告诉Swagger不要在输入类中将某些字段显示为参数? - How can I tell Swagger to not show certain fields in my input class as parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM