0

我需要添加对 Unit object 的私有引用_Unit。附加的是两个类(UnitResult)。

我知道我需要下面的代码,但是它会导致错误(如下所列):

// 14. create new class
class Result : Unit

以下创建base()需要两个构造函数的错误:

// 17. Create constructor for the class
public Result(string grade, Unit _Unit) : base(_Unit)

在我的单元类中有两个私有字符串_Code_Name. 请询问您是否需要输入任何其他课程代码或作业问题。

namespace SIT232_Assignment1
{
  // 14. create new class
  class Result
  {
    // 15. Add a private reference to a Unit objectand a private string attributes.
    private string _Grade, _Unit;

    // 16. Encapsulate the above attributes with public read-only properties
    public string Grade
    {
        get { return _Grade; }            
    }

    // 17. Create constructor for the class
    public Result(string grade, Unit _Unit) 
    {            
        _Grade = grade;            
    }

    // 18. create a public read-only property of type bool
    public bool Passed (string grade)
    {
        bool result = true;
        if (_Grade == "N")
            result = false;
        return result;
    }

    // 19. Create a public static methods 
    public static bool ValidateGrade(string grade)
    {
        bool result = false;
        if (_Grade == "N" || _Grade == "P" || _Grade == "C" || _Grade == "D" || _Grade =="HD")
            result = true;
        return result;            
    }

    // 20. Define a ToString method
    public override string ToString()
    {
        return String.Format("{0}\t{1}", _Grade);
    }
}

namespace SIT232_Assignment1
{
  // 8. Create new class
  class Unit
  {
    // 9. Add private string attributes for the unit code and unit name
    private string _Code, _Name;

    // 10. Encapsulate the above attributes with public read-only properties.
    public string Code
    {
        get { return _Code; }            
    }                
    public string Name
    {
        get { return _Name; }            
    }     

    // 11. Create constructor with two string parameters
    public Unit( string code, string name)
    {
        _Code = code;
        _Name = name;
    }

    // 27. create a private list<>
    private List<Student> _EnrolledStudents = new List<Student>();

    // 28. Encapsulate the above list with read-only
    public ReadOnlyCollection<Student> EnrolledStudents
    {
        get { return _EnrolledStudents.AsReadOnly(); }
    }

    // 29. Create a method that accecpts a single parameter
    public void RecordEnrollment(Student student)
    {
        _EnrolledStudents.Add(student);
    }

    // 30. Create a method that accecpts a single parameter
    public void RemoveEnrollment(Student student)
    {
        _EnrolledStudents.Remove(student);
    }

    // 12. Define a ToString method
    public override string ToString()
    {
        return String.Format("{0} {1}", _Code, _Name);
    }
}

此外,我得到的另一个我无法完全理解的错误是下面的方法必须是静态的,我研究过,使_Grade 静态的属性和属性也可以解决每个人显示的错误_Grade,但它仍然显示在第一个?

if (_Grade == "N" || _Grade == "P" || _Grade == "C" || _Grade == "D" || _Grade =="HD")

public static bool ValidateGrade(string grade)
4

3 回答 3

2

对于你的第一个问题:

您的类Result继承自Unit并且您的构造函数Result调用基类之一。但是,Unit只定义了一个需要两个参数(codename)的构造函数,因此您baseResult构造函数的调用需要有两个参数。

但是您可能不想继承,Unit而是添加对它的私有引用。在那里你会有类似的东西

class Result {

  private Unit _Unit;

  ...

  public Result(..., Unit _Unit)
  {
      this._Unit = _Unit;
      ...
  }
}

您的第二个错误:静态方法只能访问静态字段和属性,因此您无法从静态方法访问实例变量。您只是想验证所提供grade的是否在您的范围内,所以只需不要t refer to the instance variable_Grade :

public static bool ValidateGrade(string grade)
{
  return (grade == "N" || grade == "P" ...)
}
于 2012-04-10T09:35:42.723 回答
1

您的Unit类构造函数需要两个参数。

Result类中调用 Base 时,必须使用两个参数调用它

于 2012-04-10T09:34:46.233 回答
1

首先,在您的 Result 类中,_Unit 字段必须是 Unit 类型,而不是字符串。您获得的基本构造函数的错误是因为您在 Unit 类的构造函数中指定了 2 个参数。您必须向 Unit 类添加另一个构造函数,或者将 Result 类的构造函数更改为例如

public Result(string grade, Unit _Unit) : base(_Unit.Code, _Unit.Name)
于 2012-04-10T09:35:52.867 回答