【问题标题】:Accessing Parents attributes访问父母属性
【发布时间】:2016-11-17 19:08:56
【问题描述】:

这可能是一个愚蠢的问题,但我是 C# 新手。我想知道是否有办法在子类中直接使用父属性。我做了很多 Eiffel,当一个类被一个或多个类继承时(因为是 Eiffel 没有接口,你可以继承多个类)。

就像那个例子:(埃菲尔语言)

父类:

class Parent

features
    int id
    string lastName

儿童班:

class Child inherit

    PARENT

feature
    bool isChild

在这种情况下,Child 类已经可以访问 id 和 lastName 并且可以直接设置为 Child 属性的一部分,不必创建 Parent。

但到目前为止我做了这个(C# 语言):

父类:

public class Character
{
    Int32 Id;
    String name;
    List<String> images;

    public Character()
    {
        name = "";
        images = null;
    }

    public Character(string a_name, List<String> imagePaths)
    {
        name = a_name;
        images = imagePaths;
    }

    public Character(Int32 a_id, string a_name, List<String> imagePaths)
    {
        Id = a_id;
        name = a_name;
        images = imagePaths;
    }
}

儿童班:

public class NPC : Character
{

    public bool isVender;

    public NPC()
    {
        Character character = new Character();
        isVender = false;
    }

    public NPC(string a_name, List<String> images)
    {
        Character caracter = new Character(a_name, images);
        isVender = false;
    }

    public NPC(string a_name, List<string> images, bool a_bool)
    {
        Character caracter = new Character(a_name, images);
        isVender = a_bool;
    }

}

所以我的问题是,有没有办法像 Eiffel 一样在 C# 中直接访问父属性?

【问题讨论】:

    标签: c# inheritance eiffel


    【解决方案1】:

    将要在子类中使用的字段声明为protected。详细了解受保护的可见性修饰符here

    public class Character
    {
        protected Int32 Id;
        protected String name;
        protected List<String> images;
    
        public Character()
        {
            name = "";
            images = null;
        }
    
        public Character(string a_name, List<String> imagePaths)
        {
            name = a_name;
            images = imagePaths;
        }
    
        public Character(Int32 a_id, string a_name, List<String> imagePaths)
        {
            Id = a_id;
            name = a_name;
            images = imagePaths;
        }
    }
    

    然后您可以在子类中使用受保护的字段。

    【讨论】:

      【解决方案2】:

      真正似乎想要的是使用基类的构造函数来设置基类的字段,这与 C# 中的语法略有不同:

      public class NPC : Character
      {
      
          public bool isVender;
      
          public NPC() : base()
          {
              isVender = false;
          }
      
          public NPC(string a_name, List<String> images) : base(a_name, images)
          {
              isVender = false;
          }
      
          public NPC(string a_name, List<string> images, bool a_bool) : base(a_name, images)
          {
              isVender = a_bool;
          }
      
      }
      

      有没有办法像 Eiffel 一样在 C# 中直接访问父级的属性?

      如果您没有为类成员指定可访问性,默认情况下它们是private,这意味着它们只能在类本身内访问。所以最简单的改变就是让他们protected

      public class Character
      {
          protected Int32 Id;
          protected String name;
          protected List<String> images;
      

      更好的解决方案可能是在字段顶部添加protected 属性,这仍将保护实际字段,但允许继承的类更改属性的行为。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-15
        • 1970-01-01
        • 2018-05-12
        • 1970-01-01
        • 1970-01-01
        • 2018-08-07
        • 2015-07-21
        • 2016-02-11
        相关资源
        最近更新 更多