标签:
一,父类Animal
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace jichengchonggou { public class Animal { //获得声音的方法 public virtual string GetShoutSound() { return ""; } } }
二,子类Cat和Dog
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace jichengchonggou { public class Dog : Animal { public override string GetShoutSound() { return "旺旺旺旺!!!!!"; } } }
子类二
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace jichengchonggou { public class Cat : Animal { public override string GetShoutSound() { return "喵喵喵喵!!!!!"; } } }
三,输出:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace jichengchonggou { internal class Program { private static void Main(string[] args) //主函数 { Animal animal1 = new Dog(); Animal animal2 = new Cat(); string a1 = animal1.GetShoutSound(); string a2 = animal2.GetShoutSound(); Console.WriteLine(a1 + a2); } } }
标签:
原文地址:http://www.cnblogs.com/May-day/p/5835783.html