Философия Java3
Шрифт:
// initialization/OverloadingOrder.java // Перегрузка, основанная на порядке // следования аргументов import static net.mindview util Print.*;
public class OverloadingOrder {
static void f(String s, int i) {
print("String- " + s + ". int: " + i).
}
static void f(int i. String s) {
printCint. " + i + String: " + s):
}
public static void main(String[] args) { f("Сначала строка", 11); f(99. "Сначала число").
}
} /* Output
String
Два метода f имеют одинаковые аргументы с разным порядком следования, и это различие позволяет идентифицировать метод.
Перегрузка с примитивами
Простейший тип может быть автоматически приведен от меньшего типа к большему, и это в состоянии привнести немалую путаницу в перегрузку. Следующий пример показывает, что происходит при передаче примитивного типа перегруженному методу:
//: ini ti alizati on/Pri mi ti veOverloadi ng.java // Повышение примитивных типов и перегрузка, import static net mindview.util.Print.*;
public class PrimitiveOverloading {
void fl(char x) { printnb("fl(char)"); }'
void fKbyte x) { printnbCf l(byte)"). }
void fKshort x) { printnb("fl(short)"); }
void fl(int x) { printnbCfKint)"): }
void fKlong x) { printnD("fl(long)"); }
void fl(float x) { printnb("fl(float)"); }
void f1(double x) { printnb("fl(double)"); }
void f2(byte x) { printnb("f2(byte)"); }
void f2(short x) { printnb("f2(short)"'); }
void f2(int x) { printnb("f2(int)"); }
void f2(long x) { printnb("f2(long)"); }
void f2(float x) { printnb("f2(float)"); }
void f2(double x) { printnb("f2(double)"); }
void f3(short x) { printnb("f3(short)"); }
void f3(int x) { printnb("f3(int)")} void f3(long x) { printnb("f3(long)M); } void f3(float x) { printnb("f3(float)"); } void f3(double x) { printnb("f3(double)"); }
void f4(int x) { printnb("f4(int)"); } void f4(long x) { printnb("f4(long)"); } void f4(float x) { printnb("f4(float)"); } void f4(double x) { printnb("f4(double)"); }
void f5(long x) { printnb("f5(long)"); } void f5(float x) { printnb("f5(float)"); } void f5(double x) { printnb("f5(double)"); }
void f6(float x) { printnb("f6(float)"); } void f6(double x) { printnb("f6(double)"); }
void f7(double x) { printnb("f7(double)"); }
void testConstValО {
printnb("5: ");
fl(5);f2(5);f3(5);f4(5);f5(5).f6(5);f7(5);print;
}
void testCharO {
char x = 'x'; printnbC'char: ");
fl(x) ;f2(x) ;f3(x) ;f4(x) ;f5(x) ;f6(x) ;f7(x); print ;
}
void testByteO {
byte x = 0;
System.out.println("параметр типа byte:"); fl(x):f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testShortO {
short x = 0; printnb("short: ");
fl(x):f2(x):f3(x):f4(x);f5(x):f6(x);f7(x);print:
}
void testlntO {
int x = 0: printnbC'int: "):
fl(x) ;f2(x) ;f3(x) :f4(x) :f5(x) ;f6(x) ;f7(x); print :
}
void testLongO {
long x = 0; printnbC'long:");
fl(x):f2(x):f3(x):f4(x):f5(x):f6(x):f7(x);print;
}
void testFloatO {
float x = 0:
System.out.pri nt1n("f1 oat:");
fl(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);print;
}
void testDoubleO {
double x = 0: printnb("double:"):
fl(x) ;f2(x) ;f3(x) ;f4(x) ;f5(x) ;f6(x) ;f7(x) ;print;
}
public static void main(String[] args) { PrimitiveOverloading p =
/new PrimitiveOverloadingO; p.testConstValО. p.testCharO; p.testByteО; p testShortO; p.testlntO; p.testLongO; p testFloatO; p.testDoubleO;
}
} /* Output:
5: fl(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double) char: fl(char) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double) byte: fl(byte) f2(byte) f3(short) f4(int) f5(long) f6(float) f7(double) short: fl(short) f2(short) f3(short) f4(int) f5(long) f6(float) f7(double) int: fl(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double) long: fl(long) f2(long) f3(long) f4(long) f5(long) f6(float) f7(double) float: fl(float) f2(float) f3(float) f4(float) f5(float) f6(float) f7(double) double- fl(double) f2(double) f3(double) f4(double) f5(double) f6(double) f7(double) *///:-
Если
Что же произойдет, если ваш аргумент «больше», чем аргумент, требующийся в перегруженном методе? Ответ можно найти в модификации рассмотренной программы:
//: с04:Demotion.java
// Понижение примитивов и перегрузка.
import com.bruceeckel.simpletest.*;
public class Demotion {
static Test monitor = new TestO;
void fl(char x) { System.out.println("fl(char)"); }
void fKbyte x) { System out.println("fl(byte)"), }
void f 1(short x) { System.out.printlnC'fKshort)"); }