Java Tutorial/Class Definition/Method Overloading
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
- 1 Demonstration of both constructor and ordinary method overloading
- 2 Method Overloading
- 3 Methods with differing type signatures are overloaded - not overridden.
- 4 Overloading based on the order of the arguments
- 5 Pass long parameters to overloading method
- 6 Primitives and overloading
- 7 Using Method Overloading
- 8 Using overloaded methods to print array of different types
Demonstration of both constructor and ordinary method overloading
class MyClass {
int height;
MyClass() {
System.out.println("Planting a seedling");
height = 0;
}
MyClass(int i) {
System.out.println("Creating new Tree that is " + i + " feet tall");
height = i;
}
void info() {
System.out.println("Tree is " + height + " feet tall");
}
void info(String s) {
System.out.println(s + ": Tree is " + height + " feet tall");
}
}
public class MainClass {
public static void main(String[] args) {
MyClass t = new MyClass(0);
t.info();
t.info("overloaded method");
// Overloaded constructor:
new MyClass();
}
}
Creating new Tree that is 0 feet tall Tree is 0 feet tall overloaded method: Tree is 0 feet tall Planting a seedling
Method Overloading
Java allows you to have multiple methods having the same name, as long as each method accept different sets of argument types. In other words, in our example, it is legal to have these two methods in the same class.
public String printString(String string)
public String printString(String string, int offset)
Methods with differing type signatures are overloaded - not overridden.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show(String msg) {
System.out.println(msg + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show("This is k: "); // this calls show() in B
subOb.show(); // this calls show() in A
}
}
Overloading based on the order of the arguments
public class MainClass {
static void print(String s, int i) {
System.out.println("String: " + s + ", int: " + i);
}
static void print(int i, String s) {
System.out.println("int: " + i + ", String: " + s);
}
public static void main(String[] args) {
print("String first", 11);
print(99, "Int first");
}
}
String: String first, int: 11 int: 99, String: Int first
Pass long parameters to overloading method
It is legal to have these two methods in the same class.
public class MainClass {
public int printNumber(int i) {
return i*2;
}
public long printNumber(long i) {
return i*3;
}
public static void main(String[] args) {
}
}
Primitives and overloading
public class MainClass {
void f1(char x) {
System.out.println("f1(char)");
}
void f1(byte x) {
System.out.println("f1(byte)");
}
void f1(short x) {
System.out.println("f1(short)");
}
void f1(int x) {
System.out.println("f1(int)");
}
void f1(long x) {
System.out.println("f1(long)");
}
void f1(float x) {
System.out.println("f1(float)");
}
void f1(double x) {
System.out.println("f1(double)");
}
void f2(char x) {
System.out.println("f2(char)");
}
void f2(byte x) {
System.out.println("f2(byte)");
}
void f2(short x) {
System.out.println("f2(short)");
}
void f2(int x) {
System.out.println("f2(int)");
}
void f2(long x) {
System.out.println("f2(long)");
}
void f2(float x) {
System.out.println("f2(float)");
}
void f3(char x) {
System.out.println("f3(char)");
}
void f3(byte x) {
System.out.println("f3(byte)");
}
void f3(short x) {
System.out.println("f3(short)");
}
void f3(int x) {
System.out.println("f3(int)");
}
void f3(long x) {
System.out.println("f3(long)");
}
void f4(char x) {
System.out.println("f4(char)");
}
void f4(byte x) {
System.out.println("f4(byte)");
}
void f4(short x) {
System.out.println("f4(short)");
}
void f4(int x) {
System.out.println("f4(int)");
}
void f5(char x) {
System.out.println("f5(char)");
}
void f5(byte x) {
System.out.println("f5(byte)");
}
void f5(short x) {
System.out.println("f5(short)");
}
void f6(char x) {
System.out.println("f6(char)");
}
void f6(byte x) {
System.out.println("f6(byte)");
}
void f7(char x) {
System.out.println("f7(char)");
}
void testDouble() {
double x = 0;
System.out.println("double argument:");
f1(x);
f2((float) x);
f3((long) x);
f4((int) x);
f5((short) x);
f6((byte) x);
f7((char) x);
}
public static void main(String[] args) {
MainClass p = new MainClass();
p.testDouble();
}
}
double argument: f1(double) f2(float) f3(long) f4(int) f5(short) f6(byte) f7(char)
Using Method Overloading
A method"s name with the types and sequence of the parameters form the method"s signature
public class MainClass {
public void print(int a) {
System.out.println(a);
}
public void print(String a) {
System.out.println(a);
}
}
Using overloaded methods to print array of different types
public class MainClass {
// method printArray to print Integer array
public static void printArray(Integer[] inputArray) {
// display array elements
for (Integer element : inputArray)
System.out.printf("%s ", element);
System.out.println();
}
// method printArray to print Double array
public static void printArray(Double[] inputArray) {
// display array elements
for (Double element : inputArray)
System.out.printf("%s ", element);
System.out.println();
}
// method printArray to print Character array
public static void printArray(Character[] inputArray) {
// display array elements
for (Character element : inputArray)
System.out.printf("%s ", element);
System.out.println();
}
public static void main(String args[]) {
// create arrays of Integer, Double and Character
Integer[] integerArray = { 1, 2, 3, 4, 5, 6 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
Character[] characterArray = { "H", "E", "L", "L", "O" };
System.out.println("Array integerArray contains:");
printArray(integerArray); // pass an Integer array
System.out.println("\nArray doubleArray contains:");
printArray(doubleArray); // pass a Double array
System.out.println("\nArray characterArray contains:");
printArray(characterArray); // pass a Character array
}
}
Array integerArray contains: 1 2 3 4 5 6 Array doubleArray contains: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 Array characterArray contains: H E L L O