第二十四节 interface接口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
案例1:new 关键字运用 namespace e1 { class A { public void F() { } } class B:A { public new void F() { //在继承结构中,子类要隐藏父类的方法,可以不加new关键字 //但这样编译器会报一个警告,虽然这样可以达到方法隐藏的目的 //但是我们不推荐这样做,好的编写习惯是使用严谨的语法尽可能的避免错误的发生 } //public void F() //{ //} } class Program { static void Main(string[] args) { } } ———————————————————————————— 案例2:接口 namespace e2 { //访问修饰符 interface 接口名 //{ // 内容:(接口内所有内容都是抽象的,无具体实现) //} public interface IHomeworkCollector { //接口里所有的内容都是公开的,所以不必定义访问修饰符 void HomeworkCollector(); } public class Person { protected string name; public Person(string name) { this.name = name; } } public class Student:Person,IHomeworkCollector { public Student(string name) : base(name) { } public void HomeworkCollector() { Console.WriteLine("{0}开始收作业",name); } } public class Teacher : Person, IHomeworkCollector { public Teacher (string name) : base(name) { } public void HomeworkCollector() { Console.WriteLine("你怎么还在抄作业啊啊啊啊!!!,{0}老师开始收作业", name); } } public class Adviser : Person, IHomeworkCollector { public Adviser(string name) : base(name) { } public void HomeworkCollector() { Console.WriteLine("{0},班主任来收作业了", name); } } class Program { //接口当做参数来用 public void ExecutHomeworkCollector(IHomeworkCollector ic) { ic.HomeworkCollector(); } //接口当做返回值使用 public IHomeworkCollector CreateHomeworkCollector(string type) { switch (type.Trim()) { case "student": return new Student("王五"); case "teacher": return new Teacher("依依"); case "adviser": return new Adviser("散散"); default: return null; } } static void Main(string[] args) { //知识点 //接口不能实例化 //IHomeworkCollector ic = new IHomeworkCollector(); //方法4 Program p = new Program(); IHomeworkCollector ic = p.CreateHomeworkCollector("student"); ic.HomeworkCollector(); ic = p.CreateHomeworkCollector("adviser"); ic.HomeworkCollector(); //方法3 //Program p = new Program(); //p.ExecutHomeworkCollector(new Student("田科")); //p.ExecutHomeworkCollector(new Teacher("王启")); //p.ExecutHomeworkCollector(new Adviser("我靠!!!")); //方法1: //IHomeworkCollector ic = new Student("张三"); //ic.HomeworkCollector(); //IHomeworkCollector ic = new Teacher("李四"); //ic.HomeworkCollector(); //方法2 ////List<IHomeworkCollector> list = new List<IHomeworkCollector>(); ////list.Add(new Student("张三")); ////list.Add(new Teacher("李")); ////foreach (IHomeworkCollector ic in list) ////{ //// ic.HomeworkCollector(); ////} Console.ReadKey(); } } } —————————————————————————————— 案例3存取款 namespace e3 { interface IBankAccount { //在接口中定义一组规范(契约) //要求所有银行的账户系统按照该接口的规范来开发 //取款 void PayIn(decimal amount); //存款 bool WithDrow(decimal amount); //余额 decimal Balance { get; } } } ———————————————————————— namespace e3 { class ICBAccount:ITransferBankAcount { private decimal balance; public void PayIn(decimal amount) { balance += amount; } public bool WithDrow(decimal amount) { if (balance >= amount) { balance -= amount; return true; } else { Console.WriteLine("余额不足.........."); return false; } } //转账 public void TransferAcount(IBankAccount account, decimal amount) { bool flag= this.WithDrow(amount); if (flag) { account.PayIn(amount); } } public decimal Balance { set { balance = value; } get { return balance; } } } } ———————————————————————————————— namespace e3 { interface ITransferBankAcount:IBankAccount { //转账 void TransferAcount(IBankAccount account, decimal amount); } } ———————————————————————— namespace e3 { class Program { static void Main(string[] args) { ITransferBankAcount ia = new ICBAccount(); ia.PayIn(2000); ia.WithDrow(500); Console.WriteLine("余额:{0}", ia.Balance); IBankAccount ia2 = new ICBAccount(); ia.TransferAcount(ia2, 500); Console.WriteLine("ia的余额:{0}",ia.Balance); Console.WriteLine("ia2的余额:{0}", ia2.Balance); Console.ReadKey(); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
案例1:习题 namespace l1 { class Mammal { public virtual void SayHi() { Console.WriteLine("我是怪兽!!!"); } } class Anmainal : Mammal { public override void SayHi() { Console.WriteLine("我是一只野兽!!!"); } } class Program { static void Main(string[] args) { Anmainal an = new Anmainal(); an.SayHi(); Console.ReadKey(); } } } —————————————————————————————————— namespace l1 { class Mammal { public override string ToString() { return "我是一只野兽"; } } class Program { static void Main(string[] args) { Mammal mammal = new Mammal(); Console.WriteLine(mammal); Console.WriteLine(mammal.ToString()); Console.ReadKey(); } } } —————————————————————————————————————— 案例2: namespace l2 { abstract class Vertebrata { public abstract void ShowMyself(); } } —————————————————————— namespace l2 { class Amphibian:Vertebrata { public override void ShowMyself() { Console.WriteLine(); } } class Frog:Amphibian { public override void ShowMyself() { Console.WriteLine("\n @..@" +"\n ()" +"\n(.>__<.)" +"\n^^^ ^^^"); } } } —————————————————————————— namespace l2 { class Bird : Vertebrata { public override void ShowMyself() { Console.WriteLine(" _," + "\n-==<' `\\" + "\n ) /" + "\n /(_." + "\n | ,-,`\\" + "\n \\\\ \\ \\" + "\n \\, \\ \\" + "\n ||\\ \\`|," + "\n _|| `=`-'" + "\n ~~`~`"); } } class Duck : Bird { public override void ShowMyself() { Console.WriteLine("\n >(' )" +"\n )/" +"\n /( " +" \n/ `----/ " +"\n\\ ~=- /"); } } } —————————————————————————————————— namespace l2 { class Mammal:Vertebrata { public override void ShowMyself() { Console.WriteLine(); } } class Rabbit:Mammal { public override void ShowMyself() { Console.WriteLine(); } } class Human : Mammal { public override void ShowMyself() { Console.WriteLine("╭∩╮(︶︿︶)╭∩╮鄙视你!"); } } class Cattle : Mammal { public override void ShowMyself() { Console.WriteLine("\n(___)" +"\n(o o)_____/" +"\n @@ ` \\ " +"\n \\____, /" +"\n // // " + "\n^^ ^^ "); } } class Pig:Mammal { public override void ShowMyself() { Console.WriteLine(); } } } —————————————————————————— namespace l2 { class Program { static void Main(string[] args) { Bird bd = new Bird(); bd.ShowMyself(); Cattle cl=new Cattle(); cl.ShowMyself(); Frog fg = new Frog(); fg.ShowMyself(); Duck dk = new Duck(); dk.ShowMyself(); Console.ReadKey(); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
案例1:习题 namespace l3 { abstract class Vertebrata { public abstract void ShowMyself(); } } —————————————————————————————— namespace l3 { class Mammal:Vertebrata { public override void ShowMyself() { Console.WriteLine(); } } class Rabbit:Mammal { public override void ShowMyself() { Console.WriteLine(); } } class Human : Mammal { public override void ShowMyself() { Console.WriteLine("╭∩╮(︶︿︶)╭∩╮鄙视你!"); } } class Cattle : Mammal { public override void ShowMyself() { Console.WriteLine("\n(___)" +"\n(o o)_____/" +"\n @@ ` \\ " +"\n \\____, /" +"\n // // " + "\n^^ ^^ "); } } class Pig:Mammal { public override void ShowMyself() { Console.WriteLine(); } } } —————————————————————————— namespace l3 { class Bird : Vertebrata { public override void ShowMyself() { Console.WriteLine(" _," + "\n-==<' `\\" + "\n ) /" + "\n /(_." + "\n | ,-,`\\" + "\n \\\\ \\ \\" + "\n \\, \\ \\" + "\n ||\\ \\`|," + "\n _|| `=`-'" + "\n ~~`~`"); } } class Duck : Bird { public override void ShowMyself() { Console.WriteLine("\n >(' )" +"\n )/" +"\n /( " +" \n/ `----/ " +"\n\\ ~=- /"); } } } —————————————————————————— namespace l3 { class Amphibian:Vertebrata { public override void ShowMyself() { Console.WriteLine(); } } class Frog:Amphibian { public override void ShowMyself() { Console.WriteLine("\n @..@" +"\n ()" +"\n(.>__<.)" +"\n^^^ ^^^"); } } } —————————————————————————————— namespace l3 { class Program { static void Main(string[] args) { Human jean = new Human(); Vertebrata someone = jean; if(someone is Human) Console.WriteLine("someone is Human"); if(someone is Mammal) Console.WriteLine("someone is Mammal"); if(someone is Vertebrata) Console.WriteLine("someone is Vertebrata"); Console.ReadKey(); } } } ———————————————————————————————————————————————————————————————————————————————— 案例2: namespace l4 { class Vertebrata { } class Human:Vertebrata { public void Work() { Console.WriteLine("说话"); } } class Program { static void Main(string[] args) { //人属于人类,人也属于脊椎动物,但脊椎动物不能有人说话的能力 //Human jean = new Human(); //Vertebrata someone = jean; //someone.Work(); Human jean = new Human(); Vertebrata someone = jean; Human people = someone as Human; if (people != null) { people.Work(); } Console.ReadKey(); } } } —————————————————————————————— 案例3 namespace l5 { interface IDriveable { void Drive(); } class BusDriver:IDriveable { public void Drive() { Console.WriteLine("有经验的司机可以驾驶公共汽车"); } } class TractorDriver:IDriveable { public void Drive() { Console.WriteLine("拖拉机的司机可以去玩"); } } class Program { static void Main(string[] args) { IDriveable id = new BusDriver(); id.Drive(); IDriveable ic = new TractorDriver(); ic.Drive(); Console.ReadKey(); } } }