Udostępnij za pośrednictwem


Jak: jawnie wdrożenie członków dwa interfejsy (C# Programming Guide)

Jawne interfejsu wykonania pozwala również programistę do wprowadzenia w życie dwa interfejsy, które mają tej samej nazwy składników i podać oddzielne realizacji każdego członka interfejsu.W tym przykładzie wyświetla rozmiary pola zarówno metrykę, jak i jednostek w języku angielskim.Pole klasy implementuje dwa interfejsy, IEnglishDimensions i IMetricDimensions, które reprezentują różne systemy miary.Oba interfejsy mają nazwy składników identyczne, długość i szerokość.

Przykład

// 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
*/

Stabilne programowanie

Jeśli chcesz dokonać pomiarów domyślne w jednostkach angielski, zwykle wykonania metody długość i szerokość i wyraźnie wykonania metody długość i szerokość z interfejsu IMetricDimensions:

// 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;
}

W takim przypadku można dostęp angielskie jednostki z instancji klasy i dostęp do jednostki metryczne z instancji interfejsu:

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());
}

Zobacz też

Zadania

Jak: jawnie zaimplementować interfejsu członków (Podręcznik programowania C#)

Informacje

Klasy i strukturach (Podręcznik programowania C#)

Interfejsy (Podręcznik programowania C#)

Koncepcje

Podręcznik programowania C#