一,特性
/**
接口测试
*/
using System;
namespace InterfaceTest{
public interface IControl
{
void Paint();
}
public interface ISurface
{
void Paint();
}
public class SampleClass : IControl, ISurface
{
// 显示调用 通过接口名访问
void IControl.Paint()
{
System.Console.WriteLine("IControl.Paint");
}
void ISurface.Paint()
{
System.Console.WriteLine("ISurface.Paint");
}
}
class Program
{
static void Main()
{
SampleClass sample = new SampleClass();
IControl control = sample;
ISurface surface = sample;
// 访问同一个方法
//sample.Paint(); // 编译报错.
control.Paint(); // Calls IControl.Paint on SampleClass.
surface.Paint(); // Calls ISurface.Paint on SampleClass.
var sampleAuto = sample as IControl;
sampleAuto.Paint();
// Output:
// IControl.Paint
// ISurface.Paint
// IControl.Paint
}
}
}