ÔÚJava ÖУ¬Í¬Ò»¸öÀàÖеÄ2¸ö»ò2¸öÒÔÉϵķ½·¨¿ÉÒÔÓÐͬһ¸öÃû×Ö£¬Ö»ÒªËüÃǵIJÎÊýÉùÃ÷²»Í¬¼´¿É¡£ÔÚÕâÖÖÇé¿öÏ£¬¸Ã·½·¨¾Í±»³ÆΪÖØÔØ£¨overloaded £©£¬Õâ¸ö¹ý³Ì³ÆΪ·½·¨ÖØÔØ£¨method overloading £©¡£·½·¨ÖØÔØÊÇJava ʵÏÖ¶à̬ÐÔµÄÒ»ÖÖ·½Ê½¡£Èç¹ûÄãÒÔÇ°´ÓÀ´Ã»ÓÐʹÓùýÒ»ÖÖÔÊÐí·½·¨ÖØÔصÄÓïÑÔ£¬Õâ¸ö¸ÅÄî×î³õ¿ÉÄÜÓеãÆæ¹Ö¡£µ«ÊÇÄ㽫¿´µ½£¬·½·¨ÖØÔØÊÇJava ×¶¯ÈËÐĺÍ×îÓÐÓõÄÌØÐÔÖ®Ò»¡£
µ±Ò»¸öÖØÔØ·½·¨±»µ÷ÓÃʱ£¬Java ÓòÎÊýµÄÀàÐͺͣ¨»ò£©ÊýÁ¿À´±íÃ÷ʵ¼Êµ÷ÓõÄÖØÔØ·½·¨µÄ°æ±¾¡£Òò´Ë£¬Ã¿¸öÖØÔØ·½·¨µÄ²ÎÊýµÄÀàÐͺͣ¨»ò£©ÊýÁ¿±ØÐëÊDz»Í¬µÄ¡£ËäȻÿ¸öÖØÔØ·½·¨¿ÉÒÔÓв»Í¬µÄ·µ»ØÀàÐÍ£¬µ«·µ»ØÀàÐͲ¢²»×ãÒÔÇø·ÖËùʹÓõÄÊÇÄĸö·½·¨¡£µ±Java µ÷ÓÃÒ»¸öÖØÔØ·½·¨Ê±£¬²ÎÊýÓëµ÷ÓòÎÊýÆ¥ÅäµÄ·½·¨±»Ö´ÐС£
ÏÂÃæÊÇÒ»¸ö˵Ã÷·½·¨ÖØÔصļòµ¥Àý×Ó£º
// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// Overload test for two integer parameters. void test(int a£¬int b) { System.out.println("a and b: " + a + " " + b);}
// overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a; }}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()ob.test();ob.test(10);ob.test(10£¬20);result = ob.test(123.25);System.out.println("Result of ob.test(123.25): " + result);
}
}
¸Ã³ÌÐò²úÉúÈçÏÂÊä³ö£º
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625
´ÓÉÏÊö³ÌÐò¿É¼û£¬test()±»ÖØÔØÁËËĴΡ£µÚÒ»¸ö°æ±¾Ã»ÓвÎÊý£¬µÚ¶þ¸ö°æ±¾ÓÐÒ»¸öÕûÐͲÎÊý£¬µÚÈý¸ö°æ±¾ÓÐÁ½¸öÕûÐͲÎÊý£¬µÚËĸö°æ±¾ÓÐÒ»¸ödouble ÐͲÎÊý¡£ÓÉÓÚÖØÔز»ÊÜ·½·¨µÄ·µ»ØÀàÐ͵ÄÓ°Ï죬test()µÚËĸö°æ±¾Ò²·µ»ØÁËÒ»¸öºÍÖØÔØûÓÐÒò¹û¹ØϵµÄÖµ¡£
µ±Ò»¸öÖØÔصķ½·¨±»µ÷ÓÃʱ£¬Java ÔÚµ÷Ó÷½·¨µÄ²ÎÊýºÍ·½·¨µÄ×Ô±äÁ¿Ö®¼äÑ°ÕÒÆ¥Åä¡£µ«ÊÇ£¬ÕâÖÖÆ¥Åä²¢²»×ÜÊǾ«È·µÄ¡£ÔÚһЩÇé¿öÏ£¬Java µÄ×Ô¶¯ÀàÐÍת»»Ò²ÊÊÓÃÓÚÖØÔØ·½·¨µÄ×Ô±äÁ¿¡£ÀýÈ磬¿´ÏÂÃæµÄ³ÌÐò£º
// Automatic type conversions apply to overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for two integer parameters. void test(int a£¬int b) { System.out.println("a and b: " + a + " " + b);}
// overload test for a double parameter
void test(double a) {
System.out.println("Inside test(double) a: " + a);
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
int i = 88;
ob.test();ob.test(10£¬20);
ob.test(i); // this will invoke test(double)
ob.test(123.2); // this will invoke test(double)
}
}
¸Ã³ÌÐò²úÉúÈçÏÂÊä³ö£º
No parameters
a and b: 10 20
Inside test(double) a: 88
Inside test(double) a: 123.2
ÔÚ±¾ÀýÖУ¬OverloadDemo µÄÕâ¸ö°æ±¾Ã»Óж¨Òåtest(int) ¡£Òò´Ëµ±ÔÚOverload ÄÚ´øÕûÊý²ÎÊýµ÷ÓÃtest()ʱ£¬ÕÒ²»µ½ºÍËüÆ¥ÅäµÄ·½·¨¡£µ«ÊÇ£¬Java ¿ÉÒÔ×Ô¶¯µØ½«ÕûÊýת»»Îªdouble ÐÍ£¬ÕâÖÖת»»¾Í¿ÉÒÔ½â¾öÕâ¸öÎÊÌâ¡£Òò´Ë£¬ÔÚtest(int) ÕÒ²»µ½ÒÔºó£¬Java ½«iÀ©´óµ½double ÐÍ£¬È»ºóµ÷ÓÃtest(double) ¡£µ±È»£¬Èç¹û¶¨ÒåÁËtest(int) £¬µ±È»Ïȵ÷ÓÃtest(int) ¶ø²»»áµ÷ÓÃtest(double) ¡£Ö»ÓÐÔÚÕÒ²»µ½¾«È·Æ¥Åäʱ£¬Java µÄ×Ô¶¯×ª»»²Å»áÆð×÷Óá£
·½·¨ÖØÔØÖ§³Ö¶à̬ÐÔ£¬ÒòΪËüÊÇJava ʵÏÖ¡°Ò»¸ö½Ó¿Ú£¬¶à¸ö·½·¨¡±·¶Ð͵ÄÒ»ÖÖ·½Ê½¡£ÒªÀí½âÕâÒ»µã£¬¿¼ÂÇÏÂÃæÕâ¶Î»°£ºÔÚ²»Ö§³Ö·½·¨ÖØÔصÄÓïÑÔÖУ¬Ã¿¸ö·½·¨±ØÐëÓÐÒ»¸öΩһµÄÃû×Ö¡£µ«ÊÇ£¬Äã¾³£Ï£ÍûʵÏÖÊý¾ÝÀàÐͲ»Í¬µ«±¾ÖÊÉÏÏàͬµÄ·½·¨¡£¿ÉÒԲο¼¾ø¶ÔÖµº¯ÊýµÄÀý×Ó¡£ÔÚ²»Ö§³ÖÖØÔصÄÓïÑÔÖУ¬Í¨³£»áº¬ÓÐÕâ¸öº¯ÊýµÄÈý¸ö¼°Èý¸öÒÔÉϵİ汾£¬Ã¿¸ö°æ±¾¶¼ÓÐÒ»¸ö²î±ðÉõ΢µÄÃû×Ö¡£ÀýÈ磬ÔÚCÓïÑÔÖУ¬º¯Êýabs( )·µ»ØÕûÊýµÄ¾ø¶ÔÖµ£¬labs( ) ·µ»Ølong ÐÍÕûÊýµÄ¾ø¶ÔÖµ( )£¬¶øfabs( )·µ»Ø¸¡µãÖµµÄ¾ø¶ÔÖµ¡£¾¡¹ÜÕâÈý¸öº¯ÊýµÄ¹¦ÄÜʵÖÊÉÏÊÇÒ»ÑùµÄ£¬µ«ÊÇÒòΪCÓïÑÔ²»Ö§³ÖÖØÔØ£¬Ã¿¸öº¯Êý¶¼ÒªÓÐËü×Ô¼ºµÄÃû×Ö¡£ÕâÑù¾ÍʹµÃ¸ÅÄîÇé¿ö¸´ÔÓÐí¶à¡£¾¡¹Üÿһ¸öº¯ÊýDZÔڵĸÅÄîÊÇÏàͬµÄ£¬ÄãÈÔÈ»²»µÃ²»¼ÇסÕâÈý¸öÃû×Ö¡£ÔÚJava ÖоͲ»»á·¢ÉúÕâÖÖÇé¿ö£¬ÒòΪËùÓеľø¶ÔÖµº¯Êý¿ÉÒÔʹÓÃͬһ¸öÃû×Ö¡£È·Êµ,Java µÄ±ê×¼µÄÀà¿â°üº¬Ò»¸ö¾ø¶ÔÖµ·½·¨£¬½Ð×öabs ( )¡£Õâ¸ö·½·¨±»Java µÄmath ÀàÖØÔØ£¬ÓÃÓÚ´¦ÀíÊý×ÖÀàÐÍ¡£Java ¸ù¾Ý²ÎÊýÀàÐ;ö¶¨µ÷ÓõÄabs()µÄ°æ±¾¡£
ÖØÔصļÛÖµÔÚÓÚËüÔÊÐíÏà¹ØµÄ·½·¨¿ÉÒÔʹÓÃͬһ¸öÃû×ÖÀ´·ÃÎÊ¡£Òò´Ë£¬absÕâ¸öÃû×Ö´ú±íÁËËüÖ´ÐеÄͨÓö¯×÷£¨general action £©¡£ÎªÌض¨»·¾³Ñ¡ÔñÕýÈ·µÄÖ¸¶¨£¨specific £©°æ±¾ÊDZàÒëÆ÷Òª×öµÄÊÂÇé¡£×÷Ϊ³ÌÐòÔ±µÄÄ㣬ֻÐèÒª¼ÇסִÐеÄͨÓòÙ×÷¾ÍÐÐÁË¡£Í¨¹ý¶à̬ÐÔµÄÓ¦Ó㬼¸¸öÃû×Ö¼õÉÙΪһ¸ö¡£¾¡¹ÜÕâ¸öÀý×ÓÏ൱¼òµ¥£¬µ«Èç¹ûÄ㽫Õâ¸ö¸ÅÄîÀ©Õ¹Ò»Ï£¬Äã¾Í»áÀí½âÖØÔØÄܹ»°ïÖúÄã½â¾ö¸ü¸´ÔÓµÄÎÊÌâ¡£
µ±ÄãÖØÔØÒ»¸ö·½·¨Ê±,¸Ã·½·¨µÄÿ¸ö°æ±¾¶¼Äܹ»Ö´ÐÐÄãÏëÒªµÄÈκζ¯×÷¡£Ã»ÓÐʲô¹æ¶¨ÒªÇóÖØÔØ·½·¨Ö®¼ä±ØÐ뻥Ïà¹ØÁª¡£µ«ÊÇ£¬´Ó·ç¸ñÉÏÀ´Ëµ£¬·½·¨ÖØÔØ»¹ÊÇ°µÊ¾ÁËÒ»ÖÖ¹Øϵ¡£Õâ¾ÍÊǵ±ÄãÄܹ»Ê¹ÓÃͬһ¸öÃû×ÖÖØÔØÎ޹صķ½·¨Ê±£¬Äã²»Ó¦¸ÃÕâô×ö¡£ÀýÈ磬Äã¿ÉÒÔʹÓÃsqrÕâ¸öÃû×ÖÀ´´´½¨Ò»ÖÖ·½·¨£¬¸Ã·½·¨·µ»ØÒ»¸öÕûÊýµÄƽ·½ºÍÒ»¸ö¸¡µãÊýÖµµÄƽ·½¸ù¡£µ«ÊÇÕâÁ½ÖÖ²Ù×÷ÔÚ¹¦ÄÜÉÏÊDz»Í¬µÄ¡£°´ÕÕÕâÖÖ·½Ê½Ó¦Ó÷½·¨¾ÍÎ¥±³ÁËËüµÄ³õÖÔ¡£ÔÚʵ¼ÊµÄ±à³ÌÖУ¬ÄãÓ¦¸ÃÖ»ÖØÔØÏ໥֮¼ä¹Øϵ½ôÃܵIJÙ×÷¡£
7.1.1 ¹¹Ô캯ÊýÖØÔØ
³ýÁËÖØÔØÕý³£µÄ·½·¨Í⣬¹¹Ô캯ÊýÒ²Äܹ»ÖØÔØ¡£Êµ¼ÊÉÏ£¬¶ÔÓÚ´ó¶àÊýÄã´´½¨µÄÏÖʵµÄ
À࣬ÖØÔع¹Ô캯ÊýÊǺܳ£¼ûµÄ£¬²¢²»ÊÇʲôÀýÍ⡣ΪÁËÀí½âΪʲô»áÕâÑù£¬ÈÃÎÒÃÇ»ØÏëÉÏÒ»ÕÂÖоٹýµÄBoxÀàÀý×Ó¡£ÏÂÃæÊÇ×îа汾µÄBoxÀàµÄÀý×Ó£º
class Box { double width; double height; double depth;
// This is the constructor for Box.
Box(double w£¬double h£¬double d) {width = w; height = h;depth = d;
}
// compute and return volume double volume() { return width * height * depth;}}
ÔÚ±¾ÀýÖУ¬Box() ¹¹Ô캯ÊýÐèÒªÈý¸ö×Ô±äÁ¿£¬ÕâÒâζ×Ŷ¨ÒåµÄËùÓÐBox¶ÔÏó±ØÐë¸øBox() ¹¹Ô캯Êý´«µÝÈý¸ö²ÎÊý¡£ÀýÈ磬ÏÂÃæµÄÓï¾äÔÚµ±Ç°Çé¿öÏÂÊÇÎÞЧµÄ£º
Box ob = new Box();
ÒòΪBox( )ÒªÇóÓÐÈý¸ö²ÎÊý£¬Òò´ËÈç¹û²»´ø²ÎÊýµÄµ÷ÓÃËüÔòÊÇÒ»¸ö´íÎó¡£Õâ»áÒýÆðһЩÖØÒªµÄÎÊÌâ¡£Èç¹ûÄãÖ»ÏëÒªÒ»¸öºÐ×Ó¶ø²»ÔÚºõ (»òÖªµÀ)ËüµÄÔʼµÄ³ß´ç¸ÃÔõô°ì£¿»ò£¬Èç¹ûÄãÏëÓýö½öÒ»¸öÖµÀ´³õʼ»¯Ò»¸öÁ¢·½Ì壬¶ø¸ÃÖµ¿ÉÒÔ±»ÓÃ×÷ËüµÄËùÓеÄÈý¸ö³ß´çÓÖ¸ÃÔõô°ì£¿Èç¹ûBox ÀàÊÇÏñÏÖÔÚÕâÑùдµÄ£¬Óë´ËÀàËƵÄÆäËûÎÊÌâÄ㶼ûÓа취½â¾ö£¬ÒòΪÄãÖ»ÄÜ´øÈý¸ö²ÎÊý¶øûÓбðµÄÑ¡ÔñȨ¡£
ÐҺ㬽â¾öÕâЩÎÊÌâµÄ·½°¸ÊÇÏ൱ÈÝÒ׵ģºÖØÔØBox ¹¹Ô캯Êý£¬Ê¹ËüÄÜ´¦Àí¸Õ²ÅÃèÊöµÄÇé¿ö¡£ÏÂÃæ³ÌÐòÊÇBox µÄÒ»¸ö¸Ä½ø°æ±¾£¬Ëü¾ÍÊÇÔËÓöÔBox¹¹Ô캯ÊýµÄÖØÔØÀ´½â¾öÕâЩÎÊÌâµÄ£º
/* Here£¬Box defines three constructors to initialize
the dimensions of a box various ways.
*/
class Box {
double width; double height; double depth; // constructor used when all dimensions specified Box(double w£¬double h£¬double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created Box(double len) { width = height = depth = len;}
// compute and return volume double volume() { return width * height * depth;}}
class OverloadCons {
public static void main(String args[]) { // create boxes using the various constructorsBox mybox1 = new Box(10£¬20£¬15);Box mybox2 = new Box();Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}
}
¸Ã³ÌÐò²úÉúµÄÊä³öÈçÏÂËùʾ£º
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of mycube is 343.0
ÔÚ±¾ÀýÖУ¬µ±newÖ´ÐÐʱ£¬¸ù¾ÝÖ¸¶¨µÄ×Ô±äÁ¿µ÷ÓÃÊʵ±µÄ¹¹Ô캯Êý¡£