zoukankan      html  css  js  c++  java
  • 属性在继承中的应用


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

        public class Manager : Employee1
            {
                private string name;

                // Notice the use of the new modifier:
                public new string Name
                {
                    get { return name; }
                    set { name = value + ", Manager"; }
                }
            }

            class TestHiding
            {
                static void Main()
                {
                    Manager m1 = new Manager();

                    // Derived class property.
                    m1.Name = "John";

                    // Base class property.
                    ((Employee1)m1).Name = "Mary";//将子类的引用转换成父类的引用此时调用的是父类的set属性

                    System.Console.WriteLine("Name in the derived class is: {0}", m1.Name);
                    System.Console.WriteLine("Name in the base class is: {0}", ((Employee1)m1).Name);//此时调用的是父类的get属性
                }
            }
    }

    当派生类的属性隐藏拉基类中的属性在子类中用new修饰符做说明;代码如下

                public new string Name
                {
                    get { return name; }
                    set { name = value + ", Manager"; }
                }

    注:父类和子类的中的字段可以是不同名称

  • 相关阅读:
    顺序栈的模拟
    Maven安装教程详解
    C# CRC
    Oracle知识分类之常见规范
    Oracle知识分类之异常
    el-tree树结构的数据封装
    sessionStorage和localStorage
    el-form 中为el-input 添加正则校验 以及el-input正则限制
    动态组件与父子传值的灵活应用 ref控制子组件 props,emit传值
    el-tree树组件的封装 (运用递归对接口返回当前菜单数据为对象处理)
  • 原文地址:https://www.cnblogs.com/xiangxiangyuan/p/1302505.html
Copyright © 2011-2022 走看看