简体   繁体   English

C#8中的默认接口方法

[英]Default Interface Methods in C# 8

Consider the following code example: 请考虑以下代码示例:

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
{
}

Using the code: 使用代码:

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

My problem is on the code: 我的问题在于代码:

Console.WriteLine(player.Attack(5)); // Output 55

The question is: The output should be 15 or 55 ?! 问题是:输出应该是15还是55

According to the .NET Team: 根据.NET团队的说法:

Decision: Made 2017-04-11: Runs I2.M, which is the unambiguously most specific override at runtime. 决策:Made 2017-04-11:运行I2.M,这是运行时明确最具体的覆盖。

I'm not sure here beacuse of the keyword 'new' on the overridden interface?what should be the correct behaviour? 我不确定在重写的界面上是否存在关键字“new”?应该是正确的行为?

In case you need to compile it from source, you can download the source code from: https://github.com/alugili/Default-Interface-Methods-CSharp-8 如果您需要从源代码编译它,可以从以下网址下载源代码: https//github.com/alugili/Default-Interface-Methods-CSharp-8

Yes, it is bacause of the new keyword which actually hides the derived type implementation from parent type as it was exactly the same behavior before too for classes as well which we call Shadowing concept . 是的,这是因为new关键字实际上隐藏了父类型的派生类型实现,因为它之前的类也是完全相同的行为,我们称之为Shadowing概念

So the output would be 55 as you have reference of type IPlayer for Player object and ILimitedPlayer 's Attack method is hidden from IPlayer because of the new keyword in it's signatures 所以输出会是55,你有类型的引用IPlayerPlayer对象和ILimitedPlayerAttack方法是从隐藏IPlayer ,因为的new关键词在它的签名

I'd say you can get a "good guess" for how this should work without C#8 compiler. 我想说如果没有C#8编译器,你可以得到一个“很好的猜测”。 What we have here is basically: 我们在这里基本上是:

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);
}

So we have 2 interface methods (with same signature), and some interfaces ( IPowerPlayer and ILimitedPlayer ) provide implementations of those methods. 所以我们有2个接口方法(具有相同的签名),并且一些接口( IPowerPlayerILimitedPlayer )提供这些方法的实现。 We can just provide implementaitions in Player class itself to achieve similar functionality: 我们可以在Player类本身中提供实现来实现类似的功能:

public class Player : IPowerPlayer, ILimitedPlayer {
    int IPlayer.Attack(int amount) {
        return amount + 50;
    }

    int ILimitedPlayer.Attack(int amount) {
        return amount + 10;
    }
}

Then running code from question outputs: 然后从问题输出中运行代码:

55 55

55 55

15 15

And I think it's relatively clear why. 而且我认为为什么会相对清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM