cours-genericite
9 pages
Français
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
9 pages
Français
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

La genericite en Java (suite)F. Barthelemy14 mars 20061 Rappel : exemple simpleclass Listefboolean estVide ()freturn true ;gListe suite ()throws ListeVideException fthrow ListeVideException . exc ;gT tete ( ) throws ListeVideException fthrow ListeVideException . exc ;ggclass ListeNonVide extends ListefT premier ;Liste suivant ;ListeNonVide(T p , Liste s )fpremier = p ;suivant = s ;gboolean estVide ()freturn false ;gListe suite ( ) throws ListeVideException freturn suivant ;gT tete ( ) throws ListeVideException freturn premier ;ggclass ListeVideException extends Exceptionfstatic ListeVideException exc =new ListeVideException ( ) ;g1class Autrefstatic int longueur ( Liste l )throws ListeVideException fint res = 0;while ( ! l . estVide ())fres++;l = l . suite ( ) ;greturn res ;gpublic static void main( String [ ] args )throws ListeVideException fListe l a l = new Liste();l a l = new ListeNonVide(5, l a l ) ;l a l = new ListeNonVide(7, l a l ) ;System . out . println ( longueur ( l a l ) ) ;ggclass TesteListefpublic static void main( String [ ] args )throws ListeVideException fListe l s = new Liste();l s = new ListeNonVide(" art " , l s ) ;l s = new ListeNonVide("gosh" , l s ) ;Liste l t = new Liste();l t = new ListeNonVide("jam" , l t ) ;l t = new ListeNonVide( l s . tete () , l t ...

Informations

Publié par
Nombre de lectures 71
Langue Français

Extrait

1
La genericite en Java (suite)
F.Barthelemy
14mars2006
Rappel : exemple simple
c l a s sL i s t e<T>{ booleane s t V i d e ( ){ return true; } L i s t e<T>s u i t e ( ) throwsL i s t e V i d e E x c e p t i o n{ throwx c . e L i s t e V i d e E x c e p t i o n ; } T t e t e ( )throwsL i s t e V i d e E x c e p t i o n{ throwL i s t e V i d e E x c e p t i o n x c . e ; } } c l a s sL i s t e N o n V i d e<T>extendsL i s t e<T>{ T p r e m i e r ; L i s t e<T>;s u i v a n t L i s t e N o n V i d e (T p , L i s t e<T>s ){ p r e m i e r = p ; s u i v a n t = s ; } booleane s t V i d e ( ){ return f a l s e; } L i s t e<T>( )s u i t e throwsL i s t e V i d e E x c e p t i o n{ return;s u i v a n t } T t e t e ( )throwsL i s t e V i d e E x c e p t i o n{ return;p r e m i e r } } c l a s sL i s t e V i d e E x c e p t i o nextendsE x c e p t i o n{ s t a t i ce x c L i s t e V i d e E x c e p t i o n = newL i s t e V i d e E x c e p t i o n ( ) ; }
1
c l a s sAutre{ s t a t i c i n tl o n g u e u r ( L i s t e<?>l ) throwsL i s t e V i d e E x c e p t i o n{ i n t= 0 r e s ; while( ) )l . e s t V i d e ( ! { r e s ++; l = l . s u i t e ( ) ; } return;r e s } public s t a t i c voidmain ( S t r i n g [ ] a r g s ) throwsL i s t e V i d e E x c e p t i o n{ L i s t e<I n t e g e r>=l a l newL i s t e<I n t e g e r>( ) ; l a l =newL i s t e N o n V i d e<I n t e g e r>l a l (5 , ) ; l a l =newL i s t e N o n V i d e<I n t e g e r>) ;l a l (7 , System . o u t . p r i n t l n ( l o n g u e u r ( l a l ) ) ; } } c l a s sT e s t e L i s t e{ public s t a t i c voidmain ( S t r i n g a r g s [ ] ) throwsL i s t e V i d e E x c e p t i o n{ L i s t e<S t r i n g>=l s newL i s t e<S t r i n g>( ) ; l s =newL i s t e N o n V i d e<S t r i n g>l s ) ;(” a r t ” , l s =newL i s t e N o n V i d e<S t r i n g>;s ) , l (” g o s h ” L i s t e<S t r i n g>l t =newL i s t e<S t r i n g>( ) ; l t =newL i s t e N o n V i d e<S t r i n g>;t ) , l (” jam” l t =newL i s t e N o n V i d e<S t r i n g>( l s e t e . t , l ( ) ;t ) l s = l s . s u i t e ( ) ; } }
2
Securite apportee par la genericite
Premiereversion:utilisationgeneriquedelaclassegeneriqueListe. c l a s sE r r e u r T y p a g e{ public s t a t i c void)main ( S t r i n g a r g s [ ] { L i s t e<S t r i n g>l =newL i s t e<S t r i n g>( ) ; l =newL i s t e N o n V i d e<S t r i n g>, l ) ;(” o u i ” l =newL i s t e N o n V i d e<S t r i n g>(newI n t e g e r ( 4 ) , l ) ; } } / >j a v a c . j L i s t e a v a L i s t e . j a v a : 3 8 : c a n n o t f i n d s y m b o l s y m b o l : c o n s t r u c t o r L i s t e N o n V i d e ( j a v a . l a n g . I n t e g e r ,
2
L i s t e<t r i n gj a v a . l a n g . S >) l o c a t i o n : c l a s s L i s t e N o n V i d e<j a v a a n g . l t r i n g. S > l = new L i s t e N o n V i d e<S t r i n g>(new I n t e g e r ( 4 ) , l ) ; ˆ 1 e r r o r /
Etsilonutiliselaclassesansspeci erleparametregenerique,lecodesecompile,maisil y a un avertissement. c l a s sE r r e u r T y p a g e B i s{ public s t a t i c void)[ ] a r g s main ( S t r i n g { L i s t e l =new( ) ;L i s t e l =newL i s t e N o n V i d e ( ” o u i ” , l ) ; l =newL i s t e N o n V i d e (new( 4 ) I n t e g e r ;, l ) } } >L i s t e . j j a v a c a v a Note : L i s t e . j a v a u s e s u n c h e c k e d o r u n s a f e o p e r a t i o n s . Note : R e c o m p i l e w i t h: u X l i n t n c h e c k e df o r.d e t a i l s >j a v a c. j a v an c h e c k e d L i s t e X l i n t : u L i s t e . j a v a : 4 4 : w a r n i n g : [ u n c h e c k e d ] u n c h e c k e d c a l l t o L i s t e N o n V i d e (T, L i s t e<T>raw t y p e L i s t e N o n V i d e) a s a member o f t h e l =newL , l ) i s t e N o n V i d e ( ” o u i ” ; ˆ L i s t e . j a v a : 4 5 : w a r n i n g : [ u n c h e c k e d ] u n c h e c k e d c a l l t o L i s t e N o n V i d e (T, L i s t e<T>) l =newL i s t e N o n V i d e (new;, l ) ( 4 ) I n t e g e r ˆ 2 w a r n i n g s
3
Exemples de genericite dans la librairie
Voici des exemples issus de la librairie. public i n t e r f a c eComparable<T>{ i n to ) ;compareTo (T } public i n t e r f a c eI t e r a b l e<T>{ I t e r a t o r<T>( ) ;i t e r a t o r } public i n t e r f a c eI t e r a t o r<E>{ booleanhasNext ( ) ; E n e x t ( ) ; voidremove ( ) ; } public f i n a l c l a s sI n t e g e rextendsNumber implementsComparable<I n t e g e r>{
3
. . . public s t a t i c i n tb i t C o u n t (i n t;i ) public i n ta n o t h e r I n t e g e r ) ;compareTo ( I n t e g e r . . . } public c l a s sV e c t o r<E>extendsA b s t r a c t L i s t<E> implementsL i s t<E>. ., . { . . . public( C o l l e c t i o nV e c t o r <?extendsE>c ) ; . . . public booleano ) ;add (E public booleano ) ;remove ( O b j e c t public( ) ;E f i r s t E l e m e n t public booleanC o l l e c t i o nc o n t a i n s A l l ( <?>c ) ; . . . }
4
Des exemples compliques
importj a v a .. u t i l ; c l a s sUnPeuComplique<BA , extendsV e c t o r<A>>{ B v a r ; UnPeuComplique (B b ){ v a r = b ; } void( )v i d e { v a r . c l e a r ( ) ; } public s t a t i c void)a r g s [ ] main ( S t r i n g { V e c t o r<I n t e g e r>v =newV e c t o r<I n t e g e r>( ) ; v . add ( 4 ) ; (newUnPeuComplique<, V e c t o rI n t e g e r <I n t e g e r>>(v ) ) . v i d e ( ) ; } } c l a s sComplique<AextendsComparable<A>,BextendsV e c t o r<A>>{ voidm(B l e b , A l e a ){ i f. compareTo ( l e b . e a ( l f i r s t E l e m e n t ())==0){ System . o u t . p r i n t l n ( ” I l s s o n t egaux ” ) ; } } public s t a t i c voida r g s )main ( S t r i n g [ ] { V e c t o r<I n t e g e r>v =newV e c t o r<I n t e g e r>( ) ; v . add ( 4 ) ; (newComplique<, V I n t e g e r e c t o r<I n t e g e r> >()).m( v , 4 ) ; }
4
}
5
Ce qui ne marche pas
Onnepeutpasinstancierunobjetouuntableauavecunparametredetype. Onnepeutpasnonplusutiliserunparametredetypedansunemethodeouunevariable statique. c l a s sTableau<T>{ T [ ] t ; T v ; s t a t i c;T v2 voidm( ){ t =newT [ 1 0 ] ; v =newT ( ) ; } s t a t i cT n ( ){ return n u l l; } }
/ >j a v a c TabGen . j a v a TabGen . j a v a : 4 : nonb e r e f e r e n c e d T c a n n o t s t a t i c c l a s s c o n t e x tt a t i c from a s s t a t i c T v2 ; ˆ TabGen . j a v a : 9 : nons t a t i c c l a s s T c t a t i c c o n t e x ta n n o t b e r e f e r e n c e d from a s s t a t i c T n ( ){ ˆ TabGen . j a v a : 6 : g e n e r i c a r r a y c r e a t i o n t = new T [ 1 0 ] ; ˆ TabGen . j a v a : 7 : u n e x p e c t e d t y p e f o u n d : t y p e p a r a m e t e r T r e q u i r e d : c l a s s v = new T ( ) ; ˆ 4 e r r o r s /
Lasolution:passerlesobjetsettableauxenparametreauconstructeuroufairedes contorsions. Autres choses qui ne marchent pas : les tests dynamiques. Par exempleinstanceofet le cast. importj a v a . u t i l .; c l a s sMarchePas<T>{
5
T l a m e t h ( O b j e c t o ){ return( T ) o ; } method m( V e c t o r v ){ i f( vi n s t a n c e o fV e c t o r<S t r i n g>){ System . o u t . p r i n t l n ( ”C ’ en e s t un” ) ; } } public s t a t i c void)main ( S t r i n g a r g s [ ] { V e c t o r v =newV e c t o r ( ) ; I t e r a b l e<S t r i n g>t e r a b l e( I t = <S t r i n g>;) v } } / >j a v a cX l i n t : u n c h e c k e d MarchePas . j a v a MarchePas . j a v a : 6 : c a n n o t f i n d s y m b o l s y m b o l : c l a s s method l o c a t i o n : c l a s s MarchePas<T> method m( V e c t o r v ){ ˆ MarchePas . j a v a : 4 : w a r n i n g : [ u n c h e c k e d ] u n c h e c k e d c a s t f o u n d : j a v a . l a n g . O b j e c t r e q u i r e d : T r e t u r n ( T ) o ; ˆ MarchePas . j a v a : 7 : i l l e g a l g e n e r i c t y p e f o r i n s t a n c e o f i f ( v i n s t a n c e o f V e c t o r<S t r i n g>){ ˆ MarchePas . j a v a : 1 3 : w a r n i n g : [ u n c h e c k e d ] u n c h e c k e d c a s t f o u n d : j a v a . u t i l . V e c t o r r e q u i r e d : j a v a . l a n g . I t e r a b l e<. l a n g . S t r i n gj a v a > I t e r a b l e<S t r i n g>( I t = t e r a b l e<S t r i n g>) v ; ˆ 2 e r r o r s 2 w a r n i n g s /
6
Une chose surprenante
Quandondonneplusieursbornessuperieuresaunparametredetype,onnepeutpas toujoursutilisertouteslesoperationsdecesbornessuperieures. i n t e r f a c eL i s i b l e{ voidl i t ( ) ; } i n t e r f a c eE x t e n s i b l e{
6
voide t e n d ( ) ; } abstract c l a s sB l a b l a{ abstract voidc a u s e ( ) ; abstract i n td e g r e ( ) ; abstractB l a b l a l e P l u s B e a u ( B l a b l a b ) ; } c l a s sUneBorne<TextendsB l a b l a>{ voida r a )m(T p { p a r a . c a u s e ( ) ; p a r a . l e P l u s B e a u (n u l l) ; } } c l a s sDeuxBornes<Textends& L B l a b l a i s i b l e>{ voida r a )m(T p { p a r a . l i t ( ) ; p a r a . c a u s e ( ) ; p a r a . l e P l u s B e a u ( p a r a ) ; } } c l a s sDeuxBornesBis<Textends& L i s i b l eB l a b l a voidm(T p a r a ){ p a r a . l i t ( ) ; p a r a . e t e n d ( ) ; } } / >j a v a c DeuxBornes . j a v a DeuxBornes . j a v a : 2 1 : c a n n o t f i n d s y m b o l s y m b o l : method c a u s e ( ) p a r a . c a u s e ( ) ; ˆ DeuxBornes . j a v a : 2 2 : c a n n o t f i n d s y m b o l s y m b o l : method l e P l u s B e a u (T) p a r a . l e P l u s B e a u ( p a r a ) ; ˆ 2 e r r o r s /
& E x t e n s i b l e>{
Voyonslescodesdecompilesdecesclasses: // D e c o m p i l e d b y Jad v1 . 5 . 8 e . C o p y r i g h t 2 0 0 1 P a v e l K o u z n e t s o v . // Jad home p a g e : h t t p : / /www . g e o c i t i e s . com/ k p d u s / j a d . h t m l // D e c o m p i l e r o p t i o n s : p a c k i m p o r t s ( 3 ) // S o u r c e F i l e Name : DeuxBornes . j a v a
c l a s s
UneBorne
7
{
} // // // //
UneBorne ( ) { }
void)b l a b l a l a b l a m( B { b l a b l a . c a u s e ( ) ; b l a b l a . l e P l u s B e a u (n u l l) ; }
D e c o m p i l e d b y Jad v1 . 5 . 8 e . C o p y r i g h t 2 0 0 1 P a v e l K o u z n e t s o v . Jad home p a g e : h t t p : / /www . g e o c i t i e s . com/ k p d u s / j a d . h t m l D e c o m p i l e r o p t i o n s : p a c k i m p o r t s ( 3 ) S o u r c e F i l e Name : DeuxBornes . j a v a
c l a s s {
} // // // //
DeuxBornes
DeuxBornes ( ) { }
void)b l a b l a l a b l a m( B { ( ( L i s i b l e ) b l a b l a ) . l i t ( ) ; }
D e c o m p i l e d b y Jad v1 . 5 . 8 e . C o p y r i g h t 2 0 0 1 P a v e l K o u z n e t s o v . Jad home p a g e : h t t p : / /www . g e o c i t i e s . com/ k p d u s / j a d . h t m l D e c o m p i l e r o p t i o n s : p a c k i m p o r t s ( 3 ) S o u r c e F i l e Name : DeuxBornes . j a v a
c l a s s {
DeuxBornesBis
DeuxBornesBis ( ) { }
voidm( L )l i s i b l e i s i b l e { l i s i b l e . l i t ( ) ; ( ( E x t e n s i b l e ) l i s i b l e ) . e t e n d ( ) ;
8
}
}
9
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents