2.1
2017-09-26 08:42 xx-- 阅读(223) 评论(0) 编辑 收藏 举报
Animal.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test { class Animal //创建基类 { private bool m_sex; private int m_age; public bool Sex { get { return m_sex; } set { m_sex = value; } } public int Age { get { return m_age; } set { m_age = value; } } public Animal() { Sex = false; } public string introduce() { if (Sex) return "This is a male Animal!"; else return "This is a female Animal!"; } } class Dog:Animal //创建派生类并继承Animal { public Dog() { Sex = true; } public new string introduce() //重载introduce函数 { if (Sex) return "This is a male Dog!"; else return "This is a female Dog!"; } } class Cat : Animal //创建派生类并继承Animal { public Cat() { Sex = true; } public new string introduce() //重载introduce函数 { if (Sex) return "This is a male Cat!"; else return "This is a female Cat!"; } } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test { class Program { static void Main(string[] args) { Animal ani = new Animal(); Console.WriteLine(ani.introduce()); Dog dog = new Dog(); Console.WriteLine(ani.introduce()); Cat cat = new Cat(); Console.WriteLine(ani.introduce()); Console.WriteLine("按回车键结束"); Console.ReadLine(); } } }