我需要添加对 Unit object 的私有引用_Unit
。附加的是两个类(Unit
和Result
)。
我知道我需要下面的代码,但是它会导致错误(如下所列):
// 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)