日期:2011-05-14  浏览次数:20472 次

 

using System;

namespace demo
{
    class ClassA
    {
        protected string a = "test";

        public void Display()
        {
            Console.WriteLine(a);
        }
    }

    class ClassB:ClassA
    {
        protected string a = "another test";
    }
    /**//// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /**//// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            // TODO: Add code to start application here
            //
            ClassB instance = new ClassB();
            instance.Display();
            ((ClassA)instance).Display();
            Console.ReadLine();
        }
    }
}

此时控制台输出的内容两次都是test
但是如果做如下修改:


using System;

namespace demo
{
    class ClassA
    {
        private string _a = "test";
        protected virtual string a
        {
            get
            {
                return _a;
            }
            set
            {
                _a = value;
            }
        }

        public void Display()
        {
            Console.WriteLine(a);
        }
    }

    class ClassB:ClassA
    {
        private string _deriveda = "another string";
        protected override string a
        {
            get
            {
 &nbs