在 C# 中,接口类似于抽象类。但是,与抽象类不同,接口的所有方法都是完全抽象的(没有方法体)。
我们使用 interface
关键字来创建接口。例如,
interface IPolygon {
// method without body
void calculateArea();
}
这里,
- IPolygon 是接口的名称。
- 按照惯例,接口以 I 开头,以便我们仅通过查看其名称就可以识别它。
- 我们不能在接口内部使用访问修饰符。
- 接口的所有成员默认都是公共的。
- 接口不允许字段。
实现接口
我们不能创建接口的对象。要使用接口,其他类必须实现它。与 C# 继承中的情况一样,我们使用 :
符号来实现接口。例如,
using System;
namespace CsharpInterface {
interface IPolygon {
// method without body
void calculateArea(int l, int b);
}
class Rectangle : IPolygon {
// implementation of methods inside interface
public void calculateArea(int l, int b) {
int area = l * b;
Console.WriteLine("Area of Rectangle: " + area);
}
}
class Program {
static void Main (string [] args) {
Rectangle r1 = new Rectangle();
r1.calculateArea(100, 200);
}
}
}
输出
Area of Rectangle: 20000
在上面的示例中,我们创建了一个名为 IPolygon 的接口。该接口包含一个没有实现的方法 calculateArea(int a, int b)
。
在这里,Rectangle 类实现了 IPolygon。并且,提供了 calculateArea(int a, int b)
方法的实现。
注意:我们必须在实现它的类中提供接口所有方法的实现。
实现多个接口
与继承不同,一个类可以实现多个接口。例如,
using System;
namespace CsharpInterface {
interface IPolygon {
// method without body
void calculateArea(int a, int b);
}
interface IColor {
void getColor();
}
// implements two interface
class Rectangle : IPolygon, IColor {
// implementation of IPolygon interface
public void calculateArea(int a, int b) {
int area = a * b;
Console.WriteLine("Area of Rectangle: " + area);
}
// implementation of IColor interface
public void getColor() {
Console.WriteLine("Red Rectangle");
}
}
class Program {
static void Main (string [] args) {
Rectangle r1 = new Rectangle();
r1.calculateArea(100, 200);
r1.getColor();
}
}
}
输出
Area of Rectangle: 20000 Red Rectangle
在上面的示例中,我们有两个接口 IPolygon 和 IColor。
class Rectangle : IPolygon, IColor {
…
}
我们在 Rectangle 类中实现了这两个接口,它们用 ,
分隔。
现在,Rectangle
必须实现这两个接口的方法。
使用接口的引用变量
我们可以使用接口的引用变量。例如,
using System;
namespace CsharpInterface {
interface IPolygon {
// method without body
void calculateArea(int l, int b);
}
class Rectangle : IPolygon {
// implementation of methods inside interface
public void calculateArea(int l, int b) {
int area = l * b;
Console.WriteLine("Area of Rectangle: " + area);
}
}
class Program {
static void Main (string [] args) {
// using reference variable of interface
IPolygon r1 = new Rectangle();
r1.calculateArea(100, 200);
}
}
}
输出
Area of Rectangle: 20000
在上面的示例中,我们创建了一个名为 IPolygon 的接口。该接口包含一个没有实现的方法 calculateArea(int l, int b)
。
IPolygon r1 = new Rectangle();
请注意,我们使用了接口 IPolygon 的引用变量。它指向实现它的类 Rectangle。
虽然我们不能创建接口的对象,但我们仍然可以使用指向其实现类的接口的引用变量。
接口的实际示例
让我们看一个更实际的 C# 接口示例。
using System;
namespace CsharpInterface {
interface IPolygon {
// method without body
void calculateArea();
}
// implements interface
class Rectangle : IPolygon {
// implementation of IPolygon interface
public void calculateArea() {
int l = 30;
int b = 90;
int area = l * b;
Console.WriteLine("Area of Rectangle: " + area);
}
}
class Square : IPolygon {
// implementation of IPolygon interface
public void calculateArea() {
int l = 30;
int area = l * l;
Console.WriteLine("Area of Square: " + area);
}
}
class Program {
static void Main (string [] args) {
Rectangle r1 = new Rectangle();
r1.calculateArea();
Square s1 = new Square();
s1.calculateArea();
}
}
}
输出
Area of Rectangle: 2700 Area of Square: 900
在上面的程序中,我们创建了一个名为 IPolygon 的接口。它有一个抽象方法 calculateArea()
。
我们有两个类 Square 和 Rectangle,它们都实现了 IPolygon 接口。
计算面积的规则对每个多边形都不同。因此,calculateArea()
是在没有实现的情况下包含的。
任何实现 IPolygon 的类都必须提供 calculateArea()
的实现。因此,Rectangle 类中方法的实现独立于 Square 类中方法的实现。
C# 接口的优点
既然我们知道了接口是什么,现在让我们了解一下为什么要在 C# 中使用接口。
- 与抽象类类似,接口帮助我们在 C# 中实现**抽象**。
这里,接口中的方法calculateArea()
没有方法体。因此,它隐藏了方法的实现细节。
- 接口提供了类(实现它的类)必须遵循的**规范**。
在前面的示例中,我们将calculateArea()
用作接口 IPolygon 中的规范。这就像设定一个规则,我们应该计算每个多边形的面积。
现在,任何实现 IPolygon 接口的类都必须为 calculateArea() 方法提供实现。
- 接口用于在 C# 中实现多重继承。
- 接口提供**松耦合**(当我们更改代码的一个部分时,对代码的其他部分没有或影响最小)。
在前面的示例中,如果我们更改 Square 类中calculateArea()
的实现,它不会影响 Rectangle 类。