亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C#接口

C#接口

在本教程中,我们将通过示例了解 C# 接口。

在C#中,接口类似于抽象类。但是,与抽象类不同,接口的所有方法都是完全抽象的(没有主体的方法)。

我们使用 interface 创建接口的关键字。例如,

interface IPolygon {

  // method without body
  void calculateArea();
}

在这里,


实现接口

我们不能创建接口的对象。要使用接口,其他类必须实现它。和 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

在上面的例子中,我们有两个接口,IPolygonIColor .

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语言

  1. 命令行界面
  2. Java 接口
  3. Java 集合框架
  4. Java 集合接口
  5. Java 队列接口
  6. Java 双端队列接口
  7. Java NavigableSet 接口
  8. Java Lambda 表达式
  9. 无线车道传感器接口
  10. Java - 接口
  11. Java 9 - 私有接口方法
  12. C# - 接口