Jak jawnie implementować elementy członkowskie dwóch interfejsów (Przewodnik programowania w języku C#)
Implementacja jawnego interfejsu umożliwia również programistom zaimplementowanie dwóch interfejsów, które mają te same nazwy elementów członkowskich i nadanie każdemu członkowi interfejsu oddzielnej implementacji. W tym przykładzie przedstawiono wymiary pola zarówno w jednostkach metryk, jak i w języku angielskim. Klasa Box implementuje dwa interfejsy IEnglishDimensions i IMetricDimensions, które reprezentują różne systemy pomiarów. Oba interfejsy mają identyczne nazwy składowych, 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 lengthInches, float widthInches)
{
this.lengthInches = lengthInches;
this.widthInches = widthInches;
}
// Explicitly implement the members of IEnglishDimensions:
float IEnglishDimensions.Length() => lengthInches;
float IEnglishDimensions.Width() => widthInches;
// Explicitly implement the members of IMetricDimensions:
float IMetricDimensions.Length() => lengthInches * 2.54f;
float IMetricDimensions.Width() => 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 = box1;
// Declare an instance of the metric units interface:
IMetricDimensions mDimensions = 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
*/
Niezawodne programowanie
Jeśli chcesz ustawić domyślne miary w jednostkach języka angielskiego, zaimplementuj metody Length i Width normalnie i jawnie zaimplementuj metody Length i Width z interfejsu IMetricDimensions:
// Normal implementation:
public float Length() => lengthInches;
public float Width() => widthInches;
// Explicit implementation:
float IMetricDimensions.Length() => lengthInches * 2.54f;
float IMetricDimensions.Width() => widthInches * 2.54f;
W takim przypadku możesz uzyskać dostęp do jednostek języka angielskiego z wystąpienia klasy i uzyskać dostęp do jednostek metryk z wystąpienia interfejsu:
public static void Test()
{
Box box1 = new Box(30.0f, 20.0f);
IMetricDimensions mDimensions = 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());
}