2017-02-13 98 views
-2

我無法讓主讀取我的構造函數。它一直說我的類不包含帶有3個參數的構造函數。它是公開的。我不知道自己做錯了什麼,我在成功之前完成了這個,這是我用來爲我的測試學習的一個練習題。不包含一個構造函數,它接受3個參數

public class Actor 
{ 
    //attributes 
    private string name; 
    private int awardsNum; 
    private bool SAGMember; 

    //property 

    private string name 
    { 
     get { return name; } 
     set { name = value; } 

    } 


    //constructor 

    public Actor(string Name, int AwardsNum, bool SAGMember) 
    { 
     this.name = Name; 
     this.awardsNum = AwardsNum ++; 
     this.SAGMember = false; 
    } 

    public Actor() 
    { 
    this.name = "Bob Smith"; 
    this.awardsNum = 0 ; 
    this.SAGMember = false; 
    } 

    public override string ToString() 
    { 
     return string.Format ("Actor: " + name + "\n" + " Number of Awards: " + awardsNum + "\n"+ "SAG Member: " + SAGMember); 
    } 

} 


public static void Main (string[] args) 
     { 
      Actor a1 = new Actor ("Dustin Hoffman", 0 , true); 
      Console.WriteLine (a1); 

      Actor a2 = new Actor(); 
      Console.WriteLine (a2); 
     } 
+0

此外,在構造函數的參數SAGMEMBER是一樣的局部場SAGMEMBER –

+0

沒有'SAGMEMBER'財產演員類,但有一個'SAGMember'屬性。 – Andrew

+0

@安德魯,那是剛剛改變。 –

回答

7

我想是因爲這行你的演員類沒有編譯:

 set { name = Dustin Hoffman; } 

和JIT錯誤只是困惑,因爲只是如何打破你的代碼是作爲一個結果。

另外:

Console.WriteLine("The Crowd applauds for " + name "with" + awardsNum "awards"); 

不能簡單地坐下了在沒有人的土地,那一定是某種方法或構造內。

以下每個註釋:您聲明的名稱既是字段又是具有相同大小寫的Property,這是無效的。它被宣佈爲SAGMember,但被稱爲SAGMEMBER。

你知道在Visual Studio中有一個錯誤窗口,對吧?

+3

此外,'name'被聲明兩次,SAGMember具有不同的大小寫。 –

+1

他也錯過了一些+。 – EJoshuaS

2

實際上你有多個編譯錯誤。我建議從最高的錯誤開始,並開始工作。在這種情況下,正如@JasonLind所建議的那樣,缺少構造函數的錯誤消息可能是不正確的 - 偶爾你會看到類似這樣的情況,以前的編譯錯誤會使編譯器混淆(這就是爲什麼你通常希望從第一個開始編譯錯誤 - 有時修復一個編譯錯誤也會修復其他幾個錯誤)。

例如:

Console.WriteLine("The Crowd applauds for " + name "with" + awardsNum "awards"); 

你缺少一個+。另外,如上所述,這必須在方法內。

此外,指示:

set { name = Dustin Hoffman; } 

需要報價。你不應該那樣做,因爲你不能把物業設置爲除「達斯汀霍夫曼」之外的任何物品。

此外,name被宣佈兩次。使屬性和字段具有不同的名稱。

此外,在構造函數:

this.awardsNum = AwardsNum ++; 
    this.SAGMember = false; 

爲什麼++?此外,你總是扔掉傳入構造函數的值,因爲SAGMember被硬編碼爲false

對於ToString方法,

return string.Format ("Actor: " + name + "\n" + " Number of Awards: " + awardsNum + "\n"+ "SAG Member: " + SAGMember); 

你不使用格式字符串在這裏,只是連接。另外,爲什麼\n是一個單獨的字符串?

以下注釋:

//attributes 

不正確。這些是領域,而不是屬性 - 他們是非常不同的。

+1

我一直傾向於'Environment.NewLine'或者使用帶'.AppendLine()'的'StringBuilder'來插入換行符,而不是轉義新的行字符,但這只是大部分的個人觀點。 – Andrew

0
public class Actor 
{ 
    //Fields 
    /*Write a class 「Actor」 that contains these attributes with the appropriate level of visibility explicitly defined: 
    「Name」 which is a String, 
    「numberOfAwards」 which is an integer 
    「SAGMember」 which is a bool*/ 

    private string name; 
    private bool SAGMember; 
    private int awardsNum; 



    //property 
    //4.Write a property (get/set) for the name attribute ONLY. 

    private string Name 
    { 
     get { return name; } 
     set { name = value; } 

    } 

    //constructor 
    // Write a parameterless constructor for this class that initializes the name to 「Bob Smith」, the number of awards to 0, and the SAGMember to 「false」. 
    public Actor() 
    { 
     this.name = "bob smith"; 
     this.SAGMember = false; 
     this.awardsNum = 0; 
    } 

    public Actor(string name, bool SAGMember, int awardsNum) 
    { 
     this.name = Name; 
     this.SAGMember = true; 
     this.awardsNum = 4; 
    } 


    // over ride 
    public override string ToString() 
    { 
     return string.Format("Actor: " + name + "\n" + "\n" + "SAG Member: " + SAGMember + "Number of Awards: " + awardsNum); 
    } 


} 

-----主要------

public static void Main (string[] args) 
    { 

     Actor a1 = new Actor ("Dustin Hoffman", true, 5); 
     Console.WriteLine (a1); 

     Actor a2 = new Actor(); 
     Console.WriteLine (a2); 
    } 
} 
相關問題