C#接口
C#接口
在本教程中,我们将通过示例了解 C# 接口。
在C#中,接口类似于抽象类。但是,与抽象类不同,接口的所有方法都是完全抽象的(没有主体的方法)。
我们使用 interface
创建接口的关键字。例如,
interface IPolygon {
// method without body
void calculateArea();
}
在这里,
- 多边形 是接口的名称。
- 按照惯例,interface 以 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)
没有实现。
这里,矩形 类实现 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 和 矩形 实现 IPolygon 界面。
每个多边形的面积计算规则是不同的。因此,calculateArea()
包含但未实现。
任何实现 IPolygon 的类 必须提供 calculateArea()
的实现 .因此,在类 Rectangle 中实现该方法 独立于类 Square 中的方法 .
C#接口的优点
现在我们知道了接口是什么,让我们了解一下为什么在 C# 中使用接口。
- 类似于抽象类,接口帮助我们实现C#中的抽象 .
这里,方法calculateArea()
在界面里面,没有body。因此,它隐藏了方法的实现细节。
- 接口提供规范 必须遵循一个类(实现它)。
在我们之前的示例中,我们使用了calculateArea()
作为接口 IPolygon 内的规范 .这就像设置一个规则,我们应该计算每个多边形的面积。
现在任何实现 IPolygon 的类 接口必须提供 calculateArea() 的实现 方法。
- 接口用于在 C# 中实现多重继承。
- 接口提供松散耦合 (当我们更改代码的一部分时,对代码的其他部分没有影响或影响最小)。
在我们之前的例子中,如果我们改变calculateArea()
的实现 在广场 类它不影响 Rectangle 类。
C语言