google0312 发表于 2016-10-11 13:47:37

C#入门基础——覆盖其它成员类型


namespace 覆盖其它成员类型
{
    class Program
    {
      static void Main(string[] args)
      {
            MyDerivedClass derived = new MyDerivedClass();    //MyDerivedClass实例化
            MyBaseClass mybc = derived;                      //MyBaseClass实例化
            Console.WriteLine(derived.MyProperty);
            Console.WriteLine(mybc.MyProperty);
            Console.ReadKey();
      }
    }
    class MyBaseClass
    {
      private int myint = 5;
         virtual public int MyProperty
      {
            get                      //属性
            {
                return myint;
            }
      }
    }
    class MyDerivedClass : MyBaseClass
    {
      private int myint = 10;
         override public int MyProperty
      {
            get                        //属性
            {
                return myint;   
            }
      }
    }
}

输出结果:
10
10

match123_xbd 发表于 2023-4-3 14:29:21

{:7_141:}
页: [1]
查看完整版本: C#入门基础——覆盖其它成员类型