继承自具有相同方法签名的多个接口的类

编程入门 行业动态 更新时间:2024-10-24 22:20:34
本文介绍了继承自具有相同方法签名的多个接口的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

说,我有三个接口:

public interface I1 { void XYZ(); } public interface I2 { void XYZ(); } public interface I3 { void XYZ(); }

从这三个接口继承的类:

A class inheriting from these three interfaces:

class ABC: I1,I2, I3 { // method definitions }

问题:

  • 如果我实现如下:

  • If I implement like this:

类ABC:I1,I2,I3 {

class ABC: I1,I2, I3 {

public void XYZ() { MessageBox.Show("WOW"); }

}

它编译得很好,也运行得很好! 这是否意味着这个单一方法实现足以继承所有三个接口?

It compiles well and runs well too! Does it mean this single method implementation is sufficient for inheriting all the three Interfaces?

  • 我如何实现所有三个接口的方法和 CALL THEM ? 像这样:

  • How can I implement the method of all the three interfaces and CALL THEM? Something Like this:

ABC abc = new ABC(); abc.XYZ(); // for I1 ? abc.XYZ(); // for I2 ? abc.XYZ(); // for I3 ?

我知道它可以使用显式实现完成但是我无法打电话给他们。 :(

I know it can done using explicit implementation but I'm not able to call them. :(

推荐答案

如果使用显式实现,则必须将对象强制转换为要调用其方法的接口:

If you use explicit implementation, then you have to cast the object to the interface whose method you want to call:

class ABC: I1,I2, I3 { void I1.XYZ() { /* .... */ } void I2.XYZ() { /* .... */ } void I3.XYZ() { /* .... */ } } ABC abc = new ABC(); ((I1) abc).XYZ(); // calls the I1 version ((I2) abc).XYZ(); // calls the I2 version

更多推荐

继承自具有相同方法签名的多个接口的类

本文发布于:2023-11-05 19:10:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   接口   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!