次のコード例を検討してください。
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 からソース コードをダウンロードできます 。