简体   繁体   English

由于构造函数中的“保护级别”错误,C#“无法访问”

[英]C# “is inaccessible due to its protection level” error in constructor

The constructor of the child class "caesar" gives an error. 子类“caesar”的构造函数给出错误。 It says that name, type is inaccessible due to its protection level. 它说由于其保护级别,名称,类型无法访问。 How come? 怎么会? As this is a child class derived from "Cipher" class it shouldn't give an error like this. 因为这是从“Cipher”类派生的子类,所以不应该给出这样的错误。 How can I overcome this situation. 我怎样才能克服这种情况。 But I want those variables to be private. 但我希望这些变量是私有的。 I don't want to change them as public. 我不想将它们改为公开。

***The second code example works. ***第二个代码示例有效。 Can anybody see a difference? 任何人都能看到差异吗?

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;
        }

    }
}

----------------------------- New Edit ---------------- -----------------------------新编辑----------------

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);
        }

It looks like you have made a mistake in defining the derived class constructor. 看起来你在定义派生类构造函数时犯了一个错误。 If you want to get name and type values to the superclass, you'll have to pass them in as additional constructor arguments (for a total of 4 arguments in the derived class constructor.) For example, changing it to this should work: 如果要获取超类的nametype值,则必须将它们作为附加构造函数参数传递(在派生类构造函数中总共有4个参数。)例如,将其更改为this应该有效:

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

There are a number of other strategies you could take. 您可以采取其他一些策略。

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

The problem is the private fields name and type - the child class cannot access them unless they are marked as protected . 问题是private字段的名称和类型 - 除非将它们标记为protected否则子类无法访问它们。 What your really want I suspect is 我怀疑你真正想要的是什么

    public Caesar(int shiftamount, string shiftdirection)
        : base("Caesar5", "Caesar")
    {

Private members cannot be accessed from derived classes. 无法从派生类访问私有成员。 protected and public can. 受保护和公众可以。 You must make them protected. 你必须保护他们。 This way, only the class and its "children" will have access. 这样,只有类及其“子”才能访问。

Summary of access rights: 访问权限摘要:

  • private: can be accessed only from that class methods, nothing else private:只能从该类方法访问,没有别的
  • protected: can be accessed from that class's and its children's methods protected:可以从该类及其子级的方法访问
  • internal: can be accessed only from methods within the same assembly internal:只能从同一程序集中的方法访问
  • protected internal: same as internal + methods of derived classes from other assemblies protected internal:与其他程序集中派生类的内部+方法相同
  • public: can be accessed by everyone public:每个人都可以访问

private means that only the declaring class can access the members (meaning that inherited types cannot, either). private表示只有声明类才能访问成员(意味着继承的类型也不能)。

protected means that the declaring class and any descendants can access the members, but types outside of those cannot. protected表示声明类和任何后代可以访问成员,但那些类型之外的类型不能。

On a different note, getter and setter functions are generally not used in .NET languages. 另外,在.NET语言中通常不使用getter和setter函数。 Properties encapsulate this functionality and are what should be used instead. 属性封装了此功能,而应该使用它。 For example; 例如;

private string name;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# - 由于其保护级别而无法访问 - C# - is inaccessible due to its protection level C#编译错误:“由于其保护级别,X无法访问” - C# compile error: “X is inaccessible due to its protection level” C#多态性错误:由于其保护级别而无法访问 - C# Polymorphism Error : is inaccessible due to its protection level C#:嵌套类的构造函数使得“由于保护级别而无法访问” - C#: constructor of nested class makes “inaccessible due to protection level” c# 资源中的 SmtpClient 由于其保护级别而无法访问 - SmtpClient in c# resource is inaccessible due to its protection level 由于其保护级别2,无法访问Azure Function C#“运行” - Azure Function C# 'Run' is inaccessible due to its protection level 2 ...由于其保护级别为c#/ asp.net而无法访问 - …is inaccessible due to its protection level c#/asp.net C#:GroupEvent由于其保护级别而无法访问 - C#: GroupEvent inaccessible due to its protection level 成员由于保护级别错误而无法访问 - Member is inaccessible due to its protection level error “'System.IO.FileSystemInfo.FullPath'由于其保护级别而无法访问”C#中的错误 - “'System.IO.FileSystemInfo.FullPath' is inaccessible due to its protection level” error in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM