• 沒有找到結果。

More About Methods

N/A
N/A
Protected

Academic year: 2022

Share "More About Methods"

Copied!
25
0
0

加載中.... (立即查看全文)

全文

(1)

More About Methods

Hsuan-Tien Lin

Deptartment of CSIE, NTU

OOP Class, March 8-9, 2010

(2)

Method (1/2, Callee’s View)

1 p u b l i c c l a s s OOPStudent {

2 p u b l i c i n t s co re ;

3 p u b l i c S t r i n g name ;

4 p u b l i c v o i d s e t _ s c o r e (i n t new_score ) { sc or e = new_score ; }

5 p u b l i c S t r i n g get_name ( ) { r e t u r n name ; }

6 p u b l i c double g e t _ a d j u s t e d ( ) { r e t u r n (60 + sc or e ∗ 0 . 4 ) ; }

7 }

method: what I (the object) do parameter: what I get from the caller return value: what I want to tell the caller

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 1 / 24

(3)

Method (2/2, Caller’s View)

1 p u b l i c c l a s s OOPStudent {

2 p u b l i c i n t s co re ;

3 p u b l i c S t r i n g name ;

4 p u b l i c v o i d s e t _ s c o r e (i n t new_score ) { sc or e = new_score ; }

5 p u b l i c S t r i n g get_name ( ) { r e t u r n name ; }

6 p u b l i c double g e t _ a d j u s t e d ( ) { r e t u r n (60 + sc or e ∗ 0 . 4 ) ; }

7 }

1 p u b l i c c l a s s OOPStudentDemo {

2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {

3 OOPStudent C h a r l i e L = new OOPStudent ( ) ;

4 C h a r l i e L . s e t _ s c o r e ( 8 0 ) ;

5 System . o u t . p r i n t l n ( C h a r l i e L . g e t _ a d j u s t e d ( ) ) ;

6 System . o u t . p r i n t l n ( C h a r l i e L . get_name ( ) ) ;

7 }

8 }

method: what I (caller) want the object to do argument: what I tell the callee

return value: what I want to hear from the callee

(4)

Method: Key Point

method: an abstraction of action, where informa- tion is passed through argument/parameter and re- turn values

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 3 / 24

(5)

Return Values (1/1)

1 p u b l i c c l a s s OOPStudent {

2 p u b l i c i n t s co re ;

3 p u b l i c S t r i n g name ;

4 p u b l i c v o i d s e t _ s c o r e (i n t new_score ) { sc or e = new_score ; }

5 p u b l i c S t r i n g get_name ( ) { r e t u r n name ; }

6 p u b l i c double g e t _ a d j u s t e d ( ) { r e t u r n (60 + sc or e ∗ 0 . 4 ) ; }

7 }

1 p u b l i c c l a s s OOPStudentDemo {

2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {

3 OOPStudent C h a r l i e L = new OOPStudent ( ) ;

4 C h a r l i e L . s e t _ s c o r e ( 8 0 ) ;

5 System . o u t . p r i n t l n ( C h a r l i e L . g e t _ a d j u s t e d ( ) ) ;

6 System . o u t . p r i n t l n ( C h a r l i e L . get_name ( ) ) ;

7 }

8 }

void: must-have to mean no return value

primitive/extended return types possible

(6)

Return Values: Key Point

void for no return value

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 5 / 24

(7)

Primitive Argument/Parameter (1/1)

1 p u b l i c c l a s s Tool {

2 p u b l i c v o i d swap (i n t f i r s t , i n t second ) {

3 i n t tmp = f i r s t ;

4 f i r s t = second ;

5 second = tmp ;

6 System . o u t . p r i n t l n ( f i r s t ) ;

7 System . o u t . p r i n t l n ( second ) ;

8 }

9 }

10 p u b l i c c l a s s Demo{

11 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {

12 Tool t = new Tool ( ) ;

13 i n t i = 3 ; i n t j = 5 ;

14 t . swap ( i , j ) ;

15 System . o u t . p r i n t l n ( i ) ;

16 System . o u t . p r i n t l n ( j ) ;

17 }

18 }

first, second: swapped

i, j: didn’t

(8)

Primitive Argument/Parameter: Key Point

argument ⇒ parameter: call by value (copying) –change in parameter does not change argument

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 7 / 24

(9)

Extended Argument/Parameter

to be discussed in Chapter 5

(10)

this (1/1)

1 p u b l i c c l a s s OOPStudent {

2 p u b l i c S t r i n g name ;

3 p u b l i c S t r i n g get_name ( ) { r e t u r n name ; }

4 p u b l i c S t r i n g get_the_name ( OOPStudent WHO)

5 { r e t u r n WHO. name ; }

6 }

CharlieL.get_name() returns CharlieL.name Ptt.get_name() returns Ptt.name

how? a hidden parameter WHO can do

1 p u b l i c S t r i n g get_name ( ) { r e t u r n t h i s. name ; }

2 p u b l i c v o i d s e t _ s c o r e (i n t sc or e ) { t h i s. sc or e = sc or e ; }

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 9 / 24

(11)

this: Key Point

this: a hidden parameter in the method to keep in

touch with the instance

(12)

Local Variables (1/7)

1 p u b l i c c l a s s FibCompute {

2 p u b l i c i n t f i b (i n t n ) {

3 i n t r e s ;

4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System . o u t . p r i n t l n ( s ) ;

5 i f ( n <= 1 ) r e s = 1 ;

6 e l s e{ r e s = f i b ( n−1) ; r e s = r e s + f i b ( n−2) ; }

7 r e t u r n r e s ;

8 }

9 }

1 p u b l i c c l a s s FibDemo {

2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {

3 FibCompute f = new FibCompute ( ) ;

4 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;

5 }

6 }

a so-called recursive method

local primitive n: allocated, and assigned by argument ⇒ parameter

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 11 / 24

(13)

Local Variables (2/7)

1 p u b l i c c l a s s FibCompute {

2 p u b l i c i n t f i b (i n t n ) {

3 i n t r e s ;

4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System . o u t . p r i n t l n ( s ) ;

5 i f ( n <= 1 ) r e s = 1 ;

6 e l s e{ r e s = f i b ( n−1) ; r e s = r e s + f i b ( n−2) ; }

7 r e t u r n r e s ;

8 }

9 }

1 p u b l i c c l a s s FibDemo {

2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {

3 FibCompute f = new FibCompute ( ) ;

4 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;

5 }

6 }

local extended this: allocated, and assigned by argument (f) ⇒

parameter (this)

(14)

Local Variables (3/7)

1 p u b l i c c l a s s FibCompute {

2 p u b l i c i n t f i b (i n t n ) {

3 i n t r e s ;

4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System . o u t . p r i n t l n ( s ) ;

5 i f ( n <= 1 ) r e s = 1 ;

6 e l s e{ r e s = f i b ( n−1) ; r e s = r e s + f i b ( n−2) ; }

7 r e t u r n r e s ;

8 }

9 }

1 p u b l i c c l a s s FibDemo {

2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {

3 FibCompute f = new FibCompute ( ) ;

4 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;

5 }

6 }

local primitive res: allocated, not initialized assigned by ourselves

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 13 / 24

(15)

Local Variables (4/7)

1 p u b l i c c l a s s FibCompute {

2 p u b l i c i n t f i b (i n t n ) {

3 i n t r e s ;

4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System . o u t . p r i n t l n ( s ) ;

5 i f ( n <= 1 ) r e s = 1 ;

6 e l s e{ r e s = f i b ( n−1) ; r e s = r e s + f i b ( n−2) ; }

7 r e t u r n r e s ;

8 }

9 }

1 p u b l i c c l a s s FibDemo {

2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {

3 FibCompute f = new FibCompute ( ) ;

4 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;

5 }

6 }

local extended s: allocated, not initialized, links to a valid instance

by ourselves

(16)

Local Variables (5/7)

1 p u b l i c c l a s s FibCompute {

2 p u b l i c i n t f i b (i n t n ) {

3 i n t r e s ;

4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System . o u t . p r i n t l n ( s ) ;

5 i f ( n <= 1 ) r e s = 1 ;

6 e l s e{ r e s = f i b ( n−1) ; r e s = r e s + f i b ( n−2) ; }

7 r e t u r n r e s ;

8 }

9 }

1 p u b l i c c l a s s FibDemo {

2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {

3 FibCompute f = new FibCompute ( ) ;

4 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;

5 }

6 }

some other local variables generated by compiler: allocated, not initialized, used internally

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 15 / 24

(17)

Local Variables (6/7)

1 p u b l i c c l a s s FibCompute {

2 p u b l i c i n t f i b (i n t n ) {

3 i n t r e s ;

4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System . o u t . p r i n t l n ( s ) ;

5 i f ( n <= 1 ) r e s = 1 ;

6 e l s e{ r e s = f i b ( n−1) ; r e s = r e s + f i b ( n−2) ; }

7 r e t u r n r e s ;

8 }

9 }

1 p u b l i c c l a s s FibDemo {

2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {

3 FibCompute f = new FibCompute ( ) ;

4 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;

5 }

6 }

when call returns: the result is “copied” to the caller’s space-of-interest somewhere

local variables: discarded

(18)

Local Variables (7/7, courtesy of Prof. Chuen-Liang Chen )

local variable

instance variable

class(static) variable

b l t method

i t l

belong to method

invocation instance class declaration within method within class within class

difi i O O S

modifier static NO NO YES

allocation (when) method invocation

instance creation

class loadingg allocation (where) stack memory heap memory heap memory

initial to 0 NO YES YES

de-allocation method return

automatic garbage collection

NO

scope

usage range direct access range from

declaration to whole class whole class

Chuen-Liang Chen, NTU CS&IE / 5 OOP

declaration to end of block

whole class whole class

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 17 / 24

(19)

Local Variables: Key Point

local variables: the “status” of the current method frame

—by spec not necessarily initialized

(20)

Method Overloading (1/2)

1 c l a s s P r i n t e r {

2 v o i d p r i n t I n t e g e r (i n t a ) ;

3 v o i d p r i n t D o u b l e (double a ) ;

4 v o i d p r i n t S t r i n g ( S t r i n g a ) ;

5 }

“Integer” and “int” are basically saying the same thing lazy people don’t want to type so many words

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 19 / 24

(21)

Method Overloading (2/2)

1 c l a s s P r i n t e r {

2 v o i d p r i n t (i n t a ) ;

3 v o i d p r i n t (double a ) ;

4 i n t p r i n t ( S t r i n g a ) ;

5 v o i d p r i n t (i n t a , i n t b ) ;

6 }

7 c l a s s P r i n t e r T h a t C o m p i l e r S e e s {

8 v o i d p r i n t _ i n t (i n t a ) ;

9 v o i d p r i n t _ d o u b l e (double a ) ;

10 i n t p r i n t _ S t r i n g ( S t r i n g a ) ;

11 v o i d p r i n t _ i n t _ i n t (i n t a , i n t b ) ;

12 }

Java’s (and many modern language’s) solution: one method name, many possible argument types

called method overloading

Java “signature” of a method: include name and parameters (but

NO return types)

(22)

Method Overloading (2/2)

1 c l a s s P r i n t e r {

2 s t a t i c v o i d p r i n t (i n t a ) ;

3 s t a t i c i n t p r i n t (i n t a ) ;

4 s t a t i c double p r i n t (i n t a ) ;

5

6 s t a t i c v o i d main ( S t r i n g [ ] argv ) {

7 P r i n t e r . p r i n t ( P r i n t e r . p r i n t ( 3 ) + 5 ) ;

8 / / which one do you want t o c a l l ?

9 }

10 }

determine programmer’s intention from arguments: easy

determine programmer’s intention from return value: hard —can cast, can discard, etc.

Java “signature” of a method: include name and parameters only compiler’s job: from arguments (type), determine which method (name+parameters) to call

cannot have two methods with the same signature

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 21 / 24

(23)

Method Overloading: Key Point

method overloading: a compiler’s help by looking at

“signature” rather than “name” in calling

(24)

Operator Overloading

1 c l a s s Demo{

2 s t a t i c v o i d main ( S t r i n g [ ] argv ) {

3 i n t a = 5 ;

4 System . o u t . p r i n t l n ( 3 + a ) ; / / p l u s ( 3 , a )

5 System . o u t . p r i n t l n ( " " + a ) ; / / p l u s ( " " , a )

6 }

7 }

Java: a limited special case for String (actually, StringBuffer); the usual cases for primitive types; but not for other extended types C++: can overload almost “any” operator for any class

double-sided sword: powerful, but easily misused

H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 23 / 24

(25)

Operator Overloading: Key Point

operator overloading: very limited support in Java

(up to now, and possibly will be)

參考文獻

相關文件

The formation mechanism has been studied in this work through dynamic light scattering method which can get information about growth and distribution curve of particle size in

Once students are supported to grasp this concept, they become more willing to use English for self-expression and that in turn, is the finest form of empowerment!... What makes

The debate between Neo-Confucianists and Buddhists during the Song-Ming dynasties, in particular, the Buddhist counter-argument in retaliation of Neo-Confucianist criticism, is

Mie–Gr¨uneisen equa- tion of state (1), we want to use an Eulerian formulation of the equations as in the form described in (2), and to employ a state-of-the-art shock capturing

1 Generalized Extreme Value Distribution Let Y be a random variable having a generalized extreme- value (GEV) distribution with shape parameter ξ, loca- tion parameter µ and

Enhancing Creative Use of Language and Promoting Values Education through Reading across the Curriculum (RaC) in the Primary English Classroom (KS2). Project

Through an open and flexible curriculum framework, which consists of the Learning Targets, Learning Objectives, examples of learning activities, schemes of work, suggestions for

This formalism has still unknown parts about nonabelian extension of VPD, supersymmetry transformation of dual field, more simple action of high order g expansion with matter