Download presentation
Presentation is loading. Please wait.
Published byMargery Patterson Modified over 9 years ago
1
L EC. 06: C LASS D ETAILS (2/2) 0
2
2015 S PRING C ONTENT Class method [review] Access control Passing arguments Method overloading Variable-length Arguments [ 不測驗,可列補充教材 ] Recursion Using the keyword static Nested classes Shadowing 1
3
P ASSING ARGUMENTS There are several ways to implement passing arguments from a caller to a invoked method, such as call-by-value and call-by-reference. Java only supports call-by-value. In Java, the precise effect differs between whether the argument type is a primitive type or a reference/class type. 2
4
C ALL - BY - VALUE APPROACH The call-by-value approach copies the value of an argument into the space of corresponding formal parameter. The argument is evaluated prior to the execution of invoked method. The invoked method does not know where arguments are. Changes made to the parameter of the method have no effect on the argument in the call. 3
5
E XAMPLE class Test { void noChange(int i, int j) { i = i + j; j = -j; } class CallByValue { public static void main(String args[]) { Test ob = new Test(); int a = 15, b = 20; System.out.println("a and b before call: " + a + " " + b); ob.noChange(a, b); System.out.println("a and b after call: " + a + " " + b); } 4 a and b before call: 15 20 a and b after call: 15 20 ob noChange(int i, int j)
6
E XERCISE 1 5 Create a stack class called Stack. Calls methods push() and pop() to access the stack. Keep all members of the Stack class private. 4 21 44 top [0] [1] [2] 21 4
7
Hint: class stack { private int stack_data[]; private int stack_top; stack(int size) { … } public int push(int data) { … } public int pop() { … } 6 Handle abnormal situation class Ex1 { public static void main(String[] args) { stack s1 = new stack(100); s1.push(4); s1.push(21); System.out.println(s1.pop()); }
8
P ASSING REFERENCE TYPE ARGUMENTS IN J AVA Since the content of a reference variable is the reference of an object, passing a reference type argument copies the reference of the object pointed by the argument to the memory of the corresponding parameter. Remember that the content of a reference variable is the reference of an object, instead of the actual object. The invoked method can access the object pointed by the argument through the parameter. Passing reference type arguments has similar effect to call-by-reference. 7
9
E XAMPLE class Test { int a, b; Test(int i, int j) { a = i; b = j; } void change(Test obj) { a = obj.a + obj.b; b = -obj.b; } class PassObjRef { public static void main(String args[]) { Test ob = new Test(15, 20); System.out.println("ob.a and ob.b before call: " + ob.a + " " + ob.b); ob.change(ob); System.out.println("ob.a and ob.b after call: " + ob.a + " " + ob.b); } 8 ob.a and ob.b before call: 15 20 ob.a and ob.b after call: 35 -20 ob a b change() 15 20 obj
10
M ETHOD OVERLOADING In Java, two or more methods within the same class can share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. When an overloaded method is called, the version of the method whose parameters match the arguments is executed. Numerical promotion and boxing may apply, but the automatic conversions apply only if there is no direct match between a parameter and an argument. 9
11
E XAMPLE class Overload { void ovlDemo() { System.out.println("No parameters"); } void ovlDemo(int a) { System.out.println("One parameter: " + a); } int ovlDemo(int a, int b) { System.out.println("Two parameters: " + a + " " + b); return a + b; } double ovlDemo(double a, double b) { System.out.println("Two double parameters: " + a + " "+ b); return a + b; } 10 No parameters One parameter: 2 Two parameters: 4 6 Result of ob.ovlDemo(4, 6): 10 Two double parameters: 1.1 2.32 Result of ob.ovlDemo(1.1, 2.2): 3.42
12
class OverloadDemo { public static void main(String args[]) { Overload ob = new Overload(); int resI; double resD; // call all versions of ovlDemo() ob.ovlDemo(); ob.ovlDemo(2); resI = ob.ovlDemo(4, 6); System.out.println("Result of ob.ovlDemo(4, 6): " + resI); resD = ob.ovlDemo(1.1, 2.32); System.out.println("Result of ob.ovlDemo(1.1, 2.2): " + resD); } 11
13
E XAMPLE class Overload2 { void f(int x) { System.out.println("Inside f(int): " + x); } void f(double x) { System.out.println("Inside f(double): " + x); } 12 Inside f(int): 10 Inside f(double): 10.1 Inside f(int): 99 Inside f(int): 10 Inside f(double): 11.5
14
class TypeConv { public static void main(String args[]) { Overload2 ob = new Overload2(); int i = 10; double d = 10.1; byte b = 99; short s = 10; float f = 11.5F; ob.f(i); // calls ob.f(int) ob.f(d); // calls ob.f(double) ob.f(b); // calls ob.f(int) type conversion ob.f(s); // calls ob.f(int) type conversion ob.f(f); // calls ob.f(double) type conversion } 13 byteshortintfloatdouble
15
E XAMPLE class MyClass { int x; MyClass() { System.out.println("Inside MyClass()."); x = 0; } MyClass(int i) { System.out.println("Inside MyClass(int)."); x = i; } MyClass(double d) { System.out.println("Inside MyClass(double)."); x = (int) d; } MyClass(int i, int j) { System.out.println("Inside MyClass(int, int)."); x = i * j; } 14 Inside MyClass(). Inside MyClass(int). Inside MyClass(double). Inside MyClass(int, int). t1.x: 0 t2.x: 88 t3.x: 17 t4.x: 8
16
class OverloadConsDemo { public static void main(String args[]) { MyClass t1 = new MyClass(); MyClass t2 = new MyClass(88); MyClass t3 = new MyClass(17.23); MyClass t4 = new MyClass(2, 4); System.out.println("t1.x: " + t1.x); System.out.println("t2.x: " + t2.x); System.out.println("t3.x: " + t3.x); System.out.println("t4.x: " + t4.x); } 15
17
B INDING FOR METHOD OVERLOADING Binding is the process for creating the association between data/code with an identifier. Static binding means that binding is done during compilation. Dynamic binding means that binding is done during execution. Binding for overloaded methods is static binding. 16
18
E XAMPLE class OverloadA { public static void main(String args[]) { OverloadA obj = new OverloadA(); obj.toDo(1); // C0 obj.toDo(2, 3); // C1 obj.toDo('a', 3); // C2 obj.toDo(2, 3, 4); // C3 obj.toDo(2, 3.2f); // C4 } void toDo(int i, int j) { // executed by C1 and C2 System.out.print("A"); } void toDo(int i, int... j) { // executed by C0 and C3 System.out.print("B"); } void toDo(double i, double j){ // executed by C4 System.out.print("C"); } 17 BAABC promotion
19
E XAMPLE class OverloadB { public static void main(String args[]) { OverloadB obj = new OverloadB(); obj.toDo(2, 3, 4); // ambiguous obj.toDo(3.2f); // cause error } void toDo(int i, int j, int... k) { System.out.print("A"); } void toDo(int i, int... j) { System.out.print("B"); } void toDo(Double i) { System.out.print("C"); } } 18
20
19 OverloadB.java:5: error: no suitable method found for toDo(float) obj.toDo(3.2f); ^ method OverloadB.toDo(int,int,int...) is not applicable (argument mismatch; possible lossy conversion from float to int) method OverloadB.toDo(int,int...) is not applicable (argument mismatch; possible lossy conversion from float to int) method OverloadB.toDo(Double) is not applicable (argument mismatch; float cannot be converted to Double) Note. It will not perform numerical promotion followed by boxing.
21
裝箱 ( BOXING ) 、拆箱 ( UNBOXING ) 20 把 primitive 轉成物件,稱之為裝箱 把物件轉乘 primitive ,稱之為拆箱 Ex. Integer A = new Integer(10); // 裝箱 int b = A.intValue(); // 拆箱 b = A; // 自動拆箱 A = 20; // 自動裝箱 A 10
22
class VarArgs { static void vaTest(int... v) { System.out.println("Number of args: " + v.length); for(int i=0; i < v.length; i++) System.out.println(v[i]); System.out.println(); } public static void main(String args[]) { vaTest(10); vaTest(1, 2, 3) vaTest(); } 21 Number of args: 1 10 Number of args: 3 1 2 3 Number of args: 0 V ARIABLE - LENGTH A RGUMENTS
23
E XAMPLE class VarArgs2 { static void vaTest(int msg, int... v) { System.out.println("-->" + v.length); for(int i=0; i < v.length; i++) System.out.println(v[i]); System.out.println(); } public static void main(String args[]) { vaTest(100); vaTest(100, 1, 2, 3); } 22 -->0 -->3 1 2 3 Need at least one parameter 2 hr
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.