Upstream transformation and interface callback?

Source: Internet
Author: User
Tags export class
Original: http://www.blogjava.net/Carter0618/archive/2007/08/19/137936.html 1 What is the interface callback? [2] ?Interface callback refers to the interface variables that can be assigned to an object created by a class using an interface, this interface variable can call the method of the interface implemented by the class. In fact, when an interface variable calls a method in the interface implemented by the class, it notifies the corresponding object to call the interface method. This process is called the interface callback of the object function. See the following example.

 

Interface people {void peoplelist ();} class student implements people {public void peoplelist () {system. out. println ("I'm a student. ") ;}} class teacher implements people {public void peoplelist () {system. out. println ("I'm a teacher. ") ;}} public class example {public static void main (string ARGs []) {People A; // declare the interface variable A = new student (); // instantiate, store object references in interface variables.. peoplelist (); // interface callback A = new teacher (); // instantiated, stored in interface Variables Reference A. peoplelist (); // interface callback} result: I'm a student. I'm a teacher. Let's take a look at the concept of upcasting. 2 . What is upward Transformation [1] ? Shape S = new circle (); here, a circle object is created and the reference is assigned to the shape immediately. By inheritance, circle is a shape. Suppose you call the base class method (it has been overwritten in the export class): S. draw (); due to the later binding (polymorphism), circle will be correctly called. draw () method.

3 . Differences between upstream transformation and interface callbackSeemingly upward transformation and interface callback are the same thing. The following two sentences are from thinking in Java. Core reasons for using interfaces: to be able to transform upwards into multiple base types [1]. That is, multiple implementations of interfaces can be transformed to multiple interface base types (for details, see section 6 of abstraction and interfaces ). The reference to an interface is obtained from an object that implements an interface. The result is essentially the same as that of the base class that is transformed to this object. (This sentence is taken from the thinking in Java 3rd interface and internal class chapter.) Therefore, I think these two concepts are to explain an action in two aspects. The concept of interface callback emphasizes the function of using an interface to implement the right to use the method of the callback object (detailed analysis in the next chapter ). The upward transformation involves polymorphism and runtime binding. 4 . Use Java Interfaces implement equivalent functions of callback FunctionsDevelopers familiar with the MS-windows and X Window System event-driven programming models are used to passing function pointers that are called ("Callback") when an event occurs. Java's object-oriented model currently does not support method pointers. Java interfaces support a mechanism to obtain the equivalent functions of callback. The trick is to define a simple interface and declare the method we want to call in this interface. Suppose we want to be notified when an event occurs. We can define an interface: interestingevent. Java
Package org. ZJ. sample; public interface interestingevent {public void interestingevent ();}
This allows us to control any object of the class that implements this interface. Therefore, we do not need to care about any external type information. The class that sends the event signal must wait for the object that implements the interestingevent interface and call the interestingevent () method as appropriate. Eventnotifier. Java
Package org. ZJ. sample; public class eventnotifier {private interestingevent ie; private Boolean somethinghappened; Public eventnotifier (interestingevent event) {Ie = event; // Save the event object for future use. Somethinghappened = false; // No events to be reported. } Public void dowork () {If (somethinghappened) {// check the configured predicate. Ie. interestingevent (); // send an event signal by calling this method of the interface. } Public void sethappened () {// set the predicate. Somethinghappened = true ;}}
In the preceding example, the somethinghappened predicate is used to track whether an event should be triggered. The code for Receiving Event Notifications must implement the interestingevent interface and pass its reference to the Event Notification Program. Callme. Java
Package Org. ZJ. sample; public class callme implements interestingevent {@ suppresswarnings ("UNUSED") Private eventnotifier en; Public callme () {// note eventnotifier (interestingevent event), an interface type should be passed. // In the following example, the callme instance that implements the interestingevent interface is passed to // eventnotifier. That is, the so-called interface callback. En = new eventnotifier (this); // create an event notification program and pass its reference to it. } // Define the actual handler for the event. Public void interestingevent () {system. Out. println ("call me hello .");}}
Below is a test class. Test. Java
Package Org. ZJ. sample; public class test {public static void main (string [] ARGs) {eventnotifier en = new eventnotifier (New callme (); en. sethappened (); en. dowork ();}}
Result: Call me hello. 5 . References[1] thinking in Java 3rd [2] Ma xiangyi, Java practical tutorial, Tsinghua University Press [3] John D. Mitchell, Java interface to implement equivalent functions of callback functions, ibmdeveloper website

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.