C#繼承、抽象類、介面的知識(簡易題目)
阿新 • • 發佈:2019-02-13
abstract class Animal { private string name; public string Name { get { return name; } set { name = value; } } public abstract void Eat(); public abstract void Move(); public Animal(string name) { this.Name = name; } } //各種介面 interface IJump { void Junp(); } interface IFly { void Fly(); } interface ISwim { void Swim(); } /// <summary> /// 蛇繼承動物類繼承遊介面 /// </summary> class Snake : Animal, ISwim { public Snake(string name) : base(name) { } public override void Eat() { Console.WriteLine("吞"); } public override void Move() { Console.WriteLine("爬行"); } public void Swim() { Console.WriteLine("遊動"); } } /// <summary> /// 鳥繼承動物類繼承飛的介面 /// </summary> class Bird : Animal, IFly { public Bird(string name) : base(name) { } public override void Eat() { Console.WriteLine("捕食蟲子"); } public void Fly() { Console.WriteLine("飛"); } public override void Move() { Console.WriteLine("飛"); } } /// <summary> /// 飛行員繼承了動物類繼承了飛跳遊3個介面 /// </summary> class Flyer : Animal, IFly, IJump, ISwim { public Flyer(string name ):base(name) { } public override void Eat() { } public override void Move() { } public void Fly() { } public void Junp() { } public void Swim() { } }