简体   繁体   English

为什么 C# 允许在接口内部定义非抽象、非静态方法?

[英]Why does C# allows definitions of non-abstract, non-static methods inside of an interface?

I was experimenting on this thing and I found that C# actually allows you to have non-static, non-abstract method definitions inside an interface, which made totally no sense to me as it is not according to the rules of Object Oriented paradigm.我正在试验这个东西,我发现 C# 实际上允许你在接口内有非静态、非抽象的方法定义,这对我来说完全没有意义,因为它不符合 Object 面向范式的规则。 Here's the code you may test out in your systems which actually compiles and runs successfully the way you would expect it to.这是您可以在您的系统中测试的代码,它们实际上可以按照您期望的方式成功编译和运行。

Interface Test: -接口测试:-

public interface Test
{
    public void Display() {
        Console.WriteLine("Display called from interface");
    }
}

Class Derived that implements test: - Class 派生执行测试:-

public class Derived : Test
{
    public void SomeTestMethod()
    {
        Console.WriteLine("Just a method");
    }
}

Program.cs: -程序.cs: -

class Program
{
    static void Main(string[] args)
    {
        Test instance = new Derived();
        instance.Display();
    }
}

This code would run perfectly without any issues producing the output "Display called from interface" in the console.此代码将完美运行,不会在控制台中产生 output“从界面调用显示”的任何问题。 This makes absolutely no sense to me as to why is this even allowed.对于为什么允许这样做,这对我来说完全没有意义。 Won't this just re-introduce the diamond problem or the issue of having ambiguity in methods defined in two interfaces when both of them are implemented by a single class. I am not sure if this is something that has been introduced in newer versions of C# or is it a.Net Core thing.当两个接口都由单个 class 实现时,这是否会重新引入菱形问题或在两个接口中定义的方法中存在歧义的问题。我不确定这是否已在较新版本的C# 还是 .Net Core 的东西。 If anyone knows the reason please let me know.如果有人知道原因请告诉我。 Thanks in advance.提前致谢。

Answer to this question would be depends.这个问题的答案取决于。

  1. This feature was not there and it was introduce in C# 8.0.此功能不存在,它是在 C# 8.0 中引入的。
  2. It will be great help when you have to change interface in existing application or package that is consumed by many other third party.当您必须更改现有应用程序或许多其他第三方使用的 package 中的界面时,这将有很大帮助。 It provide back-word compatibility and also some how justify Interface Segregation of SOLID as it will not force to implement those method if it is not required.它提供后向兼容性以及一些如何证明 SOLID 接口隔离的合理性,因为如果不需要,它不会强制实施这些方法。

Example.例子。

  1. Consider Scenario.考虑情景。 This is version one of Interface IService.这是 Interface IService 的第一个版本。
public interface IService
{
    string GetMessage(); 
}
  1. Publish the package or provide to other application.发布 package 或提供给其他应用程序。
  2. That application implement interface.该应用程序实现接口。
public class ServiceA : IService
{
    public string GetMessage()
    {
        return "From ServiceA";
    }
}

public class ServiceB : IService
{
    public string GetMessage()
    {
        return "From ServiceB";
    }
}

Now there is requirement to change in interface ( best case avoid this) but if you have to do it.现在需要更改界面(最好避免这种情况),但如果您必须这样做。

Before C# 8.0 C# 8.0 之前

public interface IService
{
    string GetMessage();
    string NewMessage(); 
}

Now as soon this interface publish and consumed by other already implemented that interface.现在,一旦该接口被其他已经实现该接口的人发布和使用。 It break.它坏了。 ( This is breaking change.) (这是重大变化。)

from C# 8.0 you can provide default implementation.从 C# 8.0 开始,您可以提供默认实现。

public interface IService
{
    string GetMessage();
    string NewMessage()
    {
        return "From Interface";
    }
}

ServiceA decided to implement but ServiceB does not but it will not break previously running application. ServiceA 决定执行,但 ServiceB 不执行,但它不会中断先前运行的应用程序。

public class ServiceA : IService
{
    public string GetMessage()
    {
        return "From ServiceA";
    }

    public string NewMessage()
    {
        return "From ServiceA NewMessage";
    }
}

public class ServiceB : IService
{
    public string GetMessage()
    {
        return "From ServiceB";
    }
}

Since version 8.0, C# has support for default interface methods, which have some properties of traits, also it is similar to Java's "Default Methods"从8.0版本开始,C#支持默认接口方法,具有traits的一些属性,也类似于Java的“默认方法”

this feature enables C# to interoperate with APIs targeting Android (Java) and iOS (Swift), which support similar features.此功能使 C# 能够与针对 Android (Java) 和 iOS (Swift) 的 API 进行互操作,后者支持类似的功能。

The Question now is what is the difference between Abstract class and interface if we can write implementation in the interface.现在的问题是如果我们可以在接口中编写实现,那么抽象 class 和接口之间有什么区别。

This new feature in C# 8 is used when you have already written code that use interface and there are multiple classes that inherit from this interface and you want to add additional functionality to the interface without breaking the existing classes that are using my interface, what you can do is use default interface methods and add that functionality, this allow you to update your code without interrupting people who use your interface C# 8 中的这个新特性在你已经编写了使用接口的代码并且有多个类继承自这个接口并且你想向接口添加额外的功能而不破坏正在使用我的接口的现有类时使用,你可以做的是使用默认接口方法并添加该功能,这允许您更新代码而不会打扰使用您接口的人

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

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