다음을 통해 공유


방법: 상속을 사용하여 인터페이스 멤버를 명시적으로 구현(C# 프로그래밍 가이드)

업데이트: 2007년 11월

명시적으로 인터페이스를 구현하면 멤버 이름이 동일한 두 개의 인터페이스를 구현하여 각 인터페이스 멤버에 별도의 구현을 제공할 수 있습니다. 다음 예제에서는 상자의 크기를 미터와 인치 단위로 표시합니다. Box 클래스는 서로 다른 단위를 나타내는 두 인터페이스인 IEnglishDimensions 및 IMetricDimensions를 구현합니다. 두 인터페이스에는 모두 동일한 멤버 이름 Length와 Width가 있습니다.

예제

// Declare the English units interface:
interface IEnglishDimensions
{
    float Length();
    float Width();
}

// Declare the metric units interface:
interface IMetricDimensions
{
    float Length();
    float Width();
}

// Declare the Box class that implements the two interfaces:
// IEnglishDimensions and IMetricDimensions:
class Box : IEnglishDimensions, IMetricDimensions
{
    float lengthInches;
    float widthInches;

    public Box(float length, float width)
    {
        lengthInches = length;
        widthInches = width;
    }

    // Explicitly implement the members of IEnglishDimensions:
    float IEnglishDimensions.Length()
    {
        return lengthInches;
    }

    float IEnglishDimensions.Width()
    {
        return widthInches;
    }

    // Explicitly implement the members of IMetricDimensions:
    float IMetricDimensions.Length()
    {
        return lengthInches * 2.54f;
    }

    float IMetricDimensions.Width()
    {
        return widthInches * 2.54f;
    }

    static void Main()
    {
        // Declare a class instance box1:
        Box box1 = new Box(30.0f, 20.0f);

        // Declare an instance of the English units interface:
        IEnglishDimensions eDimensions = (IEnglishDimensions)box1;

        // Declare an instance of the metric units interface:
        IMetricDimensions mDimensions = (IMetricDimensions)box1;

        // Print dimensions in English units:
        System.Console.WriteLine("Length(in): {0}", eDimensions.Length());
        System.Console.WriteLine("Width (in): {0}", eDimensions.Width());

        // Print dimensions in metric units:
        System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
        System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
    }
}
/* Output:
    Length(in): 30
    Width (in): 20
    Length(cm): 76.2
    Width (cm): 50.8
*/

강력한 프로그래밍

인치를 기본 단위로 하려면 메서드 Length와 Width를 정상적으로 구현하고 IMetricDimensions 인터페이스에서 Length와 Width 메서드를 명시적으로 구현해야 합니다.

// Normal implementation:
public float Length()
{
    return lengthInches;
}
public float Width()
{
    return widthInches;
}

// Explicit implementation:
float IMetricDimensions.Length()
{
    return lengthInches * 2.54f;
}
float IMetricDimensions.Width()
{
    return widthInches * 2.54f;
}

이런 경우 인치 단위는 클래스 인스턴스에서 액세스하고 미터 단위는 인터페이스 인스턴스에서 액세스할 수 있습니다.

public static void Test()
{
    Box box1 = new Box(30.0f, 20.0f);
    IMetricDimensions mDimensions = (IMetricDimensions)box1;

    System.Console.WriteLine("Length(in): {0}", box1.Length());
    System.Console.WriteLine("Width (in): {0}", box1.Width());
    System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
    System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
}

참고 항목

작업

방법: 인터페이스 멤버를 명시적으로 구현(C# 프로그래밍 가이드)

개념

C# 프로그래밍 가이드

참조

클래스 및 구조체(C# 프로그래밍 가이드)

인터페이스(C# 프로그래밍 가이드)