• 沒有找到結果。

OOPClass,May24-25,2010 Hsuan-TienLin Generics

N/A
N/A
Protected

Academic year: 2022

Share "OOPClass,May24-25,2010 Hsuan-TienLin Generics"

Copied!
11
0
0

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

全文

(1)

Generics

Hsuan-Tien Lin

Department of CSIE, NTU

OOP Class, May 24-25, 2010

(2)

How can we write a class for an Integer set of arbitrary size?

H.-T. Lin (NTU CSIE) Generics OOP 05/24-25/2010 1 / 20

GPEWW -RXIKIV%VVE]_

-RXIKIV?A MEVV ! RI[ -RXIKIV? A MRX GSYRX !

ZSMH EHH -RXIKIV M _ MEVV?GSYRX A ! M a

a

(3)

How can we write a class a String set of arbitrary size?

GPEWW 7XVMRK%VVE]_

7XVMRK?A WEVV ! RI[ 7XVMRK? A MRX GSYRX !

ZSMH EHH 7XVMRK W _ WEVV?GSYRX A ! W a

a

GST] TEWXI WIEVGL VITPEGI

(4)

How can we write classes for Inte- ger/String/Double/Professor sets of arbi- trary size?

H.-T. Lin (NTU CSIE) Generics OOP 05/24-25/2010 3 / 20

GPEWW 3FNIGX%VVE]_

3FNIGX?A SEVV ! RI[ 3FNIGX? A MRX GSYRX !

ZSMH EHH 3FNIGX S _ SEVV?GSYRX A ! S a

a

(5)

How can we write one class for arbitrary

sets of arbitrary size?

(6)

Motivation of Generics (1/3)

1 c l a s s S t r i n g A r r a y {

2 p r i v a t e S t r i n g [ ] myarr ;

3 p u b l i c S t r i n g A r r a y (i n t l e n ) { myarr = new S t r i n g [ l e n ] ; }

4 p u b l i c S t r i n g g e t (i n t n ) { r e t u r n myarr [ n ] ; }

5 p u b l i c v o i d s e t (i n t n , S t r i n g s ) { myarr [ n ] = s ; }

6 p u b l i c v o i d s h o w A l l ( ) {

7 f o r(i n t i = 0 ; i <myarr . l e n g t h ; i ++)

8 System . o u t . p r i n t l n ( myarr [ i ] ) ;

9 }

10 }

11 c l a s s P r o f e s s o r A r r a y {

12 p r i v a t e P r o f e s s o r [ ] myarr ;

13 p u b l i c P r o f e s s o r A r r a y (i n t l e n ) { myarr = new P r o f e s s o r [ l e n ] ; }

14 p u b l i c P r o f e s s o r g e t (i n t n ) { r e t u r n myarr [ n ] ; }

15 p u b l i c v o i d s e t (i n t n , P r o f e s s o r p ) { myarr [ n ] = p ; }

16 p u b l i c v o i d s h o w A l l ( ) {

17 f o r(i n t i = 0 ; i <myarr . l e n g t h ; i ++)

18 System . o u t . p r i n t l n ( myarr [ i ] ) ;

19 }

20 }

Can we avoid writing the same boring things again and again?

H.-T. Lin (NTU CSIE) Generics OOP 05/24-25/2010 5 / 20

(7)

Motivation of Generics (2/3)

1 c l a s s O b j e c t A r r a y {

2 p r i v a t e O b j e c t [ ] myarr ;

3 p u b l i c O b j e c t A r r a y (i n t l e n ) { myarr = new O b j e c t [ l e n ] ; }

4 p r o t e c t e d O b j e c t g e t (i n t n ) { r e t u r n myarr [ n ] ; }

5 p r o t e c t e d v o i d s e t (i n t n , O b j e c t o ) { myarr [ n ] = o ; }

6 p u b l i c v o i d s h o w A l l ( ) {

7 f o r(i n t i = 0 ; i <myarr . l e n g t h ; i ++)

8 System . o u t . p r i n t l n ( myarr [ i ] ) ;

9 }

10 }

11

12 c l a s s S t r i n g A r r a y extends O b j e c t A r r a y {

13 p u b l i c S t r i n g A r r a y (i n t l e n ) { super( l e n ) ; }

14 p u b l i c S t r i n g g e t (i n t n ) { r e t u r n ( S t r i n g )super. g e t ( n ) ; }

15 p u b l i c v o i d s e t (i n t n , S t r i n g s ) { super. s e t ( n , s ) ; }

16 }

Yes, by inheritance and polynormphism—everything is an Object

(8)

Motivation of Generics (3/3)

1 c l a s s ANYArray {

2 p r i v a t e ANY [ ] myarr ;

3 p u b l i c ANYArray (i n t l e n ) { myarr = new ANY [ l e n ] ; }

4 p r o t e c t e d ANY g e t (i n t n ) { r e t u r n myarr [ n ] ; }

5 p r o t e c t e d v o i d s e t (i n t n , ANY o ) { myarr [ n ] = o ; }

6 p u b l i c v o i d s h o w A l l ( ) {

7 f o r(i n t i = 0 ; i <myarr . l e n g t h ; i ++)

8 System . o u t . p r i n t l n ( myarr [ i ] ) ;

9 }

10 }

Yes, by identifying the common parts, and then replacing sed ’s/ANY/String/’ ANYArray.java >

StringArray.java

H.-T. Lin (NTU CSIE) Generics OOP 05/24-25/2010 7 / 20

(9)

C++ Solution (roughly)

1 t e m p l a t e <c l a s s ANY>

2 c l a s s A r r a y {

3 p r i v a t e ANY [ ] myarr ;

4 p u b l i c A r r a y (i n t l e n ) { myarr = new ANY [ l e n ] ; }

5 p r o t e c t e d ANY g e t (i n t n ) { r e t u r n myarr [ n ] ; }

6 p r o t e c t e d v o i d s e t (i n t n , ANY o ) { myarr [ n ] = o ; }

7 p u b l i c v o i d s h o w A l l ( ) {

8 f o r(i n t i = 0 ; i <myarr . l e n g t h ; i ++)

9 System . o u t . p r i n t l n ( myarr [ i ] ) ;

10 }

11 }

12

13 {

14 Array < S t r i n g > s a r r ( 5 ) ;

15 s a r r . s e t ( 3 , " l a l a l a " ) ;

16 }

basically, the step sed ’s/ANY/String/’ ANYArray.cpp >

StringArray.cpp done by compiler

code automatically duplicates during complication as you use

(10)

Java Solution (roughly)

1 c l a s s Array <ANY> {

2 p r i v a t e ANY [ ] myarr ;

3 p u b l i c A r r a y (i n t l e n ) { myarr = (ANY [ ] ) (new O b j e c t [ l e n ] ) ; }

4 p r o t e c t e d ANY g e t (i n t n ) { r e t u r n myarr [ n ] ; }

5 p r o t e c t e d v o i d s e t (i n t n , ANY o ) { myarr [ n ] = o ; }

6 p u b l i c v o i d s h o w A l l ( ) {

7 f o r(i n t i = 0 ; i <myarr . l e n g t h ; i ++)

8 System . o u t . p r i n t l n ( myarr [ i ] ) ;

9 }

10 }

11

12 {

13 Array < S t r i n g > s a r r ( 5 ) ;

14 s a r r . s e t ( 3 , " l a l a l a " ) ;

15 }

the ANY → Object step is automatically done by compiler: a true one-class solution

H.-T. Lin (NTU CSIE) Generics OOP 05/24-25/2010 9 / 20

! RI[ %VVE] 7XVMRK"

(11)

How does duplicating solution compare

with one-class solution?

參考文獻

相關文件

check type information very strictly by compiler (as opposed to single-object polymorphism): ensure type safety in JVM. Note: type information erased

In this paper, we provide new decidability and undecidability results for classes of linear hybrid systems, and we show that some algorithms for the analysis of timed automata can

sequential search array (or linked list) selection sort array (or linked list) insertion sort linked list (or array) binary search ordered array polynomial merge sparse array on

class methods: exactly the same as class variables intention determined by directly using class names, or by reference TYPE.. run out of paper).. static (compile-time)

sequential search array (or linked list) selection sort array (or linked list) insertion sort linked list (or array) binary search ordered array polynomial merge sparse array on

check type information very strictly by compiler (as opposed to single-object polymorphism): ensure type safety in JVM. Note: type information erased

• check type information very strictly by compiler (as opposed to single-object polymorphism): ensure type safety in JVM. Note: type information erased

In this section, we describe how we can substan- tially reduce the time to instantiate a service on a nearby surrogate by storing its state on a portable storage device such as a