第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C#8中的默認(rèn)接口方法

C#8中的默認(rèn)接口方法

C#
動漫人物 2021-03-30 13:14:32
考慮下面的代碼示例: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 55ILimitedPlayer limitedPlayer = new Player();Console.WriteLine(limitedPlayer.Attack(5)); // Output 15我的問題是在代碼上:Console.WriteLine(player.Attack(5)); // Output 55問題是:輸出應(yīng)該是15還是55?!
查看完整描述

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)為原因很清楚。


查看完整回答
反對 回復(fù) 2021-04-24
  • 2 回答
  • 0 關(guān)注
  • 145 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號