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
    版权:本文版权归作者所有,转载需经作者同意。

  • 相关阅读:
    $on , $emit , $broadcast , $apply
    angularJS 从后台获取所有分类,并默认选中值(select)
    angularJS 发送get、post请求
    微信小程序点击不同的按钮,展示不同的信息内容
    小程序---模板的引用与使用
    项目最全的小程序源码网址
    小程序多个商品数量之间的增减与总价
    leetcode——两数之和(暴力,一遍hash,两遍hash,双指针)
    java在线程和内部类中的使用final关键字
    java线程——多线程访问成员变量与局部变量
  • 原文地址:https://www.cnblogs.com/cnblogsfans/p/1217388.html
Copyright © 2011-2022 走看看