我需要添加對單元對象_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)
謝謝,這是完美的。我選擇使用布拉姆W關於第一個問題的答案。但是,您在第二個問題上的輸入解決了錯誤。 – Taz 2012-04-10 09:55:55
我明白任務15(// 15。添加一個私人引用到一個單位對象)有點像隱藏(裝箱)它,但不能繼承它,但確定:-) – Matten 2012-04-10 10:27:01