【问题标题】:C# "is inaccessible due to its protection level" error in constructor构造函数中的 C#“由于其保护级别而无法访问”错误
【发布时间】:2011-07-16 19:48:19
【问题描述】:

子类“caesar”的构造函数报错。它说名称,类型由于其保护级别而无法访问。怎么会?由于这是从“密码”类派生的子类,因此不应给出这样的错误。我该如何克服这种情况。但我希望这些变量是私有的。我不想将它们更改为公开。

***第二个代码示例有效。有人能看出区别吗?

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

【问题讨论】:

  • 啊!您正在使用 C#...使用属性而不是 getter/setter 方法。
  • naming guidelines 也可能是海报的好读物。代码看起来很像 Java。 ;-)
  • 是的,实际上我的首选语言是 Java,所以我已经习惯了。由于 C# 与 Java 相似,我在 C# 中也使用我的 java 编程习惯 :D 但是无论如何你是对的 :)

标签: c# inheritance encapsulation access-modifiers


【解决方案1】:

看来您在定义派生类构造函数时犯了一个错误。如果您想将 nametype 值传递给超类,则必须将它们作为附加构造函数参数传递(派生类构造函数中总共有 4 个参数。)例如,将其更改为应该工作:

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

您还可以采取许多其他策略。

【讨论】:

  • 哦,你是对的。我刚刚对我的帖子进行了编辑。就像你说的那样。谢谢
【解决方案2】:

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

访问权限摘要:

  • private: 只能通过该类方法访问,不能访问其他方法
  • protected: 可以从该类及其子类的方法中访问
  • internal: 只能从同一程序集中的方法访问
  • protected internal: 与其他程序集派生类的内部+方法相同
  • public: 所有人都可以访问

【讨论】:

    【解决方案3】:
        public Caesar(int shiftamount, string shiftdirection)
            : base(name, type)
        {
    

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

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

    【讨论】:

      【解决方案4】:

      private 意味着只有声明类可以访问成员(意味着继承的类型也不能)。

      protected 表示声明类和任何后代都可以访问成员,但那些之外的类型不能。

      另一方面,getter 和 setter 函数通常不用于 .NET 语言。属性封装了这个功能并且应该被使用。例如;

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

      【讨论】:

        猜你喜欢
        • 2013-06-04
        • 1970-01-01
        • 2014-08-15
        • 1970-01-01
        • 1970-01-01
        • 2012-10-05
        • 1970-01-01
        • 1970-01-01
        • 2012-12-13
        相关资源
        最近更新 更多