具有相同方法签名的多个接口 [英] Multiple Interface with same method signature
本文介绍了具有相同方法签名的多个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们不在这里做作业!
我想知道,如果我的应用程序中有多个接口具有相同的方法签名,如下所示:
We do not do your homework here!
Hi,
I wanted to know, if I have more than 1 interface in a application having same method signature as below:
public interface ISum1
{
int Sum(int a, int b);
}
public interface ISum2
{
int Sum(int a, int b);
}
现在,我已经在类中实现了这些接口,如下所示:
Now I have implemented these interfaces in to class as below:
public class MainClass : ISum1, ISum2
{
public int Sum(int a, int b)
{
return a + b;
}
}
它与接口ISum1的实现配合良好,如果我想在类中的ISum2中编写方法的定义,现在允许我将相同的方法放入相同的签名中,我该如何进一步进行操作....请帮助.
VJ
It is working fine with implementation of Interface ISum1, if I wanted to write the definition for method in ISum2 in a class, it is now allowing me to put the same method with same signatures, how can I proceed further.... Please help.
VJ
推荐答案
您需要显式实现接口
看到这里..
http://msdn.microsoft.com/en-us/library/aa288461%28v = vs.71%29.aspx [ ^ ]
也请看一下这个博客,这正是您要尝试做的事情
http://sandblogaspnet.blogspot.co.uk/2008/05/Implementation-two-interface-having-same.html [ ^ ]
You need to implement the interfaces explicitly
See here..
http://msdn.microsoft.com/en-us/library/aa288461%28v=vs.71%29.aspx[^]
Have a look at this blog as well, which is exactly what you are trying to do
http://sandblogaspnet.blogspot.co.uk/2008/05/implementing-two-interface-having-same.html[^]
使用接口名称对方法进行显式限定会有所帮助
Explicity qualifying the methods with Interface names would help
public interface ISum1
{
int Sum(int a, int b);
}
public interface ISum2
{
int Sum(int a, int b);
}
public class MainClass : ISum1, ISum2
{
int ISum1.Sum(int a, int b)
{
return a + b;
}
int ISum2.Sum(int a, int b)
{
return a * b;
}
}
public class Test
{
public void TestMethod()
{
int result;
ISum1 is1 = new MainClass();
result = is1.Sum(2,3);
ISum2 is2 = new MainClass();
result = is2.Sum(2,3);
}
}
这篇关于具有相同方法签名的多个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文