zoukankan      html  css  js  c++  java
  • C#拾遗系列(2):属性

    1. 这里主要演示属性的继承和覆盖

    2. 把属性理解为方法,实际上编译器就是把属性生成方法

    示例:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

     

    namespace NetTest

    {

     

       public class TestPerpoerty

        {

     

            class DerivedClass : BaseClass

           {

               private string name = "Name-DerivedClass";

               private string id = "ID-DerivedClass";

     

               new public string Name

               {

                   get

                   {

                       return name;

                   }

     

                   // Using "protected" would make the set accessor not accessible.

                   set

                   {

                       name = value;

                   }

               }

     

               // Using private on the following property hides it in the Main Class.

               // Any assignment to the property will use Id in BaseClass.

               new private string Id

               {

                   get

                   {

                       return id;

                   }

                   set

                   {

                       id = value;

                   }

               }

           }

     

     

            class BaseClass

            {

                private string name = "Name-BaseClass";

                private string id = "ID-BaseClass";

     

                public string Name

                {

                    get { return name; }

                    set { }

                }

     

                public string Id

                {

                    get { return id; }

                    set { }

                }

            }

     

            public  void Test()

            {

                BaseClass b1 = new BaseClass();

                DerivedClass d1 = new DerivedClass();

     

                b1.Name = "Mary";

                d1.Name = "John";

     

                b1.Id = "Mary123";

                d1.Id = "John123"// The BaseClass.Id property is called.

     

                System.Console.WriteLine("Base: {0}, {1}", b1.Name, b1.Id);

                System.Console.WriteLine("Derived: {0}, {1}", d1.Name, d1.Id);

     

                /*

                输出:

                Name and ID in the base class: Name-BaseClass, ID-BaseClass

                Name and ID in the derived class: John, ID-BaseClass

                            *

                --------------------------------

                注意,如果将 new private string Id 替换为 new public string Id,则得到如下输出:

                Name and ID in the base class: Name-BaseClass, ID-BaseClass

     

                Name and ID in the derived class: John, John123

                */

            }

        }

     

    }

    扫码关注公众号,了解更多管理,见识,育儿等内容

    作者: 王德水
    出处:http://www.cnblogs.com/cnblogsfans
    版权:本文版权归作者所有,转载需经作者同意。

  • 相关阅读:
    vue-动画
    vue笔记-路由,组件
    自定义键盘信息
    自定义指令
    vue-笔记2
    轻松搭建基于 Serverless 的文档图片在线转换服务
    轻松搭建基于 SpringBoot + Vue 的 Web 商城应用
    一小时快速搭建基于阿里云容器服务-Kubernetes的Web应用
    阿里云正式推出内容平台“云栖号”:全面助力企业和个人上云决策
    云原生安全-更安全的密文管理 Vault on ACK
  • 原文地址:https://www.cnblogs.com/cnblogsfans/p/1217388.html
Copyright © 2011-2022 走看看