windows C#-显式接口实现

如果一个类实现的两个接口包含签名相同的成员,则在该类上实现此成员会导致这两个接口将此成员用作其实现。 如下示例中,所有对 Paint 的调用皆调用同一方法。 第一个示例定义类型:

复制代码
public interface IControl
{
    void Paint();
}
public interface ISurface
{
    void Paint();
}
public class SampleClass : IControl, ISurface
{
    // Both ISurface.Paint and IControl.Paint call this method.
    public void Paint()
    {
        Console.WriteLine("Paint method in SampleClass");
    }
}

下面的示例调用方法:

复制代码
SampleClass sample = new SampleClass();
IControl control = sample;
ISurface surface = sample;

// The following lines all call the same method.
sample.Paint();
control.Paint();
surface.Paint();

// Output:
// Paint method in SampleClass
// Paint method in SampleClass
// Paint method in SampleClass

但你可能不希望为这两个接口都调用相同的实现。 若要调用不同的实现,根据所使用的接口,可以显式实现接口成员。 显式接口实现是一个类成员,只通过指定接口进行调用。 通过在类成员前面加上接口名称和句点可命名该类成员。 例如:

复制代码
public class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}

类成员 IControl.Paint 仅通过 IControl 接口可用,ISurface.Paint 仅通过 ISurface 可用。 这两个方法实现相互独立,两者均不可直接在类上使用。 例如:

复制代码
SampleClass sample = new SampleClass();
IControl control = sample;
ISurface surface = sample;

// The following lines all call the same method.
//sample.Paint(); // Compiler error.
control.Paint();  // Calls IControl.Paint on SampleClass.
surface.Paint();  // Calls ISurface.Paint on SampleClass.

// Output:
// IControl.Paint
// ISurface.Paint

显式实现还用于处理两个接口分别声明名称相同的不同成员(例如属性和方法)的情况。 若要实现两个接口,类必须对属性 P 或方法 P 使用显式实现,或对二者同时使用,从而避免编译器错误。 例如:

复制代码
interface ILeft
{
    int P { get;}
}
interface IRight
{
    int P();
}

class Middle : ILeft, IRight
{
    public int P() { return 0; }
    int ILeft.P { get { return 0; } }
}

显式接口实现没有访问修饰符,因为它不能作为其定义类型的成员进行访问。 而只能在通过接口实例调用时访问。 如果为显式接口实现指定访问修饰符,将收到编译器错误 CS0106。

你可以为在接口中声明的成员定义一个实现。 如果类从接口继承方法实现,则只能通过接口类型的引用访问该方法。 继承的成员不会显示为公共接口的一部分。 下面的示例定义接口方法的默认实现:

复制代码
public interface IControl
{
    void Paint() => Console.WriteLine("Default Paint method");
}
public class SampleClass : IControl
{
    // Paint() is inherited from IControl.
}

下面的示例调用默认实现:

复制代码
var sample = new SampleClass();
//sample.Paint();// "Paint" isn't accessible.
var control = sample as IControl;
control.Paint();

任何实现 IControl 接口的类都可以重写默认的 Paint 方法,作为公共方法或作为显式接口实现。

相关推荐
1001101_QIA10 分钟前
.exe does not seem to be a Qt executable.
开发语言·qt
echome88834 分钟前
Python 异常处理:try、else、finally 的正确搭配
开发语言·前端·python·异常处理·编程技巧
云和数据.ChenGuang34 分钟前
Python 单继承与多继承深度剖析:从 MRO 到实战取舍
开发语言·人工智能·python·langchain
小咕聊编程1 小时前
基于Python的豆瓣电影数据可视化系统构建分析
开发语言·python·信息可视化
脏脏a3 小时前
延迟与画质:远程控制的生命线
开发语言·php
keren23 小时前
SceneDock:Windows 场景化工作台——一键启动工作链,还能被 AI Agent 调用(CLI / MCP)
windows·开发工具·效率工具·mcp
像风一样的男人@3 小时前
python --基于scp的文件传输助手
开发语言·python
一条破秋裤3 小时前
SDIO Wi‑Fi 设备的驱动向insmod + 反向rmmod
开发语言·php
m0_639906943 小时前
Java 重写 Override 与重载 Overload 区别、访问修饰符、static 关键字
java·开发语言
Evand J3 小时前
【MATLAB例程】平面下,5基站对待定位点的定位。基于距离辅助下的AOA。完整代码下载链接
开发语言·matlab·平面·定位·导航·测距·aoa