1. 程式人生 > >C#中的繼承與覆蓋

C#中的繼承與覆蓋

sta 文章 class static color read con public ner

原文發布時間為:2009-03-03 —— 來源於本人的百度文章 [由搬家工具導入]

//using System;
//using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
   public class Program
    {
       public static void Main()
        {
            Parent p = new Son();//输出 A from Parent! B from Son! 若改成Parent p = new Parent();那输出的都是A from Parent! B from Parent!
            p.A();
            p.B();
            Console.ReadLine();
        }
    }

    class Parent
    {
       public void A()
        {
            Console.WriteLine("A from Parent!");
        }
      public virtual void B()
        {
            Console.WriteLine("B from Parent!");
        }
    }

    class Son : Parent
    {
      public new void A()
        {
            Console.WriteLine("A from Son!");
        }
      public override void B()
        {
            Console.WriteLine("B from Son!");
        }
    }
}

C#中的繼承與覆蓋