C#:父类的值赋给子类

面向对象的编程中肯定会用到继承。有的情况下想把父类的值赋值给子类,你会怎么做呢?new一个父类的实例,再new一个子类的实例,然后逐个将属性赋值。本质上也确实这样赋值的,但是如果在程序用逐个去赋值就比较麻烦了,这个过程可以用一个归纳为一个方法来完成。如下:
父类:
public class ParentClass
    {
        private string id = string.Empty;


        private string name = string.Empty;


        private int age = 0;


        private string address = string.Empty;


        /// <summary>
        /// ID
        /// </summary>
        public string ID
        {
            get { return id; }


            set { id = value; }
        }
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get { return name; }


            set { name = value; }
        }
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        /// <summary>
        /// 住址
        /// </summary>
        public string Address
        {
            get { return address; }
            set { address = value; }
        }
    }
子类:
 public class ChildClass:ParentClass
    {
        private decimal scored = 0;


        private int? rank = null;


        /// <summary>
        /// 成绩
        /// </summary>
        public decimal Scored
        {
            get { return scored; }


            set { scored = value; }
        }
        /// <summary>
        /// 名次
        /// </summary>
        public int? Rank
        {
            get { return rank; }


            set { rank = value; }
        }
    }
遍历父类的属性赋值给子类的方法:
 private static ChildClass AutoCopy(ParentClass parent)
        {
            ChildClass child = new ChildClass();


            var ParentType = typeof(ParentClass);


            var Properties = ParentType.GetProperties();


            foreach (var Propertie in Properties)
            {
                if (Propertie.CanRead && Propertie.CanWrite)
                {
                    Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
                }
            }


            return child;
        }
调用:
static void Main(string[] args)
        {
            ParentClass parent = new ParentClass();


            parent.ID = "01";


            parent.Name = "Sam";


            parent.Age = 26;


            parent.Address = "Beijing";


            ChildClass child = AutoCopy(parent);


            child.Scored = 90;


            child.Rank = 1;


            string result = string.Format("ID:{0} Name={1} Age={2} Address={3}", child.ID, child.Name, child.Age, child.Address);


            string resultExtend = string.Format("Scored:{0} Rank={1}", child.Scored, child.Rank);


            Console.WriteLine(result);


            Console.WriteLine(resultExtend);


            Console.ReadLine();
        }
输出结果:

代码下载: http://download.csdn.net/detail/yysyangyangyangshan/5540909
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值