详谈接口
一、接口
1、概念
接口是类之间交互内容的一个抽象,把类之间需要交互的内容抽象出来定义成接口,可以更好的控制类之间的逻辑交互。
2、特性
接口类似于抽象基类。继承接口的任何非抽象类型都必须实现接口的所有成员。
不能直接实例化接口。
接口可以包含事件、索引器、方法和属性。
接口不包含方法的实现。
类和结构可从多个接口继承。
接口自身可从多个接口继承。
接口只包含成员定义,不包含成员的实现,成员的实现需要在继承的类或者结构中实现。接口的成员包括方法、属性、索引器和事件,但接口不包含字段。
3、声明接口
访问修饰符(一般public) interface 接口类名
4、实现接口
声明接口之后,类就可以通过继承接口来实现其中的抽象方法。继承接口的语法同类的继承类似,使用冒号“:”,将待继承的接口放在类的后面。如果继承多个接口,将使用逗号将其分隔。
访问修饰符 class : 接口类名{}
二、接口与抽象类
接口与抽象类非常相似,它定义了一些未实现的属性和方法。所有继承它的类都继承这些成员,在这个角度上,可以把接口理解为一个类的模板。
相似之处:
两者都包含可以由子类继承的抽象成员;
两者都不直接实例化。
两者的区别:
抽象类除拥有抽象成员之外,还可以拥有非抽象成员;而接口所有的成员都是抽象的。
抽象成员可以是私有的,而接口的成员一般都是公有的。
接口中不能含有构造函数、析构函数、静态成员和常量。
C#只支持单继承,即子类只能继承一个父类,而一个子类却能够继承多个接口。
三、示例
示例1.设计一个用于算术四则运算的ICompute接口。子类实现该接口,并测试。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 接口 7 { 8 public interface ICompute 9 { 10 double Add(double num1, double num2); 11 double Div(double num1, double num2); 12 } 13 class MyCompute:ICompute 14 { 15 public double Add(double num1,double num2) 16 { 17 18 return num1 + num2; 19 } 20 public double Div(double num1,double num2) 21 { 22 if (num2==0) 23 { 24 throw new Exception("除数为0"); 25 } 26 else 27 { 28 return num1/num2; 29 } 30 } 31 } 32 class Program 33 { 34 static void Main(string[] args) 35 { 36 MyCompute mc = new MyCompute(); 37 Console.WriteLine(mc.Div(1, 0)); 38 } 39 } 40 }
示例2.定义一个接口IShape包含方法Area()、SetData();实现CTriangle类、CRect类、CCircle类;
定义一个CArea类计算所有的面积之和,并且可以利用构造函数设置各成员的值
IShape接口定义及实现CTriangle类、CRect类、CCircle类;
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Part4 7 { 8 interface IShape 9 { 10 void SetData(); 11 double Area(); 12 } 13 class CTriangle : IShape 14 { 15 public double a; 16 public double b; 17 public double c; 18 public void SetData() 19 { 20 Console.WriteLine("分别输出三角形的三条边长:"); 21 double aa=double.Parse(Console.ReadLine()); 22 double bb = double.Parse(Console.ReadLine()); 23 double cc = double.Parse(Console.ReadLine()); 24 this.a = aa; 25 this.b = bb; 26 this.c = cc; 27 } 28 public double Area() 29 { 30 double p = (this.a + this.b + this.c) / 2.0; 31 double s = Math.Sqrt(p * (p - a) * (p - b) * (p - c)); 32 Console.WriteLine("三角形的面积为:{0}", s); 33 return s; 34 } 35 } 36 class CRect : IShape 37 { 38 public double a; 39 public double b; 40 public void SetData() 41 { 42 Console.WriteLine("分别输出矩形的长和宽:"); 43 double aa = double.Parse(Console.ReadLine()); 44 double bb = double.Parse(Console.ReadLine()); 45 this.a = aa; 46 this.b = bb; 47 } 48 public double Area() 49 { 50 double s = a * b; 51 Console.WriteLine("矩形的面积为:{0}", s); 52 return s; 53 } 54 } 55 class CCircle : IShape 56 { 57 public double r; 58 public void SetData() 59 { 60 Console.WriteLine("输入圆的半径:"); 61 double rr = double.Parse(Console.ReadLine()); 62 this.r = rr; 63 } 64 public double Area() 65 { 66 double s = Math.PI * r* r; 67 Console.WriteLine("圆的面积为:{0}", s); 68 return s; 69 } 70 } 71 72 }
定义一个CArea类计算所有的面积之和,并且可以利用构造函数设置各成员的值
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Part4 7 { 8 class CArea 9 { 10 public double tt; 11 public double rr; 12 public double cc; 13 public CArea() 14 { 15 IShape s; 16 s = new CTriangle(); 17 s.SetData(); 18 this.tt = s.Area(); 19 s = new CRect(); 20 s.SetData(); 21 this.rr = s.Area(); 22 s = new CCircle(); 23 s.SetData(); 24 this.cc=s.Area(); 25 } 26 public void SumArea() 27 { 28 double sum = tt + rr + cc; 29 Console.WriteLine("三个面积的总和为:{0}+{1}+{2}={3}", tt, rr, cc, sum); 30 } 31 } 32 }
定义一个主类调用
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Part4 7 { 8 class Test 9 { 10 static void Main(string[] args) 11 { 12 CArea ca = new CArea(); 13 ca.SumArea(); 14 while (true) ; 15 } 16 } 17 }
结果


浙公网安备 33010602011771号