Hàm tạo của lớp con "caesar" đưa ra lỗi. Nó nói rằng tên, loại là không thể tiếp cận do mức độ bảo vệ của nó. Làm thế nào mà? Vì đây là lớp con có nguồn gốc từ lớp "Mật mã" nên không nên đưa ra lỗi như thế này. Làm thế nào tôi có thể vượt qua tình trạng này. Nhưng tôi muốn các biến đó là riêng tư. Tôi không muốn thay đổi chúng thành công khai.Lỗi C# "không thể truy cập được do mức độ bảo vệ" trong hàm tạo
*** Ví dụ về mã thứ hai hoạt động. Ai có thể thấy sự khác biệt không?
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;
}
}
}
----------------------------- Chỉnh sửa mới ------------ ----
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! Bạn đang sử dụng C# ... sử dụng Properties thay vì các phương thức getter/setter. –
[nguyên tắc đặt tên] (http://msdn.microsoft.com/en-us/library/xzf533w0%28v=vs.71%29.aspx) có thể là một đọc tốt cho các poster quá. Mã trông rất giống Java. ;-) – mpontillo
Vâng thực sự ngôn ngữ preffered của tôi là Java nên tôi rất quen với nó. Như C# là tương tự như Java, tôi sử dụng thói quen lập trình java của tôi trong C# quá: D Nhưng yeah bạn là đúng anyway :) – Alptugay