Current location - Quotes Website - Personality signature - Interface issues in C#
Interface issues in C#

This is a matter of norms and thinking habits. An interface is a program's generalization and definition of classes with different functions but the same usage methods. If many of your classes inherit the same interface, but often do not need to implement all the methods or properties of this interface, that is your There is a problem with the interface design. For example:

For example, the interface "manual transmission car" requires the following methods to drive away: steering wheel, accelerator, brake, gearshift, and clutch. Ford Focus, Chery A3, Volkswagen Pineapple, Xiali N8. These specific models are equivalent to "categories". They all inherit "manual transmission sedan". I can drive a manual transmission sedan, so any of these implement the "manual transmission sedan" interface. Give me a car and I can drive it away. But you built a car with no brakes, no gears, and no clutch, and then told me it was a "manual transmission sedan" (inheriting the "manual transmission sedan" interface). I took it and drove it away. But once I want to park or change gears, I find that these devices (methods, attributes) are not available. What are the consequences? No one stipulates that the car you build cannot be called a "manual transmission sedan", but it is obvious that if others drive your car like a manual transmission sedan, there will be an accident in all likelihood. Therefore, the compiler stipulates that if you inherit an interface, you must implement all its members, just like it stipulates that if you build a "manual transmission car", you must install the steering wheel, accelerator, brake, gear, and clutch.