산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
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

위의 예에는 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# Interface의 보다 실용적인 예를 살펴보겠습니다.

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. 자바 인터페이스
  3. 자바 컬렉션 프레임워크
  4. 자바 컬렉션 인터페이스
  5. Java NavigableSet 인터페이스
  6. 자바 람다 표현식
  7. 무선 진입로 센서에 대한 인터페이스
  8. 자바 - 인터페이스
  9. Java 9 - 개인 인터페이스 메소드
  10. C# - 인터페이스