ÐòÑÔ
×î½üѧϰjava£¬½Ó´¥µ½Á˻ص÷»úÖÆ(CallBack)¡£³õʶʱ¸Ð¾õ±È½Ï»ìÂÒ£¬¶øÇÒÔÚÍøÉÏËÑË÷µ½µÄÏà¹ØµÄ½²½â£¬ÒªÃ´Ò»ÑÔ´ø¹ý£¬ÒªÃ´ËµµÄ±È½Ïµ¥´¿µÄÏñÊǸøCallBack×öÁËÒ»¸ö¶¨Òå¡£µ±È»ÁË£¬ÎÒÔÚÀí½âÁ˻ص÷Ö®ºó£¬ÔÙÈ¥¿´ÍøÉϵĸ÷ÖÖ½²½â£¬È·ÊµÃ»Ê²Ã´ÎÊÌâ¡£µ«ÊÇ£¬¶ÔÓÚ³õѧµÄÎÒÀ´Ëµ£¬È±ÁËÒ»¸öÑÐò½¥½øµÄ¹ý³Ì¡£´Ë´¦£¬½«ÎҶԻص÷»úÖƵĸöÈËÀí½â£¬°´ÕÕÓÉdzµ½ÉîµÄ˳ÐòÃèÊöһϣ¬ÈçÓв»Í×Ö®´¦£¬Íû²»Áߴͽ̣¡
¿ªÊ¼Ö®Ç°£¬ÏÈÏëÏóÒ»¸ö³¡¾°£ºÓ×ÖÉÔ°µÄСÅóÓѸոÕѧϰÁË10ÒÔÄڵļӷ¨¡£
µÚ1ÕÂ. ¹ÊʵÄÔµÆð
Ó×ʦÔÚºÚ°åÉÏдһ¸öʽ×Ó “1 + 1 = ”£¬ÓÉСÃ÷ͬѧÀ´Ìî¿Õ¡£
ÓÉÓÚÒѾѧϰÁË10ÒÔÄڵļӷ¨£¬Ð¡Ã÷ͬѧ¿ÉÒÔÍêÈ«¿¿×Ô¼ºÀ´¼ÆËãÕâ¸öÌâÄ¿£¬Ä£Äâ¸Ã¹ý³ÌµÄ´úÂëÈçÏ£º
Student 2 { 3 private String name = null; Student(String name) 6 { .name = name; 9 } setName(String name) 12 { 13 this.name = name; 14 } calcADD(int a, int b) 17 { 18 return a + b; 19 } fillBlank(int a, int b) 22 { 23 int result = calcADD(a, b); 24 System.out.println(name + "ÐÄËã:" + a + " + " + b + " = " + result); 25 } 26 }
СÃ÷ͬѧÔÚÌî¿Õ(fillBalnk)µÄʱºò£¬Ö±½ÓÐÄËã(clacADD)ÁËһϣ¬µÃ³ö½á¹ûÊÇ2£¬²¢½«½á¹ûдÔÚ¿Õ¸ñÀï¡£²âÊÔ´úÂëÈçÏ£º
Test 2 { main(String[] args) 4 { 5 int a = 1; 6 int b = 1; 7 Student s = new Student("СÃ÷"); 8 s.fillBlank(a, b); 9 } 10 }
ÔËÐнá¹ûÈçÏ£º
СÃ÷ÐÄËã:1 + 1 = 2
¸Ã¹ý³ÌÍêÈ«ÓÉStudentÀàµÄʵÀý¶ÔÏ󵥶ÀÍê³É£¬²¢Î´Éæ¼°»Øµ÷»úÖÆ¡£
µÚ2ÕÂ. Ó×ʦµÄÕÒ²ç
¿Î¼ä£¬Ó×ʦͻ·¢ÆæÏëÔÚºÚ°åÉÏдÁË“168 + 291 = ”ÈÃСÃ÷Íê³É£¬È»ºó»Ø°ì¹«ÊÒÁË¡£
»¨²Á£¡ÎªÊ²Ã´ËùÓÐÀÏʦ¶¼¸úСÃ÷¹ý²»È¥°¡£¿Ã÷Ã÷³¬¸ÙÁ˺ò»ºÃ£¡ÕâʱºòСÃ÷ͬѧÃ÷ÏÔ²»ÄÜÔÙÏñÉÏÃæÄÇÑù¿¿ÐÄËãÀ´Íê³ÉÁË£¬ÕýÔÚã±ƵÄʱºò£¬°àÉϵÄСºìͬѧµÝ¹ýÀ´Ò»¸öÖ»ÄܼÆËã¼Ó·¨µÄ¼ÆËãÆ÷£¨¼éÉÌ°¡£©£¡£¡£¡£¡¶øСÃ÷ͬѧǡºÃÖªµÀÔõôÓüÆËãÆ÷£¬ÓÚÊÇͨ¹ý¼ÆËãÆ÷¼ÆËãµÃµ½½á¹û²¢Íê³ÉÁËÌî¿Õ¡£
¼ÆËãÆ÷µÄ´úÂëΪ£º
Calculator 2 { add(int a, int b) 4 { 5 return a + b; 6 } 7 }
ÐÞ¸ÄStudentÀ࣬Ìí¼ÓʹÓüÆËãÆ÷µÄ·½·¨£º
Student 2 { 3 private String name = null; Student(String name) 6 { .name = name; 9 } setName(String name) 12 { 13 this.name = name; 14 } 15 16 @SuppressWarnings("unused") calcADD(int a, int b) 18 { 19 return a + b; 20 } useCalculator(int a, int b) 23 { Calculator().add(a, b); 25 } fillBlank(int a, int b) 28 { 29 int result = useCalculator(a, b); 30 System.out.println(name + "ʹÓüÆËãÆ÷:" + a + " + " + b + " = " + result); 31 } 32 }
²âÊÔ´úÂëÈçÏ£º
Test 2 { main(String[] args) 4 { 5 int a = 168; 6 int b = 291; 7 Student s = new Student("СÃ÷"); 8 s.fillBlank(a, b); 9 } 10 }
ÔËÐнá¹ûÈçÏ£º
СÃ÷ʹÓüÆËãÆ÷:168 + 291 = 459
¸Ã¹ý³ÌÖÐÈÔδÉæ¼°µ½»Øµ÷»úÖÆ£¬µ«ÊDz¿·ÖСÃ÷µÄ²¿·Ö¹¤×÷ÒѾʵÏÖÁËתÒÆ£¬ÓɼÆËãÆ÷À´ÐÖúʵÏÖ¡£
3. Ó×ʦ»ØÀ´ÁË
·¢ÏÖСÃ÷Íê³ÉÁË3λÊýµÄ¼Ó·¨£¬ÀÏʦ¾õµÃСÃ÷ºÜ´ÏÃ÷£¬ÊǸö¿ÉËÜÖ®²Å¡£ÓÚÊÇÓÖÔÚºÚ°åÉÏдÏÂÁË“26549 + 16487 = ”£¬ÈÃСÃ÷ÉÏ¿Î֮ǰÍê³ÉÌî¿Õ£¬È»ºóÓֻذ칫ÊÒÁË¡£
СÃ÷¿´×ŽÌÊÒÍâÃæÈö»¶¶ùµÄС»ï°é£¬²»½û±¯´ÓÖÐÀ´¡£ÔÙ²»³öÈ¥Í棬Õâ¸ö¿Î¼ä¾ÍÒª·ÏÁË°¡£¡£¡£¡£¡ ¿´×ÅСºìÔÙÒ»´ÎµÝÉÏÀ´µÄ¼ÆËãÆ÷£¬Ð¡Ã÷ÐÄÉúÒ»¼Æ£ºÈÃСºì´úÀÍ¡£
СÃ÷¸æËßСºìÌâÄ¿ÊÇ“26549 + 16487 = ”£¬È»ºóÖ¸³öÌîд½á¹ûµÄ¾ßÌåλÖã¬È»ºó¾Í³öÈ¥¿ìÀÖµÄÍæË£ÁË¡£
¡¡