是否可以从指向子类对象的父类类型引用变量访问不在父类中的子类成员?

时间:2015-12-20 14:30:31

标签: c# asp.net .net oop inheritance

根据我的理解,如果我有一个指向子类对象的父类类型引用变量:

ParentClass obj= new ChildClass();

obj.OnlyMembersThatHavebeenDerivedFromParentAreAvailable// am i wrong? 

在下面我从click here获取的示例 我应该无法访问AnnualSalary

派生类:

 public class FullTimeEmployee : Employee
    {
        public int AnnualSalary { get; set; }
    }

派生类:

public class PartTimeEmployee : Employee
    {
        public int HourlyPay { get; set; }
        public int HoursWorked { get; set; }
    }

父类

 public class Employee
    {
        private int _id;
        private string _name;
        private string _gender;
        private DateTime _dateOfBirth;

        [DataMember(Order = 1)]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [DataMember(Order = 2)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [DataMember(Order = 3)]
        public string Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }

        [DataMember(Order = 4)]
        public DateTime DateOfBirth
        {
            get { return _dateOfBirth; }
            set { _dateOfBirth = value; }
        }

        [DataMember(Order = 5)]
        public EmployeeType Type { get; set; }
    }

以下代码如何有效? :

Employee employee = null;
 employee = new FullTimeEmployee
                        {
                            Id = Convert.ToInt32(reader["Id"]),
                            Name = reader["Name"].ToString(),
                            Gender = reader["Gender"].ToString(),
                            DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]),
                            Type = EmployeeType.FullTimeEmployee,
                            AnnualSalary = Convert.ToInt32(reader["AnnualSalary"]) // how is AnnualSalary available here? 
                        };

1 个答案:

答案 0 :(得分:2)

你说得对,你不能写:

employee.AnnualSalary = 1234;

但这不是你的情况 您只是使用对象初始值设定项来初始化FullTimeEmployee对象(您可以访问所有公共字段/属性)。
基本上你正在做以下事情:

FullTimeEmployee employeeTemp = new FullTimeEmployee();
employeeTemp .AnnualSalary =2000;
Employee employee =employeeTemp;

更新

  

我认为不能将子类对象强制转换为子类   因为孩子可以做父母可以做的所有事情,反之亦然   不是真的。

你再一次是对的。
再一次,这不是你的情况......
方法的返回类型可能是Employee,但是您返回的实际对象可能是其他东西(派生类) 在这种情况下,您可以安全地将对象强制转换为派生类型 请查看以下示例

namespace CastExample
{
  class Program
  {
    static void Main(string[] args)
    {
        Employee emp = GetEmployee();
        FullTimeEmployee full = (FullTimeEmployee)emp;
        System.Console.WriteLine(full.AnnualSalary);
        PartTimeEmployee part = (PartTimeEmployee)emp;//InvalidCastException
        System.Console.ReadLine();
    }

    static Employee GetEmployee()
    {
        return new FullTimeEmployee() { Name = "George", AnnualSalary = 1234   };
    }
  }
  public class Employee
  {
     public string Name;
  }
  public class FullTimeEmployee : Employee
  {
     public int AnnualSalary { get; set; }
  }
  public class PartTimeEmployee : Employee
  {
      public int HourlyPay { get; set; }
      public int HoursWorked { get; set; }
  }
}
  

他也可以访问私有财产吗?

是的,您可以使用反射来访问私人字段 Find a private field with Reflection?
How to get the value of private field in C#?
c# use reflection to get a private member variable from a derived class

检查一下: Why can reflection access protected/private member of class in C#?
@Marc Gravell的回答解释了为什么你能做到这一点,但要特别注意@Tamas Czinege的答案(我再次在这里引用)

  

会员辅助功能不是安全功能。它是为了保护   程序员反对自己。它有助于实施   封装但它绝不是安全功能。