方法重载条件:
1.必须是同一个类
2.方法名(也可以叫函数)一样
3.参数类型不一样或参数数量不一样
注:当然不能通过返回值来区分。
今天遇到一个有趣的现象,就是参数是基本类型的情况,我们都知道基本类型可以自动向上转型,来看看这种情况是怎么重载的。
public class Test {
void print(String str){System.out.println(str);}
void test1(char x){print("test1(char)");}
void test1(byte x){print("test1(byte)");}
void test1(short x){print("test1(short)");}
void test1(int x){print("test1(int)");}
void test1(long x){print("test1(long)");}
void test1(float x){print("test1(float)");}
void test1(double x){print("test1(double)");}
void test2(byte x){print("test2(char)");}
void test2(short x){print("test2(short)");}
void test2(int x){print("test2(int)");}
void test2(long x){print("test2(long)");}
void test2(float x){print("test2(float)");}
void test2(double x){print("test2(double)");}
void test3(short x){print("test3(short)");}
void test3(int x){print("test3(int)");}
void test3(long x){print("test3(long)");}
void test3(float x){print("test3(float)");}
void test3(double x){print("test3(double)");}
void test4(int x){print("test4(int)");}
void test4(long x){print("test4(long)");}
void test4(float x){print("test4(float)");}
void test4(double x){print("test4(double)");}
void test5(long x){print("test5(long)");}
void test5(float x){print("test5(float)");}
void test5(double x){print("test5(double)");}
void test6(float x){print("test6(float)");}
void test6(double x){print("test6(double)");}
void test7(double x){print("test7(double)");}
void testChar(){
print("char");
char x = 'x';
test1(x);test2(x);test3(x);test4(x);test5(x);test6(x);test7(x);
}
void testByte(){
print("Byte");
byte x = 0;
test1(x);test2(x);test3(x);test4(x);test5(x);test6(x);test7(x);
}
void testShort(){
print("Short");
short x = 0;
test1(x);test2(x);test3(x);test4(x);test5(x);test6(x);test7(x);
}
void testInt(){
print("Int");
int x = 0;
test1(x);test2(x);test3(x);test4(x);test5(x);test6(x);test7(x);
}
void testLong(){
print("Long");
long x = 0;
test1(x);test2(x);test3(x);test4(x);test5(x);test6(x);test7(x);
}
void testFloat(){
print("Float");
float x = 0;
test1(x);test2(x);test3(x);test4(x);test5(x);test6(x);test7(x);
}
void testDouble(){
print("Double");
double x = 0;
test1(x);test2(x);test3(x);test4(x);test5(x);test6(x);test7(x);
}
/**
* @param args
*/
public static void main(String[] args) {
Test test = new Test();
test.testChar();
test.testByte();
test.testShort();
test.testInt();
test.testLong();
test.testFloat();
test.testDouble();
}
}
Result:
char
test1(char)
test2(int)
test3(int)
test4(int)
test5(long)
test6(float)
test7(double)
Byte
test1(byte)
test2(char)
test3(short)
test4(int)
test5(long)
test6(float)
test7(double)
Short
test1(short)
test2(short)
test3(short)
test4(int)
test5(long)
test6(float)
test7(double)
Int
test1(int)
test2(int)
test3(int)
test4(int)
test5(long)
test6(float)
test7(double)
Long
test1(long)
test2(long)
test3(long)
test4(long)
test5(long)
test6(float)
test7(double)
Float
test1(float)
test2(float)
test3(float)
test4(float)
test5(float)
test6(float)
test7(double)
Double
test1(double)
test2(double)
test3(double)
test4(double)
test5(double)
test6(double)
test7(double)
如果传入的数据类型小于方法中声明的形式参数类型,实际数据类型就会被提升。char稍有不同,它会直接提升为int型。