问题描述
我在具有C#8的.NET Core 3.1中拥有此简单控制台程序:
using System;
namespace ConsoleApp34
{
public interface ITest
{
public void test()
{
Console.WriteLine("Bye World!");
}
}
public class Test : ITest
{
public void CallDefault()
{
((ITest)(this)).test();
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var t = new test();
t.CallDefault();
}
}
}
我不明白为什么在((ITest)(this)).test();
行中必须进行强制转换
Test是直接从ITest派生的,因此,根据定义,“ this”是ITest
谢谢。
解决方法
默认接口实现与显式实现类似:只能通过接口类型调用,而不能通过实现类型调用。
要了解为什么会这样,请想象Test
实现了两个具有相同方法签名的接口;如果没有演员,将使用哪一个?
public interface ITest2
{
public void test()
{
Console.WriteLine("Hello World!");
}
}
public class Test : ITest,ITest2
{
public void CallDefault()
{
test(); // Do we use ITest.test() or ITest2.test()?
}
}
,
此行为是documented here。
从C#8.0开始,您可以为接口中声明的成员定义实现。如果类从接口继承了方法实现,则只能通过接口类型的引用来访问该方法。继承的成员不会出现在公共接口中。以下示例定义了接口方法的默认实现:
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方法,既可以作为公共方法,也可以作为显式接口实现。