2 回答

TA貢獻(xiàn)1752條經(jīng)驗 獲得超4個贊
我說您可以對“沒有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)提供了這些方法的實現(xiàn)。我們可以只在Player類本身中提供實現(xiàn)以實現(xiàn)類似的功能:
public class Player : IPowerPlayer, ILimitedPlayer {
int IPlayer.Attack(int amount) {
return amount + 50;
}
int ILimitedPlayer.Attack(int amount) {
return amount + 10;
}
}
然后從問題輸出運行代碼:
55
55
15
而且我認(rèn)為原因很清楚。
- 2 回答
- 0 關(guān)注
- 145 瀏覽
添加回答
舉報