l ec. 06: c lass d etails (2/2) 0. 2015 s pring c ontent class method [review] access control ...

23
LEC. 06: CLASS DETAILS (2/2) 1

Upload: margery-patterson

Post on 29-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

1

LEC. 06: CLASS DETAILS (2/2)

Page 2: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

2

2015 SPRING CONTENT

Class method [review] Access control Passing arguments Method overloading Variable-length Arguments [不測驗,可列補充教材 ] Recursion Using the keyword static Nested classes

Shadowing

Page 3: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

3

PASSING 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.

Page 4: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

4

CALL-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.

Page 5: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

5

EXAMPLEclass 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); } } a and b before call: 15 20

a and b after call: 15 20

obnoChange(int i, int j)

Page 6: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

6

EXERCISE 1

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

4 4top

top

top

top

top

[0]

[1]

[2]

21 4

Page 7: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

7

Hint:

class stack{

private int stack_data[];private int stack_top;

stack(int size){

…}

public int push(int data){

…}public int pop(){

…}

} 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());System.out.println(s1.pop());

}}

Page 8: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

8

PASSING REFERENCE TYPE ARGUMENTS IN JAVA

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.

Page 9: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

9

EXAMPLEclass 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); } } ob.a and ob.b before call: 15 20

ob.a and ob.b after call: 35 -20

ob ab

change()

15

20

obj

Page 10: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

10

METHOD 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.

Page 11: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

11

EXAMPLEclass 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; } }

No parametersOne parameter: 2Two parameters: 4 6Result of ob.ovlDemo(4, 6): 10Two double parameters: 1.1 2.32Result of ob.ovlDemo(1.1, 2.2): 3.42

Page 12: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

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);

} }

Page 13: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

13

EXAMPLEclass Overload2 { void f(int x) { System.out.println("Inside f(int): " + x); } void f(double x) { System.out.println("Inside f(double): " + x); } }

Inside f(int): 10Inside f(double): 10.1Inside f(int): 99Inside f(int): 10Inside f(double): 11.5

Page 14: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

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 } }

byte short int float double

Page 15: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

15

EXAMPLEclass 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; } }

Inside MyClass().Inside MyClass(int).Inside MyClass(double).Inside MyClass(int, int).t1.x: 0t2.x: 88t3.x: 17t4.x: 8

Page 16: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

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); } }

Page 17: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

17

BINDING 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.

Page 18: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

18

EXAMPLEclass 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"); }}

BAABC

promotion

Page 19: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

19

EXAMPLEclass 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"); }}

Page 20: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

20

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.

Page 21: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

21

裝箱 (BOXING) 、拆箱 (UNBOXING)

把 primitive轉成物件,稱之為裝箱把物件轉乘 primitive,稱之為拆箱 Ex.Integer A = new Integer(10);   // 裝箱int b = A.intValue();    // 拆箱b = A;     // 自動拆箱A = 20;     // 自動裝箱

A10

Page 22: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

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(); } }

Number of args: 110

Number of args: 3123

Number of args: 0

VARIABLE-LENGTH ARGUMENTS

Page 23: L EC. 06: C LASS D ETAILS (2/2) 0. 2015 S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length

23

EXAMPLEclass 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); } }

-->0

-->3123

Need at least one parameter

2 hr