understanding of the interface

For those who are new to object-oriented, there may be a question - what is the meaning of the interface?

 

If you inherit an interface, you need to implement its methods . I might as well define these methods in the current class and implement them. Why do I need to implement them by inheriting the interface? Isn’t this a few hundred more miles?

 

If you think that you can define these methods directly in the current class and implement them , then you will also have doubts about a concept - the doubts of inheritance .

 

You may say that inheritance is not very simple, I inherit a class, and the inherited class has the members of the parent class. If you think so, then you also have doubts about a concept - the doubts of polymorphism .

 

First of all, what are the benefits of class inheritance? Nothing more than two points: extended members, polymorphism can be achieved.

 

In C#, multiple inheritance of classes is not supported. Why not support it? Is Microsoft's technology not enough? No, just because they feel single inheritance of classes is more appropriate. Therefore, the polymorphism of an object has certain limitations, so a thing called an interface was invented .

 

This way, if necessary, my class can inherit many interfaces, making it more polymorphic.

 

The main purpose of object-oriented design is to simulate a certain thought in life, so that humans are more comfortable to think. When exercising your object-oriented design skills, you should deliberately use this kind of thinking. So, if you understand object orientation, you also understand interfaces.

 

If you always do not understand the interface, vague, then you are always troubled by a problem - polymorphism.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324853719&siteId=291194637