Thinking about the interface

Source: Internet
Author: User

1, the meaning of the interface:

have been thinking about what is the meaning of the interface, we define a class, if inherit the interface, we need to implement the interface of all the methods and properties, fields, etc., then why inherit the interface, directly define the class we want to be good, and then with the increase in contact, plus some information on the Internet search, Gradually realized that the birth of the interface is a great invention:

1), interfaces can define specifications, that is, we define a method in the interface, the subsequent inherited class needs to implement the method, and the method must have the same name. This creates a naming convention that increases the readability of the code.

2), interface can perfect the definition of rules, is a standard, how to say, that is, when we do the project if a class needs to implement a lot of methods, it is possible to forget, and define the interface and inherit him can avoid the occurrence of this problem.

3), interface is a multi-state implementation, in object-oriented programming, the interface helps to reflect the characteristics of polymorphism, such as the use of Factory mode is the use of interfaces, but also in the dependency injection and control inversion is used in the interface to reduce the coupling.

4) 、。。。 The back adds, for the time being thought so much.

2, look at the definition of an interface, take the set as an example (because the bread here contains one of my thoughts):

(Pictures from on the road ...) 's Blog: http://www.cnblogs.com/yang_sy/p/3598970.html)

Let's take another picture (the list's definition inheritance graph):

We can find our list is very good to inherit all of its predecessors, but, in fact, as long as he inherited ilist<t> and IList can achieve the same function, why Microsoft in the definition of the time to the other predecessors have inherited it?

Later, I realized that this might be to improve the readability of the code.

The above piece of code is I knock, we can see that the class third has implemented the interface Ifirst method, but I write class third did not indicate that there is inheritance ifirst, this way if others want to use my class if you want to use polymorphism, It is necessary to look down at the first level to be able to know that Ithird can be used to receive it.

Thinking about the interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.