Csharp/CSharp Tutorial/Class/Explicit Interface Implementation

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Explicit Interface Implementation

using System;
interface InterfaceOne
{
    void Execute();
}
interface InterfaceTwo
{
    void Execute();
}
class MyImplementation: InterfaceOne, InterfaceTwo
{
    void InterfaceOne.Execute() 
    {
        Console.WriteLine("InterfaceOne.Execute implementation");
    }
    void InterfaceTwo.Execute()
    {
        Console.WriteLine("InterfaceTwo.Execute implementation");
    }
}
class MainClass
{
    public static void Main()
    {
        MyImplementation MyImplementation = new MyImplementation();
        
        InterfaceOne InterfaceOne = (InterfaceOne) MyImplementation;
        InterfaceOne.Execute();
        
        InterfaceTwo InterfaceTwo = (InterfaceTwo) MyImplementation;
        InterfaceTwo.Execute();
    }
}
InterfaceOne.Execute implementation
InterfaceTwo.Execute implementation

Explicit interface implementation and its own implementation

using System;
interface InterfaceOne
{
    void Execute();
}
interface InterfaceTwo
{
    void Execute();
}
class MyImplementation: InterfaceOne, InterfaceTwo
{
    void InterfaceOne.Execute() 
    {
        Console.WriteLine("InterfaceOne.Execute implementation");
    }
    void InterfaceTwo.Execute()
    {
        Console.WriteLine("InterfaceTwo.Execute implementation");
    }
    
    public void Execute()
    {
        ((InterfaceOne) this).Execute();
    }
}
class MainClass
{
    public static void Main()
    {
        MyImplementation MyImplementation = new MyImplementation();
        
        MyImplementation.Execute();
    }
}
InterfaceOne.Execute implementation

Explicitly implement an interface member

using System; 
 
interface MyInterface { 
  bool MyMethodA(int x); 
  bool MyMethodB(int x); 
} 
 
class MyClass : MyInterface { 
 
  // Explicit implementation. 
  bool MyInterface.MyMethodA(int x) { 
    if((x%2) != 0) 
       return true; 
    else 
       return false; 
  } 
 
  // Normal implementation. 
  public bool MyMethodB(int x) { 
    MyInterface o = this; // reference to invoking object 
 
    return !o.MyMethodA(x); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    MyClass ob = new MyClass(); 
    bool result; 
 
    result = ob.MyMethodB(4); 
    if(result) Console.WriteLine("4 is even."); 
 
    // result = ob.MyMethodA(4); // Error, MyMethodA not directly accessible 
 
    MyInterface iRef = (MyInterface) ob; 
    result = iRef.MyMethodA(3); 
    if(result) Console.WriteLine("3 is odd."); 
 
  } 
}
4 is even.
3 is odd.

Interface member hiding

using System;

public interface InterfaceA
{
  void MethodA();
}

public interface InterfaceB : InterfaceA
{
  new void MethodA();  // hides MethodA() in InterfaceA
}
public class MyClass : InterfaceB
{
  void InterfaceB.MethodA()
  {
    Console.WriteLine("InterfaceB implementation of MethodA()");
  }
  public void MethodA()
  {
    Console.WriteLine("InterfaceA implementation of MethodA()");
  }
}

class MainClass
{
  public static void Main()
  {
    MyClass myClass = new MyClass();
    Console.WriteLine("Calling myClass.MethodA()");
    myClass.MethodA();
    InterfaceB mySteerable = myClass as InterfaceB;
    Console.WriteLine("Calling mySteerable.MethodA()");
    mySteerable.MethodA();
    InterfaceA myDrivable = myClass as InterfaceA;
    Console.WriteLine("Calling myDrivable.MethodA()");
    myDrivable.MethodA();
  }
}
Calling myClass.MethodA()
InterfaceA implementation of MethodA()
Calling mySteerable.MethodA()
InterfaceB implementation of MethodA()
Calling myDrivable.MethodA()
InterfaceA implementation of MethodA()

Use explicit implementation to remove ambiguity

using System; 
 
interface MyInterfaceA { 
  int Method(int x); 
} 
 
interface MyInterfaceB { 
  int Method(int x); 
} 
 
// MyClass implements both interfaces. 
class MyClass : MyInterfaceA, MyInterfaceB { 
 
  // explicitly implement the two Method()s 
  int MyInterfaceA.Method(int x) { 
    return x + x; 
  } 
  int MyInterfaceB.Method(int x) { 
    return x * x; 
  } 
 
  // call Method() through an interface reference. 
  public int MethodA(int x){ 
    MyInterfaceA a_ob; 
    a_ob = this; 
    return a_ob.Method(x); // calls MyInterfaceA 
  } 
 
  public int MethodB(int x){ 
    MyInterfaceB b_ob; 
    b_ob = this; 
    return b_ob.Method(x); // calls MyInterfaceB 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    MyClass ob = new MyClass(); 
 
    Console.Write("Calling MyInterfaceA.Method(): "); 
    Console.WriteLine(ob.MethodA(3)); 
 
    Console.Write("Calling MyInterfaceB.Method(): "); 
    Console.WriteLine(ob.MethodB(3)); 
  } 
}
Calling MyInterfaceA.Method(): 6
Calling MyInterfaceB.Method(): 9