산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> C 언어

C# - 인터페이스

인터페이스는 인터페이스를 상속하는 모든 클래스가 따라야 하는 구문적 계약으로 정의됩니다. 인터페이스는 '무엇'을 정의합니다. 구문적 계약의 일부와 파생 클래스는 '방법'을 정의합니다. 구문적 계약의 일부입니다.

인터페이스는 인터페이스의 구성원인 속성, 메서드 및 이벤트를 정의합니다. 인터페이스에는 멤버 선언만 포함됩니다. 멤버를 정의하는 것은 파생 클래스의 책임입니다. 파생 클래스가 따를 표준 구조를 제공하는 데 도움이 되는 경우가 많습니다.

추상 클래스는 어느 정도 같은 목적을 제공하지만 기본 클래스에서 소수의 메서드만 선언하고 파생 클래스가 기능을 구현하는 경우에 주로 사용됩니다.

인터페이스 선언

인터페이스는 interface 키워드를 사용하여 선언됩니다. 클래스 선언과 유사합니다. 인터페이스 문은 기본적으로 공개됩니다. 다음은 인터페이스 선언의 예입니다 -

public interface ITransactions {
   // interface members
   void showTransaction();
   double getAmount();
}

다음 예는 위 인터페이스의 구현을 보여줍니다 -

라이브 데모
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;

namespace InterfaceApplication {
   
   public interface ITransactions {
      // interface members
      void showTransaction();
      double getAmount();
   }
   public class Transaction : ITransactions {
      private string tCode;
      private string date;
      private double amount;
      
      public Transaction() {
         tCode = " ";
         date = " ";
         amount = 0.0;
      }
      public Transaction(string c, string d, double a) {
         tCode = c;
         date = d;
         amount = a;
      }
      public double getAmount() {
         return amount;
      }
      public void showTransaction() {
         Console.WriteLine("Transaction: {0}", tCode);
         Console.WriteLine("Date: {0}", date);
         Console.WriteLine("Amount: {0}", getAmount());
      }
   }
   class Tester {
     
      static void Main(string[] args) {
         Transaction t1 = new Transaction("001", "8/10/2012", 78900.00);
         Transaction t2 = new Transaction("002", "9/10/2012", 451900.00);
         
         t1.showTransaction();
         t2.showTransaction();
         Console.ReadKey();
      }
   }
}

위의 코드를 컴파일하고 실행하면 다음과 같은 결과가 생성됩니다. -

Transaction: 001
Date: 8/10/2012
Amount: 78900
Transaction: 002
Date: 9/10/2012
Amount: 451900

C 언어

  1. 명령줄 인터페이스
  2. C# 인터페이스
  3. 자바 인터페이스
  4. 자바 컬렉션 인터페이스
  5. Java NavigableSet 인터페이스
  6. 자바 람다 표현식
  7. 무선 진입로 센서에 대한 인터페이스
  8. 자바 - 인터페이스
  9. C++의 인터페이스(추상 클래스)
  10. Java 9 - 개인 인터페이스 메소드