When you implement two interfaces with the same method, how do you know which one is called?
If you have TheMethod()
in interfaces I1 and I2, and the following class
class TheCla开发者_JS百科ss : I1, I2
{
void TheMethod()
}
If something instantiates TheClass
, how does it know which interface it is using?
If that's how the client code is using the class, it doesn't really matter. If it needs to do something interface specific, it should declare the interface it needs, and assign the class to that e.g.
I1 i = new TheClass()
i.TheMethod();
Of course, using your current implementation TheClass
, it doesn't matter if declared i
as I1
, or I2
, as you only have a single implementation.
If you want a separate implementation per interface, then you need to create explicit implementations ...
void I1.TheMethod()
{
Console.WriteLine("I1");
}
void I2.TheMethod()
{
Console.WriteLine("I2");
}
But keep in mind, explicit implementations cannot be public. You can implement just one explicitly, and leave the other as the default which can be public.
void I1.TheMethod()
{
Console.WriteLine("I1");
}
public void TheMethod()
{
Console.WriteLine("Default");
}
Have a look the msdn article for more details.
If both methods are public then the same method will be called regardless of which interface it was called upon. If you need more granularity, you need to use explicit interface implementations:
class TheClass : I1, I2
{
void I1.TheMethod() {}
void I2.TheMethod() {}
}
Usage might look like:
TheClass theClass = new TheClass();
I1 i1 = theClass;
I2 i2 = theClass;
i1.TheMethod(); // (calls the first one)
i2.TheMethod(); // (calls the other one)
One thing to keep in mind is that if you make both implementations explicit, you will no longer be able to call TheMethod
on variables declared as TheClass
:
theClass.TheMethod(); // This would fail since the method can only be called on the interface
Of course, if you like, you can make only one of the implementations explicit, and keep the other one public in which case calls against TheClass
would call the public version.
You don't use an interface in the sense of calling it. An interface is just a contract defining which methods you can call essentially. You always call the implementation.
If you instantiate the class, then you are not using any of the interfaces. If you cast the reference to either interface, then you are using that interface. Example:
TheClass c = new TheClass();
c.TheMethod(); // using the class
I1 i = new TheClass();
i.TheMethod(); // using the I1 interface
As your class is declared, both interfaces will use the same method. You can also specify methods for the class and the separate interfaces:
class TheClass : I1, I2
{
void TheMethod() {} // used by the class
void I1.TheMethod() {} // used by the I1 interface
void I2.TheMethod() {} // used by the I2 interface
}
If you specify only methods for the interfaces, you can't reach the method unless you cast the reference to an interface first:
class TheClass : I1, I2
{
void I1.TheMethod() {} // used by the I1 interface
void I2.TheMethod() {} // used by the I2 interface
}
If you specify separate methods for only some interfaces, the other interfaces will use the same implementation as the class:
class TheClass : I1, I2
{
void TheMethod() {} // used by the class and the I1 interface
void I2.TheMethod() {} // used by the I2 interface
}
As long as the method signatures are the same, it is perfectly legal for a method to implement the methods of two or more interfaces.
There is no way to know "through which interface" a method is called -- there is no such concept (and it doesn't matter).
The real question is, "Who cares which interface it is using?" Really, it's not "using" the interface at all. It's using the implementation to fulfil the interface.
That is irrelevant. Casting to any of the interfaces will result in the method being called, but since an interface cannot contain code you cannot inherit behavior from it.
public interface IA
{
void Sum();
}
public interface IB
{
void Sum();
}
public class SumC : IA, IB
{
void IA.Sum()
{
Console.WriteLine("IA");
}
void IB.Sum()
{
Console.WriteLine("IB");
}
public void Sum()
{
Console.WriteLine("Default");
}
}
public class MainClass
{
static void Main()
{
IA objIA = new SumC();
IB objIB = new SumC();
SumC objC = new SumC();
objIA.Sum();
objIB.Sum();
objC.Sum();
Console.ReadLine();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern
{
interface IEmployee
{
void show();
}
interface IContarctEmployee
{
void show();
}
class Employee: IEmployee, IContarctEmployee
{
void IEmployee.show()
{
Console.WriteLine("Show Method is calling for Employee");
}
void IContarctEmployee.show()
{
Console.WriteLine("Show Method is calling for contract Employee");
}
}
class Program
{
public static void Main(string[] args)
{
IEmployee cmp = new Employee();
IContarctEmployee ice = new Employee();
cmp.show();
ice.show();
}
}
}
精彩评论