एक कक्षा जो इंटरफ़ेस लागू करती है वह स्पष्ट रूप से उस इंटरफ़ेस के सदस्य को कार्यान्वित कर सकती है। जब कोई सदस्य स्पष्ट रूप से कार्यान्वित किया जाता है, तो उसे कक्षा के उदाहरण के माध्यम से एक्सेस नहीं किया जा सकता है, लेकिन केवल इंटरफ़ेस के उदाहरण के माध्यम से।
// explicit1.cs
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());
}
}
स्पष्ट इंटरफेस कार्यान्वयन भी प्रोग्रामर दो इंटरफेस का हिस्सा है कि एक ही सदस्य के नाम के वारिस और प्रत्येक इंटरफ़ेस सदस्य एक अलग कार्यान्वयन देने के लिए अनुमति देता है। यह उदाहरण दोनों मीट्रिक और अंग्रेजी इकाइयों में एक बॉक्स के आयाम प्रदर्शित करता है। बॉक्स क्लास में दो इंटरफेस आईनग्रेजीडिमेंशन और इमेट्रिक डिमेंशन प्राप्त होते हैं, जो विभिन्न माप प्रणालियों का प्रतिनिधित्व करते हैं। दोनों इंटरफेस में समान सदस्य नाम, लंबाई और चौड़ाई होती है। उदाहरण
// explicit2.cs
// 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;
}
public static void Main()
{
// Declare a class instance "myBox":
Box myBox = new Box(30.0f, 20.0f);
// Declare an instance of the English units interface:
IEnglishDimensions eDimensions = (IEnglishDimensions) myBox;
// Declare an instance of the metric units interface:
IMetricDimensions mDimensions = (IMetricDimensions) myBox;
// 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());
}
}
अधिक जानकारी के लिए पर
नज़र इस article
क्या "सार्वजनिक इंटरफेस लागू करना" और "निजी इंटरफेस लागू करना" बहुत कठिन रहा है? – PRMan