【问题标题】:Why i can't access protected property in the derived class为什么我无法访问派生类中的受保护属性
【发布时间】:2014-01-08 16:34:44
【问题描述】:

我有两个类。这是我的代码:

//My Base class
public class People
    {
       public People()
       {
       }

        protected string name;

        protected string Name
           {
        get 
        {
            return this.name;
        }
        set 
        {
            this.name = value;
        }
    }

    }

//The Child class 

public class Student:People
    {
        private int id;

        public Student()
        { 
        }

        public Student (int id, string name)
        {
            this.id = id;
            this.Name = name;
        }

        public int ID
        {
            get
            {
                return this.id;
            }

            set

            {
                this.id = value;
            }

        }
    }

当我像下面这样创建 Student 类的实例时,我无法从父类 People 访问 NAME 属性。

 public partial class Form1 : Form
    {

        private void button1_Click(object sender, EventArgs e)
        {
            Student student1 = new Student();
            student1. // only ID property is accessible 

        }
    }

我做错了吗?由于 Student 是 People 的子类,我希望 NAME 属性应该可以通过 Student 实例访问。非常感谢您提前提供的帮助。

【问题讨论】:

  • 可以在子实例中访问。您必须将其公开才能从外部访问。你可以阅读更多关于访问修饰符here
  • 我会将第一位缩短为受保护的字符串 Name {get;set;}
  • @Pierre-LucPineault 你可以把它作为答案
  • @MikeyMouse Meh 不是真的,评论就可以了,无论如何都会通知 OP。除了“使用公共并阅读文档”之外,没有什么可说的。

标签: c# polymorphism access-modifiers


【解决方案1】:

你没有做错什么,但如果你想访问

Name

通过一个实例

Student

您必须声明该属性public。否则,只允许从该类中访问(而不是通过实例)。

【讨论】:

    【解决方案2】:

    您仍在课堂外访问它,因此它需要公开。

    您可以在此处阅读有关访问修饰符的信息:

    http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx

    将您的属性更改为:

    public string Name {get;set;}
    

    你应该很高兴。

    【讨论】:

      【解决方案3】:

      Student 类不会将Name 属性暴露给外部。 如果要从学生派生类,则可以访问 Name 属性。 或者,您可以使用 new 关键字重新实现 Name,如下所示:

      public class Student : People
          {
              private int id;
      
              public new String Name { get; set;}
      
              public Student()
              {
              }
      
              public Student(int id, string name)
              {
                  this.id = id;
                  this.Name = name;
              }
      
      
      
              public int ID
              {
                  get
                  {
                      return this.id;
                  }
      
                  set
                  {
                      this.id = value;
                  }
      
              }
          }
      

      【讨论】:

      • 请记住,您应该认真考虑在基类中公开Name 属性,而不是使用new 关键字。 (仅在基类不公开Name 至关重要时才使用它)
      【解决方案4】:

      你应该把你的Properties public

      //My Base class
      public class People
          {
             public People()
             {
             }
      
              protected string name;
      
              public string Name
                 {
              get 
              {
                  return this.name;
              }
              set 
              {
                  this.name = value;
              }
          }
      
          }
      

      【讨论】:

        【解决方案5】:

        您不能使用派生类的实例访问基类的protected 属性。 如果属性只能在派生类型的范围内访问,则为受保护的属性。

        如果您需要从Student 的实例访问该属性,请在其基类中将其声明为公共:People

        如果您还需要以某种方式覆盖它的行为,可以在基类中声明public virtual 并在Student覆盖

        【讨论】:

          【解决方案6】:

          我相信您的问题是您的受保护字段位于基类而不是继承类中。看这个:http://msdn.microsoft.com/en-us/library/bcd5672a.aspx

          public class BaseClass
          {
          public string foo
          }
          
          public class SubClass
          {
          protected string bar
          }
          

          这是受保护的更常见的用法。

          【讨论】:

            猜你喜欢
            • 2017-12-14
            • 2013-10-21
            • 1970-01-01
            • 2015-12-30
            • 2019-01-20
            • 2016-02-29
            • 2012-05-02
            • 2012-06-05
            • 2012-08-29
            相关资源
            最近更新 更多