package overloading.main;
//基本類型的重載
public class Main {
void f1(char x){
System.out.print("f1(char x) ");
}
void f1(byte x){
System.out.print("f1(byte x) ");
}
void f1(short x){
System.out.print("f1(short x) ");
}
void f1(int x){
System.out.print("f1(int x) ");
}
void f1(long x){
System.out.print("f1(long x) ");
}
void f1(float x){
System.out.print("f1(float x) ");
}
void f1(double x){
System.out.print("f1(double x) ");
}
void f2(byte x){
System.out.print("f1(byte x) ");
}
void f2(short x){
System.out.print("f1(short x) ");
}
void f2(int x){
System.out.print("f1(int x) ");
}
void f2(long x){
System.out.print("f1(long x) ");
}
void f2(float x){
System.out.print("f1(float x) ");
}
void f2(double x){
System.out.print("f1(double x) ");
}
void f3(short x){
System.out.print("f1(short x) ");
}
void f3(int x){
System.out.print("f1(int x) ");
}
void f3(long x){
System.out.print("f1(long x) ");
}
void f3(float x){
System.out.print("f1(float x) ");
}
void f3(double x){
System.out.print("f1(double x) ");
}
void f4(int x){
System.out.print("f1(int x) ");
}
void f4(long x){
System.out.print("f1(long x) ");
}
void f4(float x){
System.out.print("f1(float x) ");
}
void f4(double x){
System.out.print("f1(double x) ");
}
void f5(long x){
System.out.print("f1(long x) ");
}
void f5(float x){
System.out.print("f1(float x) ");
}
void f5(double x){
System.out.print("f1(double x) ");
}
void f6(float x){
System.out.print("f1(float x) ");
}
void f6(double x){
System.out.print("f1(double x) ");
}
void f7(double x){
System.out.print("f1(double x) ");
}
void testChar(){
System.out.println("testChar()...");
char c='a';
f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
System.out.println();
}
void testByte(){
System.out.println("testByte()...");
byte c=0;
f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
System.out.println();
}
void testShort(){
System.out.println("testShort()...");
short c=0;
f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
System.out.println();
}
void testInt(){
System.out.println("testInt()...");
int c=0;
f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
System.out.println();
}
void testLong(){
System.out.println("testLong()...");
long c=0L;
f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
System.out.println();
}
void testFloat(){
System.out.println("testFloat()...");
float c=0;
f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
System.out.println();
}
void testDouble(){
System.out.println("testDouble()...");
double c=0;
f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
System.out.println();
}
public static void main(String[] args) {
Main m=new Main();
m.testChar();
m.testByte();
m.testShort();
m.testInt();
m.testLong();
m.testFloat();
m.testDouble();
/* 輸出結果:
* testChar()...
* f1(char x) f1(int x) f1(int x) f1(int x) f1(long x) f1(float x) f1(double x)
* testByte()...
* f1(byte x) f1(byte x) f1(short x) f1(int x) f1(long x) f1(float x) f1(double x)
* testShort()...
* f1(short x) f1(short x) f1(short x) f1(int x) f1(long x) f1(float x) f1(double x)
* testInt()...
* f1(int x) f1(int x) f1(int x) f1(int x) f1(long x) f1(float x) f1(double x)
* testLong()...
* f1(long x) f1(long x) f1(long x) f1(long x) f1(long x) f1(float x) f1(double x)
* testFloat()...
* f1(float x) f1(float x) f1(float x) f1(float x) f1(float x) f1(float x) f1(double x)
* testDouble()...
* f1(double x) f1(double x) f1(double x) f1(double x) f1(double x) f1(double x) f1(double x)
*/
}
}
注:基本類型重載可以看成是佔用內存大的類型是佔用內存小的類型的父類,在重載的時候默認自動向上造型至離自己最近的父類(下面代碼驗證向上造型調用離自己最近的父類型參數方法),
package overloading.main;
public class SubAndParent {
/*
* 該類有兩個重載方法overloading,分別含參Integer和Object,
* 其中Object是Integer的父類,當輸入參數爲Integer時會調用參
* 數類型爲Integer的方法,當輸入參數類型爲Object時,會調用參數
* 類型爲Object的方法,從而驗證編譯器會默認調用離自己最近的父類或者
* 同類型參數的方法。
*/
public void overloading(Integer intg) {
System.out.println("overloading(Integer intg)");
}
public void overloading(Object obj) {
System.out.println("overloading(Object obj)");
}
public static void main(String[] args) {
SubAndParent sp = new SubAndParent();
sp.overloading(1);
sp.overloading(new Integer(1));
sp.overloading((Object) (new Integer(1)));
/*
* Output:
* overloading(Integer intg)
* overloading(Integer intg)
* overloading(Object obj)
*/
}
}
其中,char有點區別,如果不存在char類型重載方法,則char自動轉換爲int再遵守基本類型的向上造型原則(見testChar()用例)。