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

Deadly 提交于 2019-12-23 09:17:38

问题


The constructor of the child class "caesar" gives an error. 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. 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);
        }

回答1:


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:

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

There are a number of other strategies you could take.




回答2:


    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. What your really want I suspect is

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



回答3:


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
  • protected: can be accessed from that class's and its children's methods
  • internal: can be accessed only from methods within the same assembly
  • protected internal: same as internal + methods of derived classes from other assemblies
  • public: can be accessed by everyone



回答4:


private means that only the declaring class can access the members (meaning that inherited types cannot, either).

protected means that the declaring class and any descendants can access the members, but types outside of those cannot.

On a different note, getter and setter functions are generally not used in .NET languages. 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; }
}


来源:https://stackoverflow.com/questions/5329665/c-sharp-is-inaccessible-due-to-its-protection-level-error-in-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!