Android's understanding of callback Functions

Source: Internet
Author: User

I personally think callback is like polymorphism. For example, a boss calls an employee to eat, but each employee may eat different foods. However, the callback is for the interface. In short, object a calls its own method A, and method A receives the parameter B of the Instance B of interface B. In method A, the method C in interface B is executed. This may be a bit difficult to explain. Let's look at an example below.

For example, if a student in the dormitory sets an alarm, all the people in the dormitory can hear it when the alarm rings. However, James is diligent and gets up after hearing the alarm. Li Si is lazy, sleep after hearing the bell. We can regard the ringtone that is sent as an interface. Both Zhang San and Li Si have implemented this "interface". after a certain period of time, the "ringtone" will be triggered ", "ringtones" are called by people, and what people will do is what people do. See the code below.

1. Interface callperson

  1. Publicinterface callperson {
  2. Publicpolicactvoid docallperson ();
  3. }
Defines a "human" method. This interface can be understood as installing an alarm. 2. Class zhangsan
  1. Publicclass zhangsan implements callperson {
  2. String name = "zhangsan ";
  3. Public String getname (){
  4. Return name;
  5. }
  6. @ Override
  7. Publicvoid docallperson (){
  8. System. Out. println (getname () + "when I heard the bell, I got up! ");
  9. }
  10. }
3. Class Lisi
  1. Publicclass Lisi implements callperson {
  2. String name = "Lisi ";
  3. Public String getname (){
  4. Return name;
  5. }
  6. @ Override
  7. Publicvoid docallperson (){
  8. System. Out. println (getname () + "when I heard the bell, I got up! ");
  9. }
  10. }
4. Class alarm
  1. Publicclass alarm {
  2. // Ring the bell
  3. Publicvoid ring (callperson ){
  4. Callperson. docallperson ();
  5. }
  6. }
5. The activity then simulates an alarm in the activity and starts to ring.
  1. Import Android. OS. Bundle;
  2. Import Android. App. activity;
  3. Publicclass mainactivity extends activity {
  4. @ Override
  5. Publicvoid oncreate (bundle savedinstancestate ){
  6. Super. oncreate (savedinstancestate );
  7. Setcontentview (R. layout. layout_main );
  8. Alarm alarm = new alarm ();
  9. Zhangsan = new zhangsan ();
  10. Lisi = new Lisi ();
  11. // Simulate the scheduled time when the alarm is triggered and then the alarm is triggered.
  12. Alarm. Ring (zhangsan );
  13. Alarm. Ring (Lisi );
  14. }
  15. }
Finally, let's take a look at the execution results.
  1. 12-07 21:49:41. 947: INFO/system. Out (6107): zhangsan got up when he heard the bell!
  2. 12-07 21:49:41. 947: INFO/system. Out (6107): Lisi heard the bell and got up late.
Related Article

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.