• 沒有找到結果。

OOPClass,May5,2009 Hsuan-TienLin Polymorphism

N/A
N/A
Protected

Academic year: 2022

Share "OOPClass,May5,2009 Hsuan-TienLin Polymorphism"

Copied!
28
0
0

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

全文

(1)

Polymorphism

Hsuan-Tien Lin

Deptartment of CSIE, NTU

OOP Class, May 5, 2009

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 0 / 29

(2)

Polymorphism: The Motto

One Thing, Many Shapes...

brainstorm: where do we encounter this motto in the real

world?

(3)

One Variable, Many Values

1

char a ;

2

s w i t c h ( a ) {

3

case 1 :

4

r e t u r n 1 ∗ 1 ;

5

case 2 :

6

r e t u r n 2 ∗ 2 ;

7

case 3 :

8

r e t u r n 3 ∗ 3 ;

9

. . .

10

case 127:

11

r e t u r n 127 ∗ 127;

12

}

better ways?

mechanism? raw memory interpretation

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 2 / 29

(4)

One Class, Many Instances

1

S t u d e n t s ;

2

i f ( s e q u a l s s t u d e n t 1 )

3

show ( score1 ) ;

4

e l s e i f ( s e q u a l s s t u d e n t 2 )

5

show ( score2 ) ;

6

. . .

7

e l s e i f ( s e q u a l s s t u d e n t 1 0 0 )

8

show ( score100 ) ;

better ways?

mechanism? abstraction

(5)

One Method Name, Many Parameter Lists

1

/ / no o v e r l o a d i n g

2

System . o u t . p r i n t S t r i n g ( " abc " ) ;

3

System . o u t . p r i n t I n t ( 3 ) ;

4

System . o u t . p r i n t D o u b l e ( 4 . 0 ) ;

5

/ / o v e r l o a d i n g

6

System . o u t . p r i n t ( " abc " ) ;

7

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

8

System . o u t . p r i n t ( 4 . 0 ) ;

mechanism? signature by name + parameter types

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 4 / 29

(6)

A Twist in Method Overloading

1

/ / o v e r l o a d i n g

2

System . o u t . p r i n t ( " abc " ) ;

3

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

4

System . o u t . p r i n t ( 4 . 0 ) ;

5

/ / a t w i s t ( o f course , n o t e x a c t l y workable

6

S t r i n g ( " abc " ) . p r i n t ( ) ;

7

I n t e g e r ( 3 ) . p r i n t ( ) ;

8

Double ( 4 . 0 ) . p r i n t ( ) ;

mechanism? just write print() for every class (we’ll see)

(7)

Polymorphic Behavior

all cases above: some polymorphic behavior BUT almost no one calls them real “polymorphism”

Why Not?

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 6 / 29

(8)

Polymorphic Behavior on Known Stuff

polymorphic behavior of known primitive-type variables

polymorphic behavior of known classes (extended types)

polymorphic behavior of known parameter types

(9)

Unknown Stuff: Future Extensions

Backward Compatibility inheritance hierarchy

Forward Advance

newly added variables/methods

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 8 / 29

(10)

Polymorphism of Object Content

one advanced content, many compatible ways to

access

(11)

Polymorphism of Object References

one compatible reference, many advanced contents to point to

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 10 / 29

(12)

Reference Upcast versus Reference Downcast

Upcast

simple (backward compatibility)

Downcast

need to check whether content fits (forward advance)

need RTTI (run-time type information/identification) to make

downcast work (where did we see it?)

(13)

Content/Reference Polymorphism

a possible mechanism: shared prefix

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 12 / 29

(14)

Content/Reference Polymorphism

but not the only mechanism: how about chains?

(15)

Content/Reference Polymorphism: Summary

backward compatibility handled

forward advance: only via downcast (RTTI)

simpler mechanism for forward advance?

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 14 / 29

(16)

Our Needs in Forward Advance

(17)

Our Needs in Forward Advance

new instance variables for advanced state

new instance methods to manipulate new variables give new meanings to existing instance variables give new meanings to existing instance methods

write an “updated but compatible” version of existing method

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 16 / 29

(18)

Method Overriding (Virtual Function)

calls the updated version through an

upper-level reference

(19)

Method Invocation Polymorphism

one method (via upper-level reference), many possible extended behaviors

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 18 / 29

(20)

Object.equals

1

c l a s s C o o r d i n a t e extends O b j e c t {

2

double x , y ;

3

4

b o o l e q u a l s ( O b j e c t o ) {

5

i f ( o i n s t a n c e o f C o o r d i n a t e ) {

6

C o o r d i n a t e c = ( C o o r d i n a t e ) o ;

7

r e t u r n ( c . x == t h i s . x && c . y == t h i s . y ) ;

8

}

9

r e t u r n f a l s e ;

10

}

11

}

(21)

Object.toString

1

C o o r d i n a t e c = new C o o r d i n a t e ( ) ;

2

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

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 20 / 29

(22)

Twist Revisited

1

System . o u t . p r i n t ( " abc " ) ;

2

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

3

System . o u t . p r i n t ( 4 . 0 ) ;

4

/ / a t w i s t ( o f course , n o t e x a c t l y workable

5

S t r i n g ( " abc " ) . p r i n t ( ) ;

6

I n t e g e r ( 3 ) . p r i n t ( ) ;

7

Double ( 4 . 0 ) . p r i n t ( ) ;

System.out.print(Object) can have polymorphic behavior by internally

calling the updated Object.print() (actually, Object.toString()) without

overloading

(23)

A Trip with java.lang.Object

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 22 / 29

(24)

A Trip with java.lang.String

(25)

V-Table: A Possible Mechanism

again, not the only mechanism

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 24 / 29

(26)

RTTI revisited

all we need is a link to the class area (stores name, vtable, etc.) where is the link? java.lang.Object

how to access? Object.getClass()

each area is an instance of java.lang.Class

(27)

An Advanced Use

How about creating an object with dynamic class name?!

see HW5

H.-T. Lin (NTU CSIE) Polymorphism OOP(even) 05/05/2009 26 / 29

(28)

Summary on Polymorphism

one thing, many shapes

important in strongly-typed platforms with inheritance

view from content: one advanced content with many compatible access

view from reference: one compatible reference can point to many advanced contents

view from method: one compatible method “contract”, many

different method “realization”

參考文獻

相關文件

For example, Liu, Zhang and Wang [5] extended a class of merit functions proposed in [6] to the SCCP, Kong, Tuncel and Xiu [7] studied the extension of the implicit Lagrangian

We show that, for the linear symmetric cone complementarity problem (SCLCP), both the EP merit functions and the implicit Lagrangian merit function are coercive if the underlying

– A finance charge will be levied if you fail to repay the outstanding balance of retail purchase or cash advances on the payment due date.. 

final instance variable: accessed through instance, and assigned once (in declaration or every instance constructor) final instance method: cannot be overriden (≈ assigned once)

drives, memory cards, USB flash drives, ExpressCard modules, cloud storage, CDs, DVDs, and Blu-ray Discs, tape, smart cards,. and microfilm and microfiche Internal hard

The one we saw earlier (p. 305) models the stock price minus the present value of the anticipated dividends as following geometric Brownian motion.. One can also model the stock

“involvement”, “media users’ behavior” and “age level” yield reciprocal influence on two causal models, “contents and types of webpage → advertising effectiveness”

A model of service quality perceptions and health care consumer behavior. Measurement and evaluation of satisfaction processes in