Method overload. Why cannot it be distinguished based on the return type ?, Reload return

Source: Internet
Author: User

Method overload. Why cannot it be distinguished based on the return type ?, Reload return

For details, see: 4.

I. Method Overloading

An important feature of any programming language is the use of names. When an object is created, a name is assigned to the bucket assigned to the object.

The so-called method is the name of an action. With this name, you can reference all objects and methods. A good name can make the system easier to understand and modify.

In daily life, the same words can express different meanings-they are "overloaded. This method is useful, especially when there is a small difference between meanings. Compared with "Wash shirts", "wash dogs in dog mode", and "wash cars in car wash ", "shirt washing", "dog washing", and "Car Washing" are more concise and convenient.

Wash (Shirt); wash (Dog dog); wash (Car car );

Most programming languages (especially C) require a unique identifier for each method. Therefore, you must not use a function named print () to display an integer. print () can be used to display floating point numbers. Overload is not supported. Each function must have a unique name.

Java and C ++ support overloading (for example, constructor overloading, using different constructor methods to create an object in multiple ways ).

 

Ii. Distinguishing heavy load methods

The rule is simple: each overload method must have a unique list of parameter types

1. Number of parameters

2. Parameter type

3. Parameter order (generally not used, which may cause maintenance difficulties)

 

Iii. Heavy Load involving basic types

1. When the real parameter type is smaller than the shape parameter type, the actual data type will be upgraded.

Note: The char type is slightly different. If you cannot find a method that just accepts the char parameter, the char type will be upgraded directly to the int type.

Sample Code:

1 package com. mianshi. easy; 2 3 public class Overload {4 5 void f1 (char x) {System. out. print ("f1 (char)" + ",") ;}; 6 void f1 (byte x) {System. out. print ("f1 (byte)" + ",") ;}; 7 void f1 (int x) {System. out. print ("f1 (int)" + ",") ;}; 8 void f1 (long x) {System. out. print ("f1 (long)" + ",") ;}; 9 void f1 (float x) {System. out. print ("f1 (float)" + ",") ;}; 10 void f1 (short x) {System. out. print ("f1 (short)" + ",") ;}; 11 void f1 (double x) {System. out. print ("f1 (double)" + ",") ;}; 12 13 void f2 (byte x) {System. out. print ("f2 (byte)" + ",") ;}; 14 void f2 (int x) {System. out. print ("f2 (int)" + ",") ;}; 15 void f2 (long x) {System. out. print ("f2 (long)" + ",") ;}; 16 void f2 (float x) {System. out. print ("f2 (float)" + ",") ;}; 17 void f2 (short x) {System. out. print ("f2 (short)" + ",") ;}; 18 void f2 (double x) {System. out. print ("f2 (double)" + ",") ;}; 19 20 void f3 (int x) {System. out. print ("f3 (int)" + ",") ;}; 21 void f3 (long x) {System. out. print ("f3 (long)" + ",") ;}; 22 void f3 (float x) {System. out. print ("f3 (float)" + ",") ;}; 23 void f3 (short x) {System. out. print ("f3 (short)" + ",") ;}; 24 void f3 (double x) {System. out. print ("f3 (double)" + ",") ;}; 25 26 void f4 (int x) {System. out. print ("f4 (int)" + ",") ;}; 27 void f4 (long x) {System. out. print ("f4 (long)" + ",") ;}; 28 void f4 (float x) {System. out. print ("f4 (float)" + ",") ;}; 29 void f4 (double x) {System. out. print ("f4 (double)" + ",") ;}; 30 31 void f5 (long x) {System. out. print ("f5 (long)" + ",") ;}; 32 void f5 (float x) {System. out. print ("f5 (float)" + ",") ;}; 33 void f5 (double x) {System. out. print ("f5 (double)" + ",") ;}; 34 35 void f6 (float x) {System. out. print ("f6 (float)" + ",") ;}; 36 void f6 (double x) {System. out. print ("f6 (double)" + ",") ;}; 37 38 void f7 (double x) {System. out. print ("f7 (double)" + ",") ;}; 39 40 void testConstVal () {41 System. out. print ("5:" + ""); 42 f1 (5); f2 (5); f3 (5); f4 (5); f5 (5 ); f6 (5); f7 (5); 43} 44 45 void testChar () {46 char x = 'X'; 47 System. out. print ("char:" + ""); 48 f1 (x); f2 (x); f3 (x); f4 (x); f5 (x ); f6 (x); f7 (x); 49} 50 51 void testByte () {52 byte x = 0; 53 System. out. print ("byte:" + ""); 54 f1 (x); f2 (x); f3 (x); f4 (x); f5 (x ); f6 (x); f7 (x); 55} 56 57 void testShort () {58 byte x = 0; 59 System. out. print ("short:" + ""); 60 f1 (x); f2 (x); f3 (x); f4 (x); f5 (x ); f6 (x); f7 (x); 61} 62 63 void testInt () {64 int x = 0; 65 System. out. print ("int:" + ""); 66 f1 (x); f2 (x); f3 (x); f4 (x); f5 (x ); f6 (x); f7 (x); 67} 68 void testLong () {69 long x = 0; 70 System. out. print ("long:" + ""); 71 f1 (x); f2 (x); f3 (x); f4 (x); f5 (x ); f6 (x); f7 (x); 72} 73 74 void testFloat () {75 float x = 0; 76 System. out. print ("float:" + ""); 77 f1 (x); f2 (x); f3 (x); f4 (x); f5 (x ); f6 (x); f7 (x); 78} 79 80 void testDouble () {81 double x = 0; 82 System. out. print ("double:" + ""); 83 f1 (x); f2 (x); f3 (x); f4 (x); f5 (x ); f6 (x); f7 (x); 84} 85 86 public static void main (String [] args) {87 88 Overload o = new Overload (); 89 90 // describe the following code one by one in order, and observe the result 91 o. testChar (); 92/* o. testByte (); 93 o. testConstVal (); 94 o. testInt (); 95 o. testShort (); 96 o. testLong (); 97 o. testFloat (); 98 o. testDouble (); */99} 100}View Code

Result:

char:   f1(char)、f2(int)、f3(int)、f4(int)、f5(long)、f6(float)、f7(double)、

When the input parameter is char type, f1 has the method to accept the char type parameter, directly called, The f2-f4 does not accept the char type parameter method, will be directly promoted to int type, then gradually improve in the f5-f7.

Comment 1, comment 2:

Result:

byte:   f1(byte)、f2(byte)、f3(short)、f4(int)、f5(long)、f6(float)、f7(double)、

In f1 and f2, the method that accepts byte parameters is called directly. The method that does not accept byte parameters in the f3-f7 will be gradually improved.

Comment the first two. Comment The Third One:

Result:

5:      f1(int)、f2(int)、f3(int)、f4(int)、f5(long)、f6(float)、f7(double)、

Constant Value 5 is treated as an int value, so if a heavy load method accepts an int value, it will be called, The f1-f4 accepts int type data, and then gradually improve.

Comment on the first three items. Comment on Article 4:

Result:

int:    f1(int)、f2(int)、f3(int)、f4(int)、f5(long)、f6(float)、f7(double)、

Same as above.

Comment out the first four, and describe the Fifth One:

Result:

short:  f1(short)、f2(short)、f3(short)、f4(int)、f5(long)、f6(float)、f7(double)、

In the first five annotations, comment the Sixth One:

Result:

long:   f1(long)、f2(long)、f3(long)、f4(long)、f5(long)、f6(float)、f7(double)、

Comment the first six items, and comment the seventh article:

Result:

float:  f1(float)、f2(float)、f3(float)、f4(float)、f5(float)、f6(float)、f7(double)、

For the first seven notes, comment the eighth one:

Result:

double: f1(double)、f2(double)、f3(double)、f4(double)、f5(double)、f6(double)、f7(double)、

 

2. When the real parameter type is greater than the shape parameter type, it must be converted forcibly.

1 package com. mianshi. easy; 2 3 public class Overload {4 5 void f1 (char x) {System. out. print ("f1 (char)" + ",") ;}; 6 void f1 (byte x) {System. out. print ("f1 (byte)" + ",") ;}; 7 void f1 (short x) {System. out. print ("f1 (short)" + ",") ;}; 8 void f1 (int x) {System. out. print ("f1 (int)" + ",") ;}; 9 void f1 (long x) {System. out. print ("f1 (long)" + ",") ;}; 10 void f1 (float x) {System. out. print ("f1 (float)" + ",") ;}; 11 void f1 (double x) {System. out. print ("f1 (double)" + ",") ;}; 12 13 void f2 (byte x) {System. out. print ("f2 (byte)" + ",") ;}; 14 void f2 (int x) {System. out. print ("f2 (int)" + ",") ;}; 15 void f2 (long x) {System. out. print ("f2 (long)" + ",") ;}; 16 void f2 (float x) {System. out. print ("f2 (float)" + ",") ;}; 17 void f2 (short x) {System. out. print ("f2 (short)" + ",") ;}; 18 void f2 (char x) {System. out. print ("f2 (char)" + ",") ;}; 19 20 void f3 (int x) {System. out. print ("f3 (int)" + ",") ;}; 21 void f3 (long x) {System. out. print ("f3 (long)" + ",") ;}; 22 void f3 (byte x) {System. out. print ("f3 (byte)" + ",") ;}; 23 void f3 (short x) {System. out. print ("f3 (short)" + ",") ;}; 24 void f3 (char x) {System. out. print ("f3 (char)" + ",") ;}; 25 26 void f4 (int x) {System. out. print ("f4 (int)" + ",") ;}; 27 void f4 (short x) {System. out. print ("f4 (short)" + ",") ;}; 28 void f4 (byte x) {System. out. print ("f4 (byte)" + ",") ;}; 29 void f4 (char x) {System. out. print ("f4 (char)" + ",") ;}; 30 31 void f5 (short x) {System. out. print ("f5 (short)" + ",") ;}; 32 void f5 (byte x) {System. out. print ("f5 (byte)" + ",") ;}; 33 void f5 (char x) {System. out. print ("f5 (char)" + ",") ;}; 34 35 void f6 (byte x) {System. out. print ("f6 (byte)" + ",") ;}; 36 void f6 (char x) {System. out. print ("f6 (char)" + ",") ;}; 37 38 void f7 (char x) {System. out. print ("f7 (char)" + ",") ;}; 39 40 void testDouble () {41 double x = 0; 42 System. out. print ("double:" + ""); 43 f1 (x); f2 (float) x); f3 (long) x); f4 (int) x); f5 (short) x); f6 (byte) x); f7 (char) x ); 44} 45 46 public static void main (String [] args) {47 48 Overload o = new Overload (); 49 50 o. testDouble (); 51} 52}View Code

Result:

double: f1(double)、f2(float)、f3(long)、f4(int)、f5(short)、f6(byte)、f7(char)、

This type of conversion is used to implement narrow conversions. Otherwise, the compiler may encounter errors.

 

4. Identify overload methods with return values

The following two methods:

Void f (){}

Int f () {return 1 ;}

As long as the compiler can clearly determine the semantics based on the context, for example, in int x = f ();, then it can indeed distinguish the overloaded method accordingly. However, sometimes you don't care about the return value of a method. What you want is other effects of a method call (this is often called "called for side effects "), in this case, you may call the method and ignore the returned value. Therefore, if you call the method as follows:

Fun ();

In this case, how can Java determine which f () is called? How do people understand this code? Therefore, it is impossible to distinguish between overloaded methods based on the return values of methods.

 

For more information, see Java programming ideas.

 

5. Two small online examples :(

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.