Preliminary understanding of Java--The tenth chapter-object-oriented-interface application

Source: Internet
Author: User

One.

Put the interface to this knowledge and give it a deeper look.

Re-development, the first design interface, and then develop the program, and finally consider the expansion function.

Before defining an application, define the rules first. In my application, this rule is used. The rule is the interface, then the additional device, is the implementation of the interface.

The two functions of the interface, define rules using rules, and the other is implementation rules.

Interface is like, how to understand it? Sub-class implementation of the interface, USB is an interface, USB mouse, USB hard disk, are like a USB, rather than a kind of USB. Inheritance is a, it is a kind of inside.

This part of the explanation, originally did not usemouse (Mouse m) This method, no mouse. Now pry open the computer, welding a mouse, in the bookpc appeared a Usemouse method. If you want to add more features, you will continue to write methods to the class, the computer will continue to pry open, welding peripherals. It is inappropriate to do so.

The above is using the USB rule. From the beginning to parse the program, the first computer this class is necessary to have, which encapsulates a lot of things. The most important part of the composition is the provision of the USB interface and the ability to use the USB interface. That is, there is a Useusb () method, the parameters are the parameters of the USB interface type, whenever the method into the USB interface type parameters, we can operate on it, the current set of operation is open and close. Why the statement in the main function is USEUSB (NULL), NULL is because the object is not yet determined, and the object is the peripheral to be added later. In fact, the most important thing is the interface, and the description of the code location of the function. The method of Useusb () exists at the same time as the main function, because when the computer is present, the rules must be determined and the rules explicitly used. Why is the definition of an interface placed outside? To recall from the above example, to move the mouse method outside of the computer class, this is added later. For peripheral extraction, a USB interface is formed. But in the end when writing code, it must be the first to write the code of the USB interface, and then write the computer class.

The above two, the order of instruction is from the top down. First, there is one above, and then the next one. If the class is written as Class Upan, such peripherals do not match the computer's USB rules (the defined USB interface is the rule.) Rules are defined outside the computer class and can be defined in a computer class. ), so to implement the USB interface, to be written as class Upan implements USB. At the same time, the U disk in this class, there must be a switch method. Finally, the main function in the Useusb () method is not empty, there is an object.

Here, the computer must have interface settings, peripherals also have interface settings, this is two parts. (In the actual development, will usually be made part, just need to implement the interface in the sub-class, to the main function of a put, the inside of the automatic operation.) At the time of writing, the front is all done. The computer is already ready, everyone is more research peripheral equipment. )

In this way, from the overall point of view, the preliminary design preparation, later in the add equipment to the inside, so can be used.

Note: References to interface types point to child class objects, and interfaces cannot create objects.

Three features of the interface in the application of the program,

Compared to the first mouse placement in the main function, now the mouse, U disk and so on is later, so the coupling is worse.

Preliminary understanding of Java--The tenth chapter-object-oriented-interface application

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.