C# 构造函数 - 设置第二个属性

发布于 2024-11-29 00:56:06 字数 1072 浏览 0 评论 0原文

我有这个类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国粹 2024-12-06 00:56:06

最好根据参数传递链式构造函数参数:

public CalendarData_Day(DateTime datum, DayType typDne)
    : this(datum, typDne, typeDne != DayType.Weekend)
{        
}

这样您就不需要将属性设置两次 - 一次设置为某种默认值,然后根据您已知的信息修复它。

我个人会将参数名称从 typDne 更改为 dayType 或类似名称。

编辑:我刚刚看到您正在考虑将测试放入最后构造函数而不是第二构造函数中。我希望按原样接受调用者为 vybran 给出的值,而不是有条件地忽略。您仅描述希望第二个构造函数检查DayType == Weekend - 而不是最后一个构造函数 - 因此只有第二个构造函数应该更改。

编辑:如果周末或假期的Choose必须为假,那么我会在最后一个构造函数中强制执行,但选择 em> 在第二个构造函数中:

public CalendarData_Day(DateTime datum, DayType typDne)
    : this(datum, typDne,
           typeDne != DayType.Weekend && typeDne != DayType.Holiday)
{        
}

public CalendarData_Day(DateTime datum, DayType typDne, bool vybran)
{
    if (vybran && (typeDne == DayType.Weekend || typeDne == DayType.Holiday))
    {
        throw new ArgumentException(
           "vybran cannot be true for holiday or weekend dates", "vybran");
    }
    this.Date = datum;
    this.TypeOfDay = typDne;
    this.Choose = vybran;
}

It would be nicer to pass the chained constructor argument based on the parameter:

public CalendarData_Day(DateTime datum, DayType typDne)
    : this(datum, typDne, typeDne != DayType.Weekend)
{        
}

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 to dayType 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 for DayType == 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:

public CalendarData_Day(DateTime datum, DayType typDne)
    : this(datum, typDne,
           typeDne != DayType.Weekend && typeDne != DayType.Holiday)
{        
}

public CalendarData_Day(DateTime datum, DayType typDne, bool vybran)
{
    if (vybran && (typeDne == DayType.Weekend || typeDne == DayType.Holiday))
    {
        throw new ArgumentException(
           "vybran cannot be true for holiday or weekend dates", "vybran");
    }
    this.Date = datum;
    this.TypeOfDay = typDne;
    this.Choose = vybran;
}
拥有 2024-12-06 00:56:06

验证构造函数中的参数是完全可以的。

不过,您应该确保如果条件为 false,您的代码仍然按预期工作:

if (TypeOfDay == DayType.Weekend)
{
   this.Choose = false;
}
else
{
   this.Choose = vybran;
}

如果传入的参数完全错误,您可以考虑抛出 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:

if (TypeOfDay == DayType.Weekend)
{
   this.Choose = false;
}
else
{
   this.Choose = vybran;
}

You may consider throwing an ArgumentException if the parameters passed in are completely wrong.

温馨耳语 2024-12-06 00:56:06

如果您的构造函数之一具有非常广泛的逻辑,您可能会受益于调用私有构造函数的静态构造函数。

class MyClass
{
  private MyClass(...)
  {

  }
  public static MyClass CreateMyClassWithValidation(...)
  {
    if(....)
      return new MyClass(...);
  } 
}

If you have very extensive logic in one of your constructors you might benefit from static construction functions calling private constructors.

class MyClass
{
  private MyClass(...)
  {

  }
  public static MyClass CreateMyClassWithValidation(...)
  {
    if(....)
      return new MyClass(...);
  } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文