Programing

C #에서 클래스는 다른 클래스와 인터페이스에서 상속 할 수 있습니까?

lottogame 2020. 7. 11. 09:59
반응형

C #에서 클래스는 다른 클래스와 인터페이스에서 상속 할 수 있습니까?


클래스가 클래스와 인터페이스에서 상속받을 수 있는지 알고 싶습니다. 아래 예제 코드는 작동하지 않지만 원하는 작업을 전달한다고 생각합니다. 내가하고 싶은 이유는 회사에서 USB, 직렬, 이더넷 등의 장치를 만들기 때문입니다. 모든 응용 프로그램에서 공통 사항 (연결, 연결 끊기, 펌웨어 가져 오기 등)을 동일하게 유지하는 데 도움이되는 모든 장치의 프로그램을 작성하는 데 사용할 수있는 일반적인 구성 요소 / 인터페이스를 개발하려고합니다.

이 질문에 추가하려면 GenericDevice가 다른 프로젝트에있는 경우 해당 프로젝트에 IOurDevices 인터페이스를 넣은 다음 첫 번째 프로젝트에 대한 참조를 추가하면 USBDevice 클래스가 인터페이스를 구현하도록 할 수 있습니까? 하나의 프로젝트를 참조한 다음 장치가 무엇인지에 따라 다른 인터페이스를 구현하고 싶습니다.

class GenericDevice
{
   private string _connectionState;
   public connectionState
   {
      get{return _connectionState; }
      set{ _connectionState = value;}
   }
}

interface IOurDevices
{
   void connectToDevice();
   void DisconnectDevice();
   void GetFirmwareVersion();
}

class USBDevice : IOurDevices : GenericDevice
{
   //here I would define the methods in the interface
   //like this...
   void connectToDevice()
   {
       connectionState = "connected";
   }
}

//so that in my main program I can do this...

class myProgram
{
   main()
   {
      USBDevice myUSB = new USBDevice();
      myUSB.ConnectToDevice;
   }
}

예. 시험:

class USBDevice : GenericDevice, IOurDevice

참고 : 기본 클래스는 인터페이스 이름 목록 앞에 와야합니다.

물론, 인터페이스가 정의한 모든 멤버를 구현해야합니다. 그러나 기본 클래스에 인터페이스 멤버와 일치하는 멤버가 포함 된 경우 기본 클래스 멤버는 인터페이스 멤버의 구현으로 작동 할 수 있으며이를 수동으로 다시 구현할 필요는 없습니다.


아뇨 그러나 클래스에서 상속 하고 하나 이상의 인터페이스를 구현할 수 있습니다.

이와 같은 개념을 논의 할 때는 명확한 용어가 중요합니다. 여기서 볼 수있는 것 중 하나는 여기와 인쇄본에서 Jon Skeet의 글을 표시 할 때 항상 물건을 설명하는 방식이 정확하다는 것입니다.


질문과 관련이 없으며 (Mehrdad의 대답이 당신을 데려 갈 것입니다), 이것이 nitpicky로 간주되지 않기를 바랍니다 : 클래스는 인터페이스를 상속 하지 않고 인터페이스를 구현 합니다.

.NET은 다중 상속을 지원하지 않으므로 용어를 올바르게 유지하면 통신에 도움이 될 수 있습니다. 클래스는 하나의 수퍼 클래스에서 상속 할 수 있으며 원하는 수의 인터페이스를 구현할 수 있습니다.


In response to Eric's comment... I had a discussion with another developer about whether or not interfaces "inherit", "implement", "require", or "bring along" interfaces with a declaration like:

public interface ITwo : IOne

The technical answer is that ITwo does inherit IOne for a few reasons:

  • Interfaces never have an implementation, so arguing that ITwo implements IOne is flat wrong
  • ITwo inherits IOne methods, if MethodOne() exists on IOne then it is also accesible from ITwo. i.e: ((ITwo)someObject).MethodOne()) is valid, even though ITwo does not explicitly contain a definition for MethodOne()
  • ...because the runtime says so! typeof(IOne).IsAssignableFrom(typeof(ITwo)) returns true

We finally agreed that interfaces support true/full inheritance. The missing inheritance features (such as overrides, abstract/virtual accessors, etc) are missing from interfaces, not from interface inheritance. It still doesn't make the concept simple or clear, but it helps understand what's really going on under the hood in Eric's world :-)


I found the answer to the second part of my questions. Yes, a class can implement an interface that is in a different class as long that the interface is declared as public.

참고URL : https://stackoverflow.com/questions/2059425/in-c-can-a-class-inherit-from-another-class-and-an-interface

반응형