2012-04-10 71 views
0

我需要添加對單元對象_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) 

回答

2

關於第一個問題調用它:

你的類ResultUnit inherites和你的Result構造函數調用基的一個類。但是,在Unit中,只有一個構造函數需要兩個參數(codename),因此您在Result構造函數上調用base需要有兩個參數。

但是,您可能不想從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" ...) 
} 
+0

謝謝,這是完美的。我選擇使用布拉姆W關於第一個問題的答案。但是,您在第二個問題上的輸入解決了錯誤。 – Taz 2012-04-10 09:55:55

+0

我明白任務15(// 15。添加一個私人引用到一個單位對象)有點像隱藏(裝箱)它,但不能繼承它,但確定:-) – Matten 2012-04-10 10:27:01

1

單位類的構造函數需要兩個參數。

結果類調​​用基地必須有兩個參數

1

首先,在您的結果上課,_UNIT字段的類型爲單位,而不是字符串。 您得到的基礎構造函數的錯誤是因爲您在Unit類的構造函數中指定了2個參數。要麼你必須添加另一個構造函數的單元類,或更改結構類的構造函數爲例如

public Result(string grade, Unit _Unit) : base(_Unit.Code, _Unit.Name) 
+0

感謝您對構造函數的提及。這是我們所教的類型,所以最好使用它。 – Taz 2012-04-10 09:50:06