Poznámka
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
Explicitní implementace rozhraní také umožňuje programátorovi implementovat dvě rozhraní se stejnými názvy členů a dát každému členu rozhraní samostatnou implementaci. V tomto příkladu se zobrazí rozměry pole v metrice i v anglických jednotkách. Třída Box implementuje dvě rozhraní IEnglishDimensions a IMetricDimensions, které představují různé měrné systémy. Obě rozhraní mají stejné názvy členů, délku a šířku.
Příklad
// 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)
{
_lengthInches = lengthInches;
_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(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:
Console.WriteLine($"Length(in): {eDimensions.Length()}");
Console.WriteLine($"Width (in): {eDimensions.Width()}");
// Print dimensions in metric units:
Console.WriteLine($"Length(cm): {mDimensions.Length()}");
Console.WriteLine($"Width (cm): {mDimensions.Width()}");
}
}
/* Output:
Length(in): 30
Width (in): 20
Length(cm): 76.2
Width (cm): 50.8
*/
Robustní programování
Pokud chcete provést výchozí měření v anglických jednotkách, implementujte metody Length a Width normálně a explicitně implementujte metody Length a Width z rozhraní 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;
V tomto případě můžete přistupovat k anglickým jednotkám z instance třídy a přistupovat k jednotkám metrik z instance rozhraní:
public static void Test()
{
Box box1 = new(30.0f, 20.0f);
IMetricDimensions mDimensions = box1;
Console.WriteLine($"Length(in): {box1.Length()}");
Console.WriteLine($"Width (in): {box1.Width()}");
Console.WriteLine($"Length(cm): {mDimensions.Length()}");
Console.WriteLine($"Width (cm): {mDimensions.Width()}");
}