子類「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);
}
Gah!您正在使用C#...使用屬性而不是getter/setter方法。 –
[命名指南](http://msdn.microsoft.com/en-us/library/xzf533w0%28v=vs.71%29。aspx)也可能是海報的好讀物。代碼看起來很像Java。 ;-) – mpontillo
是的,其實我的優先語言是Java,所以我非常習慣它。由於C#與Java很類似,所以我在C#中也使用了我的java編程習慣:D但是,無論如何,你是對的:) – Alptugay