工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
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

上記の例では、2 つのインターフェース 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() があります .

2 つのクラス Square があります と長方形 IPolygon を実装する インターフェース。

面積の計算ルールはポリゴンごとに異なります。したがって、 calculateArea() 実装なしで含まれています。

IPolygon を実装するクラス calculateArea() の実装を提供する必要があります .したがって、クラス Rectangle でのメソッドの実装 クラス Square のメソッドから独立しています .


C# インターフェースの利点

インターフェイスとは何かがわかったので、C# でインターフェイスが使用される理由について学びましょう。


C言語

  1. コマンドラインインターフェイス
  2. Java インターフェイス
  3. Java コレクション フレームワーク
  4. Java コレクション インターフェイス
  5. Java キュー インターフェイス
  6. Java NavigableSet インターフェイス
  7. Java ラムダ式
  8. ワイヤレスドライブウェイセンサーへのインターフェース
  9. Java-インターフェース
  10. Java 9 - プライベート インターフェイス メソッド
  11. C# - インターフェイス