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

Alp*_*gay 6 c# inheritance encapsulation access-modifiers

子类"caesar"的构造函数给出错误.它说由于其保护级别,名称,类型无法访问.怎么会?因为这是从"Cipher"类派生的子类,所以不应该给出这样的错误.我怎样才能克服这种情况.但我希望这些变量是私有的.我不想将它们改为公开.

***第二个代码示例有效.任何人都能看到差异吗?

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

    }
}
Run Code Online (Sandbox Code Playgroud)

-----------------------------新编辑----------------

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);
        }
Run Code Online (Sandbox Code Playgroud)

mpo*_*llo 7

看起来你在定义派生类构造函数时犯了一个错误.如果要获取超类的值nametype值,则必须将它们作为附加构造函数参数传递(在派生类构造函数中总共有4个参数.)例如,将其更改为this应该有效:

    public Caesar(int shiftamount, 
                  string shiftdirection, 
                  string name, 
                  string type) 
                  : base(name, type)
Run Code Online (Sandbox Code Playgroud)

您可以采取其他一些策略.


Arm*_*yan 5

不能从派生类访问私有成员。受保护和公共可以。你必须保护它们。这样,只有班级及其“孩子”才能访问。

访问权限摘要:

  • private: 只能从该类方法访问,没有别的
  • protected: 可以从该类及其子类的方法访问
  • internal: 只能从同一程序集中的方法访问
  • protected internal: 与来自其他程序集的派生类的内部 + 方法相同
  • public: 每个人都可以访问


Bro*_*ass 5

    public Caesar(int shiftamount, string shiftdirection)
        : base(name, type)
    {
Run Code Online (Sandbox Code Playgroud)

问题是private字段名称和类型 - 子类无法访问它们,除非它们被标记为protected.我怀疑你真正想要的是什么

    public Caesar(int shiftamount, string shiftdirection)
        : base("Caesar5", "Caesar")
    {
Run Code Online (Sandbox Code Playgroud)