namespace ConsoleApplication2 { public abstract class Animal { public string GetRootName() { return "动物"; } public abstract string GetName(); } public class Monkey : Animal { public override string GetName() { return "猴子"; } } public class GoldenMonkey : Monkey { public override string GetName() { return "金丝猴"; } } }
class Program { static void Main(string[] args) { Monkey mon = new Monkey(); Console.WriteLine(" Monkey mon = new Monkey()" + mon.GetRootName()); Console.WriteLine(" mon.GetRootName()" + mon.GetRootName()); Animal an1 = new Monkey(); Console.WriteLine(" Animal an1 = new Monkey()" + an1.GetName()); Monkey mon1 = new GoldenMonkey(); Console.WriteLine(" Monkey mon1 = new GoldenMonkey()" + mon1.GetName()); } }
基本原则是 等号左边的对象为主,但如果有override可以要要以等号右边。