C# 8 中的默认接口方法
Default Interface Methods in C# 8
考虑以下代码示例:
public interface IPlayer
{
int Attack(int amount);
}
public interface IPowerPlayer: IPlayer
{
int IPlayer.Attack(int amount)
{
return amount + 50;
}
}
public interface ILimitedPlayer: IPlayer
{
new int Attack(int amount)
{
return amount + 10;
}
}
public class Player : IPowerPlayer, ILimitedPlayer
{
}
使用代码:
IPlayer player = new Player();
Console.WriteLine(player.Attack(5)); // Output 55, --> im not sure from this output. I can compile the code but not execute it!
IPowerPlayer powerPlayer = new Player();
Console.WriteLine(powerPlayer.Attack(5)); // Output 55
ILimitedPlayer limitedPlayer = new Player();
Console.WriteLine(limitedPlayer.Attack(5)); // Output 15
我的问题出在代码上:
Console.WriteLine(player.Attack(5)); // Output 55
问题是:输出应该是15或者55?!
根据 .NET 团队的说法:
Decision: Made 2017-04-11: Runs I2.M, which is the unambiguously most specific override at runtime.
由于覆盖界面上的关键字 'new',我在这里不确定?正确的行为应该是什么?
如需从源码编译,可从以下网址下载源码:
https://github.com/alugili/Default-Interface-Methods-CSharp-8
是的,这是因为 new
关键字实际上隐藏了父类型的派生类型实现,因为它与 类 之前的行为完全相同,我们称之为 Shadowing concept.
所以输出将是 55 因为你有 IPlayer
类型的引用 Player
对象和 ILimitedPlayer
的 Attack
方法对 IPlayer
隐藏,因为它的签名中有 new
关键字
我想说你可以得到一个 "good guess" 来了解它在没有 C#8 编译器的情况下应该如何工作。我们这里基本上是:
public interface IPlayer {
// method 1
int Attack(int amount);
}
public interface IPowerPlayer : IPlayer {
// no methods, only provides implementation
}
public interface ILimitedPlayer : IPlayer {
// method 2, in question also provides implementation
new int Attack(int amount);
}
所以我们有 2 个接口方法(具有相同的签名),一些接口(IPowerPlayer
和 ILimitedPlayer
)提供了这些方法的实现。我们可以只在 Player
class 本身提供实现来实现类似的功能:
public class Player : IPowerPlayer, ILimitedPlayer {
int IPlayer.Attack(int amount) {
return amount + 50;
}
int ILimitedPlayer.Attack(int amount) {
return amount + 10;
}
}
然后 运行 来自问题输出的代码:
55
55
15
而且我认为原因比较清楚。
考虑以下代码示例:
public interface IPlayer
{
int Attack(int amount);
}
public interface IPowerPlayer: IPlayer
{
int IPlayer.Attack(int amount)
{
return amount + 50;
}
}
public interface ILimitedPlayer: IPlayer
{
new int Attack(int amount)
{
return amount + 10;
}
}
public class Player : IPowerPlayer, ILimitedPlayer
{
}
使用代码:
IPlayer player = new Player();
Console.WriteLine(player.Attack(5)); // Output 55, --> im not sure from this output. I can compile the code but not execute it!
IPowerPlayer powerPlayer = new Player();
Console.WriteLine(powerPlayer.Attack(5)); // Output 55
ILimitedPlayer limitedPlayer = new Player();
Console.WriteLine(limitedPlayer.Attack(5)); // Output 15
我的问题出在代码上:
Console.WriteLine(player.Attack(5)); // Output 55
问题是:输出应该是15或者55?!
根据 .NET 团队的说法:
Decision: Made 2017-04-11: Runs I2.M, which is the unambiguously most specific override at runtime.
由于覆盖界面上的关键字 'new',我在这里不确定?正确的行为应该是什么?
如需从源码编译,可从以下网址下载源码: https://github.com/alugili/Default-Interface-Methods-CSharp-8
是的,这是因为 new
关键字实际上隐藏了父类型的派生类型实现,因为它与 类 之前的行为完全相同,我们称之为 Shadowing concept.
所以输出将是 55 因为你有 IPlayer
类型的引用 Player
对象和 ILimitedPlayer
的 Attack
方法对 IPlayer
隐藏,因为它的签名中有 new
关键字
我想说你可以得到一个 "good guess" 来了解它在没有 C#8 编译器的情况下应该如何工作。我们这里基本上是:
public interface IPlayer {
// method 1
int Attack(int amount);
}
public interface IPowerPlayer : IPlayer {
// no methods, only provides implementation
}
public interface ILimitedPlayer : IPlayer {
// method 2, in question also provides implementation
new int Attack(int amount);
}
所以我们有 2 个接口方法(具有相同的签名),一些接口(IPowerPlayer
和 ILimitedPlayer
)提供了这些方法的实现。我们可以只在 Player
class 本身提供实现来实现类似的功能:
public class Player : IPowerPlayer, ILimitedPlayer {
int IPlayer.Attack(int amount) {
return amount + 50;
}
int ILimitedPlayer.Attack(int amount) {
return amount + 10;
}
}
然后 运行 来自问题输出的代码:
55
55
15
而且我认为原因比较清楚。