PDA

View Full Version : سوال: Explicit Interface Implementations دقیقا چیست و چطور می توان از آن استفاده نمود؟



rasol_afkham
شنبه 29 فروردین 1394, 23:19 عصر
با سلام خدمت دوستان:
اگر در راهنمای MSDN در قسمت Member Class دیده باشید در پایین صفحه جدولی قرار دارد با سر تیتر
Explicit Interface Implementations
می خواستم بدانم که گزینه های این قسمت چیست؟ و چطور می توان از آنها استفاده نمود؟

salar IT man
شنبه 29 فروردین 1394, 23:52 عصر
یکی از روش های پیاده سازی اینترفیس ها توسط کلاس ها محسوب میشود . اگر کلاسی به صورت صریح (Explicit) اینترفیسی را پیاده کند در آن صورت استفاده کننده ها از این کلاس را ملزم میکند که برای دسترسی به متد هایی که درون اینترفیس ما هست و توسط کلاس پیاده شده است ، آبجکت مورد نظر باید درون آبجکتی از اینترفیس قرار گیرد یا اینکه از Casting استفاده کنید.


interface IDimensions
{
float Length();
float Width();
}

class Box : IDimensions
{
float lengthInches;
float widthInches;

public Box(float length, float width)
{
lengthInches = length;
widthInches = width;
}
// Explicit interface member implementation:
float IDimensions.Length()
{
return lengthInches;
}
// Explicit interface member implementation:
float IDimensions.Width()
{
return widthInches;
}

public static void Main()
{
// Declare a class instance "myBox":
Box myBox = new Box(30.0f, 20.0f);
// Declare an interface instance "myDimensions":
IDimensions myDimensions = (IDimensions) myBox;
// Print out the dimensions of the box:
/* The following commented lines would produce compilation
errors because they try to access an explicitly implemented
interface member from a class instance: */
//System.Console.WriteLine("Length: {0}", myBox.Length());
//System.Console.WriteLine("Width: {0}", myBox.Width());
/* Print out the dimensions of the box by calling the methods
from an instance of the interface: */
System.Console.WriteLine("Length: {0}", myDimensions.Length());
System.Console.WriteLine("Width: {0}", myDimensions.Width());
}
}





این هم مثال