using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
    class FarmAnimal
    {
        public string name;
        public virtual string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public virtual void MakeSound()
        {
            Console.WriteLine(Name + " makes a sound...");
        }
    }    class Cow : FarmAnimal
    {
        public override void MakeSound()
        {
            Console.WriteLine(Name + " goes Mooooo...");
        }
    }    class Horse : FarmAnimal
    {
        public override string Name
        {
            set
            {
                base.Name = value + " a Horse";
            }
        }
    }    class Program
    {
        static void Main(string[] args)
        {
            Cow c = new Cow();
            Horse h = new Horse();
            c.Name = "Elsie";
            h.Name = "Mr.Ed";
            c.MakeSound();
            h.MakeSound();
            Console.WriteLine(h.Name);     //////
            Console.ReadKey();
        }
    }
}
在程序中用//////标出的那一行,为什么覆载之后的property没有提供get访问器还是能够访问到?

解决方案 »

  1.   

    你只重载了Set访问器,Get访问器从父类继承。
      

  2.   


    有道理,你重载的是 SET,FarmAnimal 这个基类里面本来就有get方法,所以被继承了.
      

  3.   

    不是重载了整个property麽,难道里面的get和set还得分别重载?
      

  4.   

    属性是可以单独重载的,其实属性就是Get和Set两个方法