构造函数限制

本文关键字:构造函数 | 更新日期: 2023-09-27 18:04:09

我想知道是否有办法限制构造中的值。这是我的代码:

class Student : Human 
{
    private double Grade;
    public Student(string FirstName, string LastName, double Grade)
        : base(FirstName, LastName)
    {
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Grade = Grade;
    }
}

当我创建一个新学生时,我想将成绩限制在>= 2.00 和 <= 6.00 之间,例如编译错误或运行时的 excepion。有办法吗?(不用担心其他字段名字和姓氏(

构造函数限制

您可以检查它并在运行时抛出异常,如下所示:

if (grade < 2.00 || grade > 6.00)
    throw new ArgumentOutOfRangeException("grade");

始终将此类条件放在方法或构造函数的开头。我什至将它们放在自己的#region中(但这是我个人的喜好(:

public Student(string firstName, string lastName, double grade)
    : base(firstName, lastName)
{
    #region Contract
    if (grade < 2.00 || grade > 6.00)
        throw new ArgumentOutOfRangeException("grade");
    #endregion
    this.FirstName = firstName;
    this.LastName = lastName;
    this.Grade = grade;
}

但是,有一种方法可以使用代码协定获取此类内容的编译时警告。您可以在 MSDN 上下载代码协定,还可以在那里找到文档。它只与Visual Studio的非Express版本集成,由Microsoft编写。它将检查方法调用是否可能遵循您指定的协定。然后,您的代码将变为:

using System.Diagnotistics.Contracts;
public Student(string firstName, string lastName, double grade)
    : base(firstName, lastName)
{
    #region Contract
    Contract.Requires<ArgumentOutOfRangeException>(grade >= 2.00);
    Contract.Requires<ArgumentOutOfRangeException>(grade <= 6.00);
    #endregion
    this.FirstName = firstName;
    this.LastName = lastName;
    this.Grade = grade;
}

你可以抛出一个异常:

class Student : Human 
{
    private double Grade;
    public Student(string FirstName, string LastName, double Grade)
        : base(FirstName, LastName)
    {
        if (Grade >= 2 && Grade <= 6) { 
          throw new ArgumentOutOfRangeException();
        }
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Grade = Grade;
    }
}

如果您使用的是Microsoft代码合约,您也可以这样做:

class Student : Human  {
    private double Grade;
    public Student(string FirstName, string LastName, double Grade)
        : base(FirstName, LastName)
    {
        System.Diagnotistics.Contracts.Contract.Requires<ArgumentOutOfRangeException>(Grade >= 2 && Grade <= 6);
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Grade = Grade;
    }
}

还有另一种选择,可能有点麻烦,但可以确保给定的范围,根本不使用double,而是创建一个名为 Grade 的自定义类。您的Grade类将包含从 double 到的强制转换,并将强制执行验证。这意味着所有 Grade 验证逻辑都在 Grade 结构中,Student类只接收一个 Grade 对象,不必担心它。

public struct Grade
{
    public static readonly double MinValue = 2.0;
    public static readonly double MaxValue = 6.0;
    private double value;
    public static explicit operator double(Grade grade)
    {
        return grade.value + MinValue;
    }
    public static explicit operator Grade(double gradeValue)
    {
        if (gradeValue < MinValue || gradeValue > MaxValue)
            throw new ArgumentOutOfRangeException("gradeValue", "Grade must be between 2.0 and 6.0");
        return new Grade{ value = gradeValue - MinValue };
    }
}

你会像这样使用它:

double userInput = GetUserInputForGrade();
Grade grade = (Grade)userInput; // perform explicit cast.
Student student = new Student(firstName, lastName, grade);

编辑:我已经用@EricLippert的建议更新了代码,以使该类向开发人员公开其最小/最大值。

编辑 2:根据@JeppeStigNielsen的建议再次更新。value字段现在存储与 MinValue 的偏移量,因此,无论 0 是否在有效范围内,对 default(Grade) 的调用都将返回有效值(等于 MinValue(。

class Student : Human 
{
    private double Grade;
    public Student(string FirstName, string LastName, double Grade)
        : base(FirstName, LastName)
    {
        if (Grade < 2 || Grade > 6)
            throw new ArgumentOutOfRangeException("Grade must be between 2 and 6");
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Grade = Grade;
    }
}
public Student(string FirstName, string LastName, double Grade)
    : base(FirstName, LastName)
    {
     if(Grade >= 2.0 || Grade <= 6.00)
      throw  new ArgumentException("your message");     
    }

如果你想要任何编译时检查的机会,你必须使用代码契约。它们本质上是运行时的异常,因此等效于带有异常的参数验证。

public Student(string firstName, string lastName, decimal grade)
{
    Contract.Requires(grade >= 2);
    Contract.Requires(grade <= 6);
    FirstName = firstName;
    LastName = lastName;
    Grade = grade;     
}

看到这个问题

由合同和施工方设计

class Student : Human 
{
    private double Grade;
    public Student(string FirstName, string LastName, double Grade)
        : base(FirstName, LastName)
    {
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Grade = Grade;
        if(Grade >= 2 and Grade <= 6){
            throw new Exception("Incorrect grade input");
    }
}

或者抛出你想要的相关异常,例如 ArgumentOutOfException 等......