实现两个定义相同方法的接口的类
//inteface i11
public interface i11
{
void m11();
}
//interface i22
public interface i22
{
void m11();
}
//class ab implements interfaces i11 and i22
public class ab : i11, i22
{
public void m11()
{
Console.WriteLine("class");
}
}
现在,当我们在 Main() 方法中创建一个 ab 类的对象时 将调用哪个接口方法,请全部解释。
//inteface i11
public interface i11
{
void m11();
}
//interface i22
public interface i22
{
void m11();
}
//class ab implements interfaces i11 and i22
public class ab : i11, i22
{
public void m11()
{
Console.WriteLine("class");
}
}
now when in Main() method we create an object of class ab
which interface method will get called please explain all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只有一种方法实现,因此显然无论引用的类型如何,都会调用该方法。 IE:
和
实际上是相同的。
ab.m11
满足两个接口中方法的签名。另外,您没有使用显式接口实现< /a>,所以这不是一个因素。There's only one method implementation, so obviously that gets called, regardless of the type of the reference. I.E.:
and
are effectively the same.
ab.m11
satisfies the signature of the method in both interfaces. Also, you're not using explicit interface implementation, so that isn't a factor.接口只是说“任何实现此接口的东西都可以确保具有这些方法”。
如果您在这种情况下考虑它,您会发现它不会调用接口的方法;而是调用接口的方法。相反,它只是实现所需的方法调用。
Interfaces merely say "Anything that implements this interface can be sure to have these methods".
If you think about it in that context, you'll see that it doesn't call the interface's method; rather, it merely implements the method calls required.
您将接口的实现与具体类的扩展混淆了。我相信这会让你对这段关系产生倒退的想法。根据定义,接口没有具体的实现。它仅定义实现类必须使用的契约。也许一个例子可以帮助澄清。
假设我们有一个名为
IBank
的接口。该接口具有以下定义:然后我们对该接口的具体实现如下:
我无法创建
IBank
的新实例。以下代码将无法编译。相反,我必须创建一个新的具体银行实例,我可以将其视为
IBank
。然后我可以调用这个类的方法。因此,当我调用
AddMoney
时,我使用了接口定义的方法,但实际上是在EuroBank
类上调用AddMoney
的具体实现。因此,在您的示例中,您正在调用
ab
类上的m11
方法。但是,ab
同时实现了i11
和i22
。因此,它可以被视为i11
和i22
对象。以下两行代码都将起作用。我可以在任一对象上调用
m11
方法,但它将始终使用在对象ab
上找到的方法的具体实现。You are confusing the implementation of an interface with the extension of concrete class. I believe this is causing you to think of the relationship backwards. An interface, by definition, has no concrete implementation. It only defines a contract that implementing classes must use. Perhaps an example can help clarify.
Suppose we have an interface called
IBank
. This interface has the following definition:Then we have a concrete implementation of this interface as follows:
I cannot create a new instance of
IBank
. The following code will not compile.Instead, I must create a new concrete instance of a bank that I can treat as an
IBank
. I can then call methods on this class.So when I call
AddMoney
, I using the method defined by the interface, but I am actually calling the concrete implementation ofAddMoney
on theEuroBank
class.So in your example, you are calling the
m11
method on theab
class. However,ab
implements bothi11
andi22
. Therefore, it can be treated as both ani11
andi22
object. Both of the following lines of code will work.I can call the
m11
method on either object, but it will always use the concrete implementation of the method found on the objectab
.该方法不是接口方法。
将调用的方法是类
ab
的m11
方法。The method is not an interface method.
The method that will be called is the
m11
method of classab
.