Java learning notes (events, internal classes)

Source: Internet
Author: User

Java currently does not support delegation, that is, method callback. to achieve this function, object callback is required. although Java provides anonymous objects to simplify the compilation of event processing objects to some extent, there are many interface methods. If you use named objects or anonymous objects to process events, you need to implement all the methods of this event interface. Most of the time, you only need to implement one or more of these methods. Using the method of directly implementing the interface will increase the workload, if there are more than one interface, it will make the event interface processing programming look messy and complicated. Therefore, use the event adapter to give the interface a default implementation, and the user's event will be inherited from this adaptive class, in the inheritance class, you only need to implement the required methods without implementing all the methods, which simplifies the event processing code. another benefit is the benefits of "adaptation mode. this adaptation can be used to separate the event interface from the specific event handler, because the event interface is generally at a more abstract level, which is generally relatively fixed, the event handler is generally at the specific layer. The called interface of the specific handler does not necessarily conform to the event interface description required for the event trigger call. Therefore An adapter class can be used to transfer such calls, providing better flexibility and scalability, and improving code reuse. For example, when a button is clicked, the processing code after the button is clicked has its own call interface, but the button clicking event can be triggered by a mouse clicking, it can also be triggered by the carriage return event in the focus.

Using the adapter method has many advantages, but there is also a disadvantage that it is easy to generate imperceptible errors, the root cause of this error is that no keyword exists in the virtual method and overwrite method in Java inheritance, while Java is case sensitive, so it is easy to write the method that overwrites the error.

The internal class of Java is a very interesting thing. It does not belong to an external class, but an instance of an external class, which is different from the internal class of C. The internal class of C # differs from the external class only in the access permission. The internal class can directly access members of the external class, and access the current instance of the external class through the external class name someclass. This. The static internal class does not belong to the instance of the external class. Therefore, you cannot call instance methods of external classes. The mechanism of common internal classes provides a lot of convenience in implementing thread security, and it is easy to form a set of instance collaboration objects. Therefore, it is better to use internal classes to process Java events.

In Java, except for private and final methods, they are all Virtual Methods and can be overwritten in subclass. although this method can use less than two keywords, but as a strong language, this is not good for programmers, it is easy to cause unnecessary errors, it is difficult for programmers to distinguish between overwrite and reload, and the case sensitivity also makes it difficult to overwrite the parent class method. in many cases, public methods do not mean virtual methods. To determine whether a method is a virtual method, you can give it to the programmer. This also increases the code security. the virtual mode can often be used to replace some Aspect-oriented programming functions. It is a common design mode, however, it is difficult to implement this mode in Java (the virtual mode is described in my previous blog ).
In terms of event processing mechanism, I still hope that Java will support the delegate type in future versions. This not only simplifies the writing of Event code, but also facilitates event interaction between multiple classes. reduce Unnecessary interface implementation.

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.