C# 构造函数 - 设置第二个属性
我有这个类:
public class CalendarData_Day
{
public DateTime Date { get; set; }
public DayType TypeOfDay { get; set; }
public bool Choose { get; set; }
public CalendarData_Day(DateTime datum) : this(datum, DayType.Normal, true)
{
}
public CalendarData_Day(DateTime datum, DayType typDne) : this(datum, typDne, true)
{
}
public CalendarData_Day(DateTime datum, DayType typDne, bool vybran)
{
this.Date = datum;
this.TypeOfDay = typDne;
this.Choose = vybran;
}
}
我想在第二个构造函数中检查 DayType 是否为 Weekend,如果不是则不发送到 Choose true but false。有人知道我该怎么做吗? 我知道我可以添加到最后一个构造函数 if 并检查,但它似乎不适合我。我认为有更好的方法,我认为我应该以其他方式来做,或者这在最后一个构造函数中可以吗:
if (TypeOfDay == DayType.Weekend)
this.Choose = false;
我知道它有效,但我不知道这是正确的方法。
编辑: 很抱歉我没有解释一切。有超过 2 个 DayType,假设有 Holiday、Work,...我希望用户可以仅使用第二个构造函数来调用类,如果 DayType 是 Weekend 或 Holiday,则 Choose 必须为 false,但如果是 Normal 或Work 应该为 true,否则用户必须使用最后一个构造函数并将 DayType 设置为 Work,并将 Choose 设置为 false。这很复杂,很抱歉我应该第一次写这个。
I have this class:
public class CalendarData_Day
{
public DateTime Date { get; set; }
public DayType TypeOfDay { get; set; }
public bool Choose { get; set; }
public CalendarData_Day(DateTime datum) : this(datum, DayType.Normal, true)
{
}
public CalendarData_Day(DateTime datum, DayType typDne) : this(datum, typDne, true)
{
}
public CalendarData_Day(DateTime datum, DayType typDne, bool vybran)
{
this.Date = datum;
this.TypeOfDay = typDne;
this.Choose = vybran;
}
}
and I want in second constructor check if DayType is Weekend and if it is then not send to Choose true but false. Anybody knows how can I do it?
I know I can add to last constructor if and checked but it doesn´t seem right for me. I think there is better way I think that I should do it other way or is this in last contructor okay:
if (TypeOfDay == DayType.Weekend)
this.Choose = false;
I know it´s working but I don´t know it is right way.
Edit:
I am sorry for that I don´t explained everything. There is more than 2 DayTypes, lets say there is Holiday, Work, ... And I want that user can call class with just second constructor and if DayType would be Weekend or Holiday then Choose must be false but if it would be Normal or Work it should be true or user must user last contructor and set DayType to Work and Choose to false. It is complicated I am sorry I should wrote this first time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好根据参数传递链式构造函数参数:
这样您就不需要将属性设置两次 - 一次设置为某种默认值,然后根据您已知的信息修复它。
我个人会将参数名称从
typDne
更改为dayType
或类似名称。编辑:我刚刚看到您正在考虑将测试放入最后构造函数而不是第二构造函数中。我希望按原样接受调用者为 vybran 给出的值,而不是有条件地忽略。您仅描述希望第二个构造函数检查
DayType == Weekend
- 而不是最后一个构造函数 - 因此只有第二个构造函数应该更改。编辑:如果周末或假期的
Choose
必须为假,那么我会在最后一个构造函数中强制执行,但选择值 em> 在第二个构造函数中:It would be nicer to pass the chained constructor argument based on the parameter:
That way you don't need to set the property twice - once to a sort of default value and then fix it based on information you already knew.
I would personally change the parameter name from
typDne
todayType
or something similar.EDIT: I've only just seen that you were considering putting your test into the last constructor rather than the second one. I would expect the value given by the caller for
vybran
to be accepted as-is, rather than conditionally ignored. You only describe wanting the second constructor to check forDayType == Weekend
- not the last constructor - so it's only the second constructor that should change.EDIT: If
Choose
must be false for Weekend or Holiday then I would enforce that in the last constructor but pick the value in the second constructor:验证构造函数中的参数是完全可以的。
不过,您应该确保如果条件为 false,您的代码仍然按预期工作:
如果传入的参数完全错误,您可以考虑抛出
ArgumentException
。Validating parameters in your constructor is perfectly OK.
Though you should ensure that if the condition is false your code still works as expected:
You may consider throwing an
ArgumentException
if the parameters passed in are completely wrong.如果您的构造函数之一具有非常广泛的逻辑,您可能会受益于调用私有构造函数的静态构造函数。
If you have very extensive logic in one of your constructors you might benefit from static construction functions calling private constructors.