2011-03-16 16 views
6

子類「caesar」的構造函數出錯。它表示,由於其保護級別,名稱,類型無法訪問。怎麼來的?由於這是從「Cipher」類派生的子類,它不應該給出這樣的錯誤。我如何克服這種情況。但我希望這些變量是私人的。我不想將它們改爲公開的。C#「由於其保護級別而無法訪問」錯誤構造函數中的錯誤

***第二個代碼示例有效。任何人都可以看到不同之處嗎?

namespace Encrypter 
{ 
    class Cipher 
    { 
     public Cipher(string name, string type) 
     { 
      setName(name); 
      setType(type); 

     } 
     private string name; 
     private string type; 

     public void setName(string newName) 
     { 
      name = newName; 
     } 
     public string getName() 
     { 
      return name; 
     } 
     public void setType(string newType) 
     { 
      type = newType; 
     } 
     public string getType() 
     { 
      return type; 
     } 
     public string encrypt(string text) 
     { 
      return text; 
     } 
     public string decrypt(string text) 
     { 
      return text; 
     } 
    } 
} 




namespace Encrypter 
{ 
    class Caesar : Cipher 
    { 

     private int shiftamount; 
     private string shiftdirection; 
     public Caesar(int shiftamount, string shiftdirection) : base(name, type) 
     { 
      setShiftamount(shiftamount); 
      setShiftdirection(shiftdirection); 
     } 
     public void setShiftamount(int newShiftamount) 
     { 
      shiftamount = newShiftamount; 
     } 
     public int getShiftamount() 
     { 
      return shiftamount; 
     } 
     public void setShiftdirection(string newShiftdirection) 
     { 
      shiftdirection = newShiftdirection; 
     } 
     public string getShiftdirection() 
     { 
      return shiftdirection; 
     } 

    } 
} 

-----------------------------新編輯------------ ----

class MyFile 
    { 
     public MyFile(int id, string name, int size, string type) 
     { 
      setId(id); 
      setName(name); 
      setSize(size); 
      setType(type); 

     } 
     private int id; 
     private string name; 
     private string type; 
     private int size; 




class Movie : MyFile 
    { 
     private string director; 
     private int release_year; 
     public Movie(string director, int release_year, int id, string name, int size) : base(id, name, size, "m") 
     { 
      setDirector(director); 
      setRelease_year(release_year); 
     } 
+16

Gah!您正在使用C#...使用屬性而不是getter/setter方法。 –

+0

[命名指南](http://msdn.microsoft.com/en-us/library/xzf533w0%28v=vs.71%29。aspx)也可能是海報的好讀物。代碼看起來很像Java。 ;-) – mpontillo

+0

是的,其實我的優先語言是Java,所以我非常習慣它。由於C#與Java很類似,所以我在C#中也使用了我的java編程習慣:D但是,無論如何,你是對的:) – Alptugay

回答

7

看起來您在定義派生類構造函數時犯了一個錯誤。如果你想獲得超類的nametype值,你必須將它們作爲附加的構造函數參數傳遞(在派生類構造函數中總共有4個參數)。例如,將其更改爲:

public Caesar(int shiftamount, 
        string shiftdirection, 
        string name, 
        string type) 
        : base(name, type) 

還有其他一些策略可以採取。

+0

哦,你是對的。我剛剛對我的帖子進行了修改。就像你說的那樣。謝謝 – Alptugay

3

無法從派生類訪問私有成員。受保護和公衆可以。你必須讓他們受到保護。這樣,只有班級及其「孩子」才能訪問。

的訪問權限摘要:

  • private:只能從類的方法來訪問,沒有別的
  • protected:可以從類的訪問和其子的方法
  • internal:只能從訪問同一程序集內的方法
  • protected internal:與內部+來自其他程序集的派生類的方法相同
  • public:每個人都可以訪問
3

private意味着聲明類可以訪問成員(意味着遺傳類型不能,要麼)。

protected意味着聲明類和任何後代都可以訪問這些成員,但這些成員之外的類型不能。

在另一個說明中,getter和setter函數通常不用於.NET語言。屬性封裝了這個功能,應該用什麼來代替。例如;

private string name; 

public string Name 
{ 
    get { return name; } 
    set { name = value; } 
} 
5
public Caesar(int shiftamount, string shiftdirection) 
     : base(name, type) 
    { 

的問題是private域的名稱和類型 - 子類不能訪問它們,除非它們被標記爲protected。你真的想要我懷疑的是

public Caesar(int shiftamount, string shiftdirection) 
     : base("Caesar5", "Caesar") 
    { 
相關問題