考虑以下代码示例:
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 团队的说法:
决定:于 2017 年 4 月 11 日做出:运行 I2.M,这是运行时最明确的最具体的覆盖。
我不确定这里是因为重写界面上的关键字“new”吗?正确的行为应该是什么?
如果您需要从源代码编译它,您可以从以下网址下载源代码: https ://github.com/alugili/Default-Interface-Methods-CSharp-8