1.什么是接口
接口是指定一组函数成员而不实现他们的引用类型,
class CA { public string name; public int age; }
class CB { public string First; public string Last; public double PersonsAge; }
class Program { static void Main(string[] args) { CA a = new CA() { name = "holliszzz", age = 25 }; PrintInfo(a); Console.ReadKey(); } static void PrintInfo(CA item) { Console.WriteLine("Name {0} , Age {1}", item.name, item.age); } //只有传入的是CA对象才会正常工作,传入CB对象就不行; //因为PrintInfo形参指明实参必须为CA类型,且CB与CA结构不同, }
解决办法,
interface Info// 声明接口 { string GetName(); string GetAge(); }
class CA : Info { public string name; public int age; public string GetName() //实现两个接口方法 { return name; } public string GetAge() { return age.ToString(); } }
class CB : Info { public string First; public string Last; public double PersonsAge; public string GetName()// 实现两个接口方法 { return First + "" + Last; } public string GetAge() { return PersonsAge.ToString(); } }
class Program { static void Main(string[] args) { CA a = new CA() { name = "张三", age = 25 }; CB b = new CB() { First = "李", Last = "四", PersonsAge = 26 }; PrintInfo(a); PrintInfo(b); Console.ReadKey(); } static void PrintInfo(Info item)// 传入接口引用 { Console.WriteLine("name = {0} , age = {1}", item.GetName(), item.GetAge()); } }
2.icomparable_接口示例
class MyClass : IComparable// 类实现引用 { public int TheValue; public int CompareTo(object obj)// 实现方法 { MyClass mc = (MyClass)obj; if (this.TheValue < mc.TheValue) return -1; if (this.TheValue > mc.TheValue) return 1; return 0; } }
class Program { static void Main(string[] args) { //var MyInt = new[] { 1, 3, 2, 4, 5 }; //Array.Sort(MyInt); //foreach (var item in MyInt) //{ // Console.Write("{0}", item); //} //Console.ReadKey(); var myInt = new[] { 1, 4, 7, 9, 2 }; MyClass[] mcArr = new MyClass[5]; for (int i = 0; i < 5; i++) { mcArr[i] = new MyClass(); mcArr[i].TheValue = myInt[i]; } PrintOut("初始化数组{0}", mcArr); Array.Sort(mcArr); PrintOut("排序后数组{1}", mcArr); Console.ReadKey(); } static void PrintOut(string s, MyClass[] mc) { Console.Write(s); foreach (var item in mc) { Console.Write("{0}", item.TheValue); } Console.WriteLine(""); } }
3.接口声明
static void Main(string[] args) { //接口声明不能包含以下成员:数据成员,静态成员, //接口声明只能包含以下非静态成员:方法,属性,事件,索引器, //函数成员声明不能包含任何代码实现,且实用分号结尾, //接口声明可以有任何的访问修饰符,接口成员是隐式的public,不允许写任何访问修饰符包括public, }
4.接口实现
interface Info// 声明接口 { void PrintOut(string s); }
class MyClass : Info// 实现接口 { public void PrintOut(string s) { Console.WriteLine("简单接口调用示例{0}", s); } }
class Program { static void Main(string[] args) { MyClass mc = new MyClass();// 创建实例调用, mc.PrintOut("Test"); Console.ReadKey(); //如果类实现了接口,就必须实现接口的所有成员, //如果类从基类继承并实现接口,基类名称必须放在所有接口名称之前, } }
5.接口是引用类型
class Program { static void Main(string[] args) { MyClass mc = new MyClass(); mc.PrintOut("Object"); Info inf = (Info)mc; // 将类对象的引用转换为接口类型的引用, //Info i = mc as Info; //as 运算符的使用 inf.PrintOut("Interface"); Console.ReadKey(); } } interface Info { void PrintOut(string s); } class MyClass : Info { public void PrintOut(string s) { Console.WriteLine("Calling through {0}", s); } }
6.派生成员作为实现
class Program { static void Main(string[] args) { //实现接口的类可以从它的基类继承实现的代码, Derived d = new Derived(); d.PrintOut("yes"); Console.ReadKey(); } } interface info { void PrintOut(string s); } class MyBaseClass { public void PrintOut(string s) { Console.WriteLine("不实现接口 {0}", s); } } class Derived : MyBaseClass, info { }
7.显示成员接口实现
class Program { static void Main(string[] args) { //单个类可以实现多个接口需要的所有成员, //我们希望每一个接口分离实现该怎么做,即创建显示接口成员实现, MyClass mc = new MyClass(); // 创建类对象 Info1 i1 = (Info1)mc; //获取Info1的引用 i1.PrintOut("Interface1"); //调用显示实现 Info2 i2 = (Info2)mc; i2.PrintOut("Interface2"); Console.ReadKey(); } } interface Info1 // 声明接口 { void PrintOut(string s); } interface Info2 { void PrintOut(string s); } class MyClass : Info1, Info2 { void Info1.PrintOut(string s) // 限定接口名 { Console.WriteLine("Info1 {0}", s); } void Info2.PrintOut(string s) { Console.WriteLine("Info2 {0}", s); } }
class MyClass : Info1 { //显示接口成员实现只可以通过指向接口的引用来访问, //即使是类中的另一成员也不可以直接访问显示实现, void Info1.PrintOut(string s) { Console.WriteLine("Info1"); } public void Test() { ((Info1)this).PrintOut("successful");//*** } //强制转换当前对象的引用(this)为接口类型的引用,并使用指向接口的引用来调用显示接口实现, //其其它类成员不能直接访问显示接口成员实现,他们必须通过接口的引用来访问, }
8.接口可以继承接口
class Program { static void Main(string[] args) { MyData md = new MyData(); md.SetData(5); Console.WriteLine("value = {0}", md.GetData()); Console.ReadKey(); } } interface IDataRetrieve { int GetData(); } interface IDataStore { void SetData(int x); } interface IDataIO : IDataRetrieve, IDataStore { } class MyData : IDataIO { int x; public int GetData() { return x; } public void SetData(int n) { x = n; } }
9.不同类实现一个接口示例
class Program { static void Main(string[] args) { Animal[] a = new Animal[3]; //创建Animal数组, a[0] = new Cat(); a[1] = new Dog(); a[2] = new Bird(); foreach (Animal item in a) //var = Animal 在数组a中循环, { ILiveBirth ilb = item as ILiveBirth; if (ilb != null) { Console.WriteLine("Baby is called {0}", ilb.BabyCalled()); } } Console.ReadKey(); } } interface ILiveBirth //声明接口 { string BabyCalled(); } class Animal //声明基类 { } class Cat : Animal, ILiveBirth //声明Cat类 { string ILiveBirth.BabyCalled() { return "miao"; } } class Dog : Animal, ILiveBirth //声明Dog类 { string ILiveBirth.BabyCalled() // ILiveBirth手动敲打的, { return "wang"; } } class Bird : Animal //声明Bird类 { }