添加对象的私有引用

时间:2012-04-10 09:27:59

标签: c# debugging

我需要为Unit对象_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);
    }
}

此外,我得到的另一个错误,我根本无法完全理解,下面的方法是静态,我研究过这也是{{1}的属性和属性} static 解决了每个人_Grade上显示的错误,但它仍显示在第一个<?p>上?

_Grade

3 个答案:

答案 0 :(得分:2)

关于你的第一个问题:

您的班级Result继承自Unit,您的Result构造函数会调用其中一个基类。但是,在Unit中只定义了一个需要两个参数(codename)的构造函数,因此您在base构造函数中对Result的调用需要有两个参数。

但您可能不希望从Unit继承但是添加私有引用。那里你会有像

这样的东西
class Result {

  private Unit _Unit;

  ...

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

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

public static bool ValidateGrade(string grade)
{
  return (grade == "N" || grade == "P" ...)
}

答案 1 :(得分:1)

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

结果类中调用Base时,必须使用两个参数

调用它

答案 2 :(得分:1)

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

public Result(string grade, Unit _Unit) : base(_Unit.Code, _Unit.Name)
相关问题