问题
I recently ran into a list of features that are being considered for addition in the next version of C#. One of them is called "default interface methods":
https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md
In short, it will allow you to define actual method implementations on the interfaces themselves meaning interfaces can now have implementations. Since this is the case, and C# classes can implement/inherit from multiple interfaces then why in the world would I ever use abstract classes?
The only thing that comes to my mind is that interfaces cannot have a constructor so maybe there is a need to run some logic in the abstract class constructor and that would justify defining an abstract class.
Is there any other scenario that anyone can think of?
回答1:
Apart from state as mentioned in comments,
Base Class
You can't inherit an interface from a Base class. Interface can only inherit an interface. You would need abstract class to derive from some other class. And since you can't inherit from class, you can't override class methods. Which you can override in abstract class.
回答2:
Usually class inheritance feels like a taxonomy (x 'is a' y), while interfaces are behaviours (x 'can do' y), so there is a subtle difference in implied meaning. Although you're right that the technical distinction is less clear cut.
来源:https://stackoverflow.com/questions/45641921/if-default-interface-methods-are-implemented-in-c-sharp-8-0-why-would-i-ever-nee