多态-接口
接口
接口就是一个规范,能力,在一个类需要多继承时使用
public interface I...able
{
成员;//方法,属性,索引器
}
显示实现接口就是为了解决方法重名问题
为了多态。 接口不能被实例化。 也就是说,接口不能new(不能创建对象)
接口中的成员不能加“访问修饰符”,接口中的成员访问修饰符为public,不能修改。
(默认为public) 接口中的成员不能有任何实现(“光说不做”,只是定义了一组未实现的成员)。
接口中只能有方法、属性、索引器、事件,不能有“字段”和构造函数。
接口与接口之间可以继承,并且可以多继承。
接口并不能去继承一个类,而类可以继承接口 (接口只能继承于接口,而类既可以继承接口,也可以继承类)
实现接口的子类必须实现该接口的全部成员。
一个类可以同时继承一个类并实现多个接口,如果一个子类同时继承了父类A,并实现了接口IA,那么语法上A必须写在IA的前面。
class MyClass:A,IA{},因为类是单继承的。
显示实现接口的目的:解决方法的重名问题 什么时候显示的去实现接口: 当继承的借口中的方法和参数一摸一样的时候,要是用显示的实现接口
当一个抽象类实现接口的时候,需要子类去实现接口。
1.接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _09接口 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //接口就是一个规范、能力。 14 } 15 } 16 17 public class Person 18 { 19 public void CHLSS() 20 { 21 Console.WriteLine("我是人类,我可以吃喝拉撒睡"); 22 } 23 } 24 public class NBAPlayer 25 { 26 public void KouLan() 27 { 28 Console.WriteLine("我可以扣篮"); 29 } 30 } 31 32 public class Student : Person,IKouLanable 33 { 34 public void KouLan() 35 { 36 Console.WriteLine("我也可以扣篮"); 37 } 38 } 39 40 41 public interface IKouLanable 42 { 43 void KouLan(); 44 } 45 46 }
2.接口的语法特征
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _10接口的语法特征 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 14 } 15 } 16 17 public interface IFlyable 18 { 19 //接口中的成员不允许添加访问修饰符 ,默认就是public 20 void Fly(); 21 string Test(); 22 //不允许写具有方法体的函数 23 24 // string _name; 25 string Name 26 { 27 get; 28 set; 29 } 30 31 } 32 }
3.自动属性和普通属性
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _11_自动属性和普通属性 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 } 14 } 15 public class Person 16 { 17 private string _name; 18 19 public string Name 20 { 21 get { return _name; } 22 set { _name = value; } 23 } 24 25 /// <summary> 26 /// 自动属性 27 /// </summary> 28 public int Age 29 { 30 get; 31 set; 32 } 33 } 34 }
4.接口特点
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _12接口特点 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 IFlyable fly = new Bird();//new Person();// new IFlyable(); 14 fly.Fly(); 15 Console.ReadKey(); 16 } 17 } 18 public class Person:IFlyable 19 { 20 public void Fly() 21 { 22 Console.WriteLine("人类在飞"); 23 } 24 } 25 26 27 public class Student:Person, IFlyable 28 { 29 public void Fly() 30 { 31 Console.WriteLine("人类在飞"); 32 } 33 } 34 35 public class Bird : IFlyable 36 { 37 public void Fly() 38 { 39 Console.WriteLine("鸟在飞"); 40 } 41 } 42 43 public interface IFlyable 44 { 45 //不允许有访问修饰符 默认为public 46 //方法、自动属性 47 void Fly(); 48 } 49 50 51 52 public interface M1 53 { 54 void Test1(); 55 } 56 57 public interface M2 58 { 59 void Test2(); 60 } 61 62 public interface M3 63 { 64 void Test3(); 65 } 66 67 68 public interface SupperInterface : M1, M2, M3 69 { 70 71 } 72 73 public class Car : SupperInterface 74 { 75 76 public void Test1() 77 { 78 throw new NotImplementedException(); 79 } 80 81 public void Test2() 82 { 83 throw new NotImplementedException(); 84 } 85 86 public void Test3() 87 { 88 throw new NotImplementedException(); 89 } 90 } 91 92 }
5.接口练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _13接口练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //麻雀会飞 鹦鹉会飞 鸵鸟不会飞 企鹅不会飞 直升飞机会飞 14 //用多态来实现 15 //需方法、抽象类、接口 16 17 18 IFlyable fly = new Plane();//new MaQue();//new YingWu(); 19 fly.Fly(); 20 Console.ReadKey(); 21 22 23 } 24 } 25 26 public class Bird 27 { 28 public double Wings 29 { 30 get; 31 set; 32 } 33 public void EatAndDrink() 34 { 35 Console.WriteLine("我会吃喝"); 36 } 37 } 38 39 public class MaQue : Bird,IFlyable 40 { 41 42 public void Fly() 43 { 44 Console.WriteLine("麻雀会飞"); 45 } 46 } 47 48 public class YingWu : Bird, IFlyable,ISpeak 49 { 50 51 52 public void Fly() 53 { 54 Console.WriteLine("鹦鹉会飞"); 55 } 56 57 public void Speak() 58 { 59 Console.WriteLine("鹦鹉可以学习人类说话"); 60 } 61 } 62 63 64 public class TuoBird : Bird 65 { 66 67 } 68 69 public class QQ : Bird 70 { 71 72 } 73 74 75 public class Plane : IFlyable 76 { 77 public void Fly() 78 { 79 Console.WriteLine("直升飞机转动螺旋桨飞行"); 80 } 81 } 82 83 84 85 public interface IFlyable 86 { 87 void Fly(); 88 89 } 90 91 public interface ISpeak 92 { 93 void Speak(); 94 } 95 96 97 }
6.显示实现接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _14_显示实现接口 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //显示实现接口就是为了解决方法的重名问题 14 IFlyable fly = new Bird(); 15 fly.Fly(); 16 Bird bird = new Bird(); 17 bird.Fly(); 18 19 Console.ReadKey(); 20 } 21 } 22 23 24 public class Bird : IFlyable 25 { 26 public void Fly() 27 { 28 Console.WriteLine("鸟飞会"); 29 } 30 /// <summary> 31 /// 显示实现接口 32 /// </summary> 33 // void IFlyable.Fly() 34 //{ 35 // Console.WriteLine("我是接口的飞"); 36 //} 37 38 } 39 40 public interface IFlyable 41 { 42 void Fly(); 43 } 44 }
7.多态总结
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _15_总结 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //什么时候用虚方法来实现多态? 14 //什么使用用抽象类来实现多态? 15 //什么时候用接口来实现多态? 16 17 //真的鸭子会游泳 木头鸭子不会游泳 橡皮鸭子会游泳 18 19 ISwimming swim = new XPDuck();//new RealDuck(); 20 swim.Swim(); 21 RealDuck rd = new RealDuck(); 22 rd.Swim(); 23 RealDuck xd = new XPDuck(); 24 xd.Swim(); 25 Console.ReadKey(); 26 } 27 } 28 29 public class RealDuck:ISwimming 30 { 31 32 public virtual void Swim() 33 { 34 Console.WriteLine("真的鸭子靠翅膀游泳"); 35 } 36 } 37 38 public class MuDuck 39 { 40 41 } 42 43 public class XPDuck : RealDuck, ISwimming 44 { 45 46 public override void Swim() 47 { 48 Console.WriteLine("橡皮鸭子飘着游泳"); 49 } 50 } 51 52 public interface ISwimming 53 { 54 void Swim(); 55 } 56 }

浙公网安备 33010602011771号