1. 先定义一个基类 “人”
/// <summary> /// 定义一个基类human /// 有姓名,性别,年龄等属性,吃饭睡觉等行为 /// 1. 抽象方法必须放在抽象类中 /// 2. 抽象类可以包含实例方法、字段、委托、别的类、事件、属性 /// 3. 抽象类不能被实例化 /// 4. is a /// </summary> public abstract class BaseHuman { public string Name { set; get; } public bool Sex { set; get; } public int Age { set; get; } public void Eat() { Console.WriteLine("{0} have the eat action", this.Name ); } public void Sleep() { Console.WriteLine("{0} have the sleep action", this.Name ); } public abstract void Career(); //因为职业可能不同,所以这里定义了抽象方法,那么类就必须定义成抽象类 }
2. 定义两个类“程序员”和“翻译官”
/// <summary> /// 单继承,多接口 /// </summary> public class Programmer:BaseHuman,Iknowledge { public override void Career() { Console.WriteLine("{0} is a programmer", this.Name); } public void Knowledge() { Console.WriteLine("{0} knows C#, SQL,Html", this.Name); } }
class Translator:BaseHuman,Iknowledge { public override void Career() { Console.WriteLine("{0} is a translator", this.Name ); } public void Knowledge() { Console.WriteLine ("{0} knows Englsh, Franch",this.Name ); } }
3. 定义一个接口
/// <summary> /// 接口只能包含 方法、属性和事件 /// 不能包含字段,实例方法,委托和别的类 /// can do /// </summary> public interface Iknowledge { void Knowledge(); }