天天看點

了解多态性的一個小例子(C#)

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication2

{

class Program

class OneBase

protected virtual string GetName()

return "OneBase";

}

public void PrintName()

Console.WriteLine(GetName());

class A : OneBase

protected override string GetName()

return "A";

class B : OneBase

return "B";

static void Main(string[] args)

List<OneBase> baseLst = new List<OneBase>();

B b = new B();

baseLst.Add(b);

A a = new A();

baseLst.Add(a);

foreach (OneBase o in baseLst)

o.PrintName();

Console.ReadLine();

本文轉自 xchsp 51CTO部落格,原文連結:http://blog.51cto.com/freebird/687319,如需轉載請自行聯系原作者