Documente Academic
Documente Profesional
Documente Cultură
Bertrand Estellon
29 mai 2012
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 1 / 379
Java Cours 1
Les objectifs :
I Faciliter le developpement et levolution des applications ;
I Permettre le travail en equipe ;
I Augmenter la qualite des logiciels (moins de bugs).
Solutions proposees :
I Decoupler (separer) les parties des projets ;
I Limiter (et localiser) les modifications lors des evolutions ;
I Reutiliser facilement du code.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 2 / 379
Java Cours 1
Le langage Java :
I est un langage de programmation oriente objet
I cree par James Gosling et Patrick Naughton (Sun)
I presente officiellement le 23 mai 1995.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 3 / 379
Java Cours 1
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 4 / 379
Java Cours 1
Le programme HelloWorld.java :
class HelloWorld {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g [ ] ) {
System . o u t . p r i n t l n ( H e l l o w o r l d ! ) ;
}
}
Compilation et execution :
$ javac HelloWorld.java
$ ls
HelloWorld.java HelloWorld.class
$ java HelloWorld
Hello world !
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 5 / 379
Java Cours 1
Commentaires
class HelloWorld {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g [ ] ) {
/ Commentaire
sur p l u s i e u r s l i g n e s .
/
// s u r une s e u l e l i g n e .
System . o u t . p r i n t l n ( H e l l o w o r l d ! ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 6 / 379
Java Cours 1
int a = 12;
double b = 1 3 . 5 ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 7 / 379
Java Cours 1
Syntaxe
c l a s s PetitProgramme {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g [ ] ) {
f o r ( i n t i = 0 ; i < 1 0 0 ; i ++)
System . o u t . p r i n t l n ( i ) ;
boolean b = t r u e ;
int k = 1;
while ( b ) {
i f ( k%100==0) b = f a l s e ;
k++;
}
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 8 / 379
Java Cours 1
Les structures du C
En C, la declaration dune structure permet de definir ses champs :
s t r uc t DeuxEntiers {
int x ;
int y ;
}
typedef s t r u c t DeuxEntiers DeuxEntiers ;
Lallocation de differentes zones memoires utilisant cette structure :
D e u x E n t i e r s p1 = m a l l o c ( s i z e o f p1 ) ;
D e u x E n t i e r s p2 = m a l l o c ( s i z e o f p2 ) ;
Acces aux donnees des structures :
p r i n t f ( %d %d , p1>x , p1>y ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 9 / 379
Java Cours 1
Les structures du C
70
1
20
50 2
20
30
10 20 20
90
20 50
20 40
Programme
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 10 / 379
Java Cours 1
70
20 1
20 2
50
30
10 20
20
20 90 50
20 40
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 11 / 379
Java Cours 1
Un objet :
I rend un ensemble de services (interface de lobjet)
I contient des donnees (etat de lobjet)
AecterValeurs
10
20 CalculerSomme
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 12 / 379
Java Cours 1
class Additionneur {
i n t v1 , v2 ;
void a f f e c t e r V a l e u r s ( i n t valeur1 , i n t v a l e u r 2 ) {
v1 = v a l e u r 1 ;
v2 = v a l e u r 2 ;
}
in t calculerSomme () {
r e t u r n v1+v2 ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 13 / 379
Java Cours 1
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 14 / 379
Java Cours 1
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 15 / 379
Java Cours 1
Reference null
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 16 / 379
Java Cours 1
Comparaison de references
Il est possible de comparer deux references :
class ProgrammePrincipal {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g [ ] ) {
A d d i t i o n n e u r add = new A d d i t i o n n e u r ( ) ;
A d d i t i o n n e u r add2 = add ;
A d d i t i o n n e u r add3 = new A d d i t i o n n e u r ( ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 17 / 379
Java Cours 1
class ProgrammePrincipal {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g [ ] ) {
A d d i t i o n n e u r add = new A d d i t i o n n e u r ( ) ;
add . a f f e c t e r V a l e u r s ( 1 0 , 3 0 ) ;
i n t r = add . c a l c u l e r S o m m e ( ) ;
System . o u t . p r i n t l n ( r ) ;
add = n u l l ; // Linstance creee par le new peut etre detruite
....
}
}
Objectif : une reference contient null ou designe un objet
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 18 / 379
Java Cours 1
cla ss ProgrammePrincipal {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g [ ] ) {
Lien l i e n = creerCycle ( ) ; l i e n = null ;
... //Toutes les instances qui composent le cycle ne sont plus accessibles.
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 19 / 379
Java Cours 1
Constructeur (Declaration)
class Additionneur {
i n t v1 , v2 ;
in t calculerSomme () {
r e t u r n v1+v2 ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 20 / 379
Java Cours 1
p u b l i c A( ) {
a = 1;
a = 2;
}
}
Constructeur (Utilisation)
class ProgrammePrincipal {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g [ ] ) {
A d d i t i o n n e u r add1 = new A d d i t i o n n e u r ( 1 0 , 2 0 ) ;
A d d i t i o n n e u r add2 = new A d d i t i o n n e u r ( 2 0 ) ;
i n t r 1 = add1 . c a l c u l e r S o m m e ( ) ;
i n t r 2 = add2 . c a l c u l e r S o m m e ( ) ;
System . o u t . p r i n t l n ( r 1 ) ; // a f f i c h e 30
System . o u t . p r i n t l n ( r 2 ) ; // a f f i c h e 40
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 22 / 379
Java Cours 1
Mot-cle this
class Additionneur {
in t calculerSomme () {
r e t u r n v a l e u r 1+v a l e u r 2 ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 23 / 379
Java Cours 1
Mot-cle this
class Additionneur {
in t calculerSomme () {
r e t u r n v a l e u r 1+v a l e u r 2 ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 24 / 379
Java Cours 1
static int c ;
i n t v1 , v2 ;
public i n t calculerSomme () {
r e t u r n v1+v2+c ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 25 / 379
Java Cours 1
cla ss ProgrammePrincipal {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g ) {
Additionneur . setConstant (1);
A d d i t i o n n e u r add1 = new A d d i t i o n n e u r ( 1 0 , 1 0 ) ;
A d d i t i o n n e u r add2 = new A d d i t i o n n e u r ( 2 0 , 2 0 ) ;
System . o u t . p r i n t l n ( add1 . c a l c u l e r S o m m e ( ) ) ; // affiche 21
System . o u t . p r i n t l n ( add2 . c a l c u l e r S o m m e ( ) ) ; // affiche 41
Additionneur . c = 2;
System . o u t . p r i n t l n ( add1 . c a l c u l e r S o m m e ( ) ) ; // affiche 22
System . o u t . p r i n t l n ( add2 . c a l c u l e r S o m m e ( ) ) ; // affiche 42
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 26 / 379
Java Cours 1
static int c ;
...
public s t a t i c void setConstant ( i n t c ) {
Additionneur . c = c ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 27 / 379
Java Cours 1
Les tableaux
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 28 / 379
Java Cours 1
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 29 / 379
Java Cours 1
i n t [ ] t a b = new i n t [ 1 2 ] ;
f o r ( i n t i = 0 ; i < t a b . l e n g t h ; i ++) t a b [ i ] = i ;
System . o u t . p r i n t l n ( t a b [ 1 2 ] ) ;
i n t [ ] tab = n u l l ;
System . o u t . p r i n t l n ( t a b [ 1 2 ] ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 30 / 379
Java Cours 1
Chanes de caracteres
Trois classes permettent de gerer les chanes de caracteres :
I la classe String : chane invariable ;
I la classe StringBuffer : chane modifiable (multi-thread).
I la classe StringBuilder : chane modifiable (mono-thread).
Declaration et creation :
S t r i n g h = H e l l o ;
S t r i n g w = World ;
Concatenation :
S t r i n g hw = h + + w + ! ;
int c = 13;
S t r i n g hw12c = h + + w + + 12 + + c ;
La conversion est effectee en utilisant lune des methodes statiques
valueOf de la classe String.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 31 / 379
Java Cours 1
Chanes de caracteres
Affichage :
System . o u t . p r i n t ( hw ) ; // affiche Hello World !
System . o u t . p r i n t l n ( hw12c ) ; // affiche Hello World 12 13
// avec retour a la ligne
Comparaison :
S t r i n g a1 = a ;
S t r i n g a2 = a ;
S t r i n g a3 = new S t r i n g ( a ) ;
System . o u t . p r i n t l n ( a1==a2 ) ; // affiche true
System . o u t . p r i n t l n ( a1==a3 ) ; // affiche false
System . o u t . p r i n t l n ( a1 . e q u a l s ( a3 ) ) ; // affiche true
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 32 / 379
Java Cours 1
cla ss ProgrammePrincipal {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g ) {
F o r m u l a i r e f = new F o r m u l a i r e ( 2 ) ;
f . a j o u t e r B o u t o n ( new Bouton ( o u v r i r , 1 0 , 1 0 , 2 0 , 2 0 ) ) ;
f . a j o u t e r B o u t o n ( new Bouton ( a n n u l e r , 3 0 , 1 0 , 4 0 , 2 0 ) ) ;
Bouton b ;
b = f . d e t e c t e r C l i c (15 , 15);
i f ( b!= n u l l ) b . a f f i c h e r T e x t e ( ) ; // affiche ouvrir
b = f . d e t e c t e r C l i c (35 , 15);
i f ( b!= n u l l ) b . a f f i c h e r T e x t e ( ) ; // affiche annuler
b = f . d e t e c t e r C l i c (50 , 15);
i f ( b!= n u l l ) b . a f f i c h e r T e x t e ( ) ; // naffiche rien
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 33 / 379
Java Cours 1
i n t nbBoutons ;
Bouton [ ] b o u t o n s ;
F o r m u l a i r e ( i n t maxNbBoutons ) {
b o u t o n s = new Bouton [ maxNbBoutons ] ; nbBoutons = 0 ;
}
v o i d a j o u t e r B o u t o n ( Bouton bo ut on ) {
b o u t o n s [ nbBoutons ] = b ou t o n ; nbBoutons++;
}
Bouton d e t e c t e r C l i c ( i n t x , i n t y ) {
f o r ( i n t i = 0 ; i < nbBoutons ; ++i )
i f ( boutons [ i ] . d e t e c t e r C l i c ( x , y ))
return boutons [ i ] ;
return null ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 34 / 379
Java Cours 1
String texte ;
i n t x1 , y1 , x2 , y2 ;
Bouton ( S t r i n g t e x t e , i n t x1 , i n t y1 , i n t x2 , i n t y2 ) {
this . texte = texte ;
t h i s . x1 = x1 ; t h i s . y1 = y1 ;
t h i s . x2 = x2 ; t h i s . y2 = y2 ;
}
b o olea n d e t e c t e r C l i c ( i n t x , i n t y ) {
r e t u r n x>=x1 && x<=x2 && y>=y1 && y <= y2 ;
}
void a f f i c h e r T e x t e () {
System . o u t . p r i n t l n ( t e x t e ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 35 / 379
Java Cours 1
Packages
I Par defaut, les classes sont placees dans le paquet sans nom.
I Les noms de paquet qui commencent par java. sont reserves a Java.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 36 / 379
Java Cours 1
I Une classe peut etre designee en prefixant le nom de la classe par celui
du paquet (java.awt.List = classe List du paquet java.awt).
I Une classe C du paquet nomPaquet peut etre designee par son nom :
I depuis les classes du paquet nomPaquet
I depuis les fichiers qui contiennent import nomPaquet.C
I depuis les fichiers qui contiennent import nomPaquet.*
I Les classes du paquet java.lang sont toujours importees : ce paquet
contient les classes fondamentales de Java (Object, String...).
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 37 / 379
Java Cours 1
I Par defaut, une classe ou une methode est non-publique : elle nest
accessible que depuis les classes du meme paquet.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 38 / 379
Java Cours 1
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 39 / 379
Java Cours 1
p r i v a t e i n t maP i l e [ ] = new i n t [ 1 0 0 ] ;
private int t a i l l e = 0;
Resume du cours
Les points abordes pendant ce cours :
I Objets, classes et instances
I Variables de type reference
I Definir une classe
I Instancier une classe
I Definir et utiliser des constructeurs
I Destruction des instances (Garbage collector)
I Mot-cle this
I Membres statiques
I Les tableaux
I Les chanes de caracteres
I Organisation en packages
I Convention de nommage
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 42 / 379
Java Cours 2
70
20 1
20 2
50
30
10 20
20
20 90 50
20 40
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 44 / 379
Java Cours 2
Un objet :
I rend un ensemble de services (interface de lobjet)
I contient des donnees (etat de lobjet)
AecterValeurs
10
20 CalculerSomme
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 45 / 379
Java Cours 2
Abstraction
Des objets implementant des services identiques de facons differentes :
acher(String s)
alerter()
Telephone
acher(String s)
sonner()
alerter()
acher(String s)
Ordinateur
alerter()
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 46 / 379
Java Cours 2
Abstraction
Objectifs :
I Traiter des objets en utilisant les services quils partagent :
f o r ( i n t i = 0 ; i < a f f i c h e u r s . l e n g t h ; i ++)
afficheurs [ i ]. afficher (s) ;
}
/
Alerte l u t i l i s a t e u r .
/
public void a l e r t e r ( ) ;
}
Une interface peut etre vue comme un contrat entre celui qui utilise les
objets et celui qui les implemente.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 48 / 379
Java Cours 2
c l a s s Telephone implements A f f i c h e u r {
void a l e r t e r () {
a f f i c h e r ( A l e r t e ! ! ! ) ;
}
void sonner ( ) {
System . o u t . p r i n t l n ( b i p ! ! ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 49 / 379
Java Cours 2
S t r i n g nom ;
p u b l i c O r d i n a t e u r ( S t r i n g nom ) {
t h i s . nom = nom ;
}
void a l e r t e r () {
System . o u t . p r i n t l n ( A l e r t e de +nom ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 50 / 379
Java Cours 2
References et interfaces
Declaration dune variable de type reference vers une instance dune
classe qui implemente linterface Afficheur :
Afficheur aff ;
Il nest pas possible dinstancier une interface :
A f f i c h e u r a f f = new A f f i c h e u r ( ) ; // interdit !
a f f . a f f i c h e r ( t o t o ) ; // que faire ?
Il est possible dinstancier une classe qui implemente une interface :
T e l e p h o n e t = new T e l e p h o n e ( ) ;
et de mettre la reference dans une variable de type Afficheur :
Afficheur aff1 = t ;
A f f i c h e u r a f f 2 = new O r d i n a t e u r ( t r u c ) ;
A f f i c h e u r a f f 3 = new I n t e g e r ( 2 ) // impossible ! !
References et interfaces
Utilisation dobjets qui implementent linterface :
class ProgrammePrincipal {
void a f f i c h e r C h a i n e ( A f f i c h e u r [ ] a f f i c h e u r s , S t r i n g s ) {
f o r ( u n s i g n e d i n t i = 0 ; i < a f f i c h e u r s . l e n g t h ; i ++)
afficheurs [ i ]. afficher (s) ;
}
v o i d a f f i c h e r T a b l e a u ( S t r i n g [ ] tab , A f f i c h e u r a ) {
f o r ( u n s i g n e d i n t i = 0 ; i < t a b . l e n g t h ; i ++)
a . a f f i c h e r ( tab [ i ] ) ;
}
}
Verification a la compilation :
A f f i c h e u r a f f = new T e l e p h o n e ( ) ;
a f f . s o n n e r ( ) ; // impossible (ne fait pas partie de linterface)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 52 / 379
Java Cours 2
Polymorphisme
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 53 / 379
Java Cours 2
Resume
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 54 / 379
Java Cours 2
Implementations multiples
interface Visualisable {
/
Affiche une chane de caracteres qui represente lobjet.
/
public void v i s u a l i s e r ( ) ;
}
interface Pile {
/
Empile la valeur v sur la pile.
@param v la valeur a empiler .
/
public void empiler ( i n t v ) ;
/
Depile une valeur sur la pile.
@ r e t u r n la valeur depilee .
/
public int depiler ( ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 55 / 379
Java Cours 2
Implementations multiples
Implementation des deux interfaces precedentes :
c l a s s P i l e V i s u a l i s a b l e implements V i s u a l i s a b l e , P i l e {
i n t [ ] tab ; i n t s i z e ;
p u b l i c v o i d e m p i l e r ( i n t v ) { t a b [ s i z e ] = v ; s i z e ++; }
p u b l i c i n t d e p i l e r ( ) { s i z e ; r e t u r n t a b [ s i z e ] ; }
public void v i s u a l i s e r () {
f o r ( i n t i = 0 ; i < s i z e ; i ++)
System . o u t . p r i n t ( t a b [ i ]+ ) ;
System . o u t . p r i n t l n ( ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 56 / 379
Java Cours 2
Implementations multiples
String s ;
public void v i s u a l i s a b l e () {
System . o u t . p r i n t l n ( s ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 57 / 379
Java Cours 2
Implementations multiples
Exemple :
V i s u a l i s a b l e [ ] t a b = new V i s u a l i s a b l e [ 3 ] ;
t a b [ 0 ] = new S t r i n g V i s u a l i s a b l e ( b o n j o u r ) ;
P i l e V i s u a l i s a b l e p = new P i l e V i s u a l i s a b l e ( 1 0 ) ;
tab [ 1 ] = p ;
t a b [ 2 ] = new S t r i n g V i s u a l i s a b l e ( s a l u t ) ;
p . empiler (10);
p . empiler (30);
System . o u t . p r i n t l n ( p . d e p i l e r ( ) ) ;
p . empiler (12);
f o r ( i n t i = 0 ; i < t a b . l e n g t h ; i ++)
tab [ i ] . v i s u a l i s e r ( ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 58 / 379
Java Cours 2
Implementations multiples
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 59 / 379
Java Cours 2
G3
R5 R6
R4
interface Cliquable {
/
Propage les consequences dun clic
@param x coordonnee x du clic
@param y coordonnee y du clic
@ r e t u r n true si le point touche un point gris de la forme,
false sinon
/
p u b l i c boolean t r a i t e r C l i c ( i n t x , i n t y ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 61 / 379
Java Cours 2
i n t x1 , y1 , x2 , y2 , numero ;
R e c t a n g l e ( i n t x1 , i n t y1 , i n t x2 , i n t y2 , i n t numero ) {
t h i s . x1 = x1 ; t h i s . x2 = x2 ;
t h i s . y1 = y1 ; t h i s . y2 = y2 ;
t h i s . numero = numero ;
}
p u b l i c bo o le a n t r a i t e r C l i c ( i n t x , i n t y ) {
i f ( x>=x1 && x<=x2 && y>=y1 && y<=y2 ) {
System . o u t . p r i n t l n ( R +numero ) ;
return true ;
}
return f a l s e ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 62 / 379
Java Cours 2
i n t numero , s i z e ;
Cliquable [ ] elements ;
Groupe ( i n t c a p a c i t e , i n t numero ) {
e l e m e n t s = new C l i q u a b l e [ c a p a c i t e ] ; size = 0;
t h i s . numero = numero ;
}
v o i d a j o u t e r E l e m e n t ( C l i q u a b l e c ) { e l e m e n t s [ s i z e ]= c ; s i z e ++;}
p u b l i c bo o le a n t r a i t e r C l i c ( i n t x , i n t y ) {
b oolea n r e s = f a l s e ;
f o r ( i n t i = 0 ; i < s i z e ; i ++)
i f ( elements [ i ] . t r a i t e r C l i c (x , y )) r e s = true ;
i f ( r e s ) System . o u t . p r i n t l n ( G +numero ) ;
return res ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 63 / 379
Java Cours 2
g1 . t r a i t e r C l i c ( 6 5 , 6 0 ) ;
Quaffiche ce programme ?
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 64 / 379
Java Cours 2
G1
G2
D1 R2 R3
G3
D3 R5 D2
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 65 / 379
Java Cours 2
i n t x , y , r , numero ;
p u b l i c D i s q u e ( i n t x , i n t y , i n t r , i n t numero ) {
this . x = x ; this . y = y ; this . r = r ;
t h i s . numero = numero ;
}
p u b l i c b o o le a n t r a i t e r C l i c ( i n t x , i n t y ) {
i n t dx = x t h i s . x , dy = y t h i s . y ;
i f ( Math . s q r t ( dx dx+dy dy)<= r ) {
System . o u t . p r i n t l n ( D+numero ) ;
return true ;
}
return f a l s e ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 66 / 379
Java Cours 2
g1 . t r a i t e r C l i c ( 9 2 , 6 5 ) ;
Quaffiche ce programme ?
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 67 / 379
Java Cours 2
G1
G2
D1 R2 R3
G3
D3 R5 D2
onClick()
onClick()
onClick()
onClick()
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 68 / 379
Java Cours 2
public interface C l i c k L i s t e n e r {
/
Invoquee quand lobjet est clique.
/
public void o n C l i c k ( C l i q u a b l e c ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 69 / 379
Java Cours 2
c l a s s R e c t a n g l e implements C l i q u a b l e {
....
C l i c k L i s t e n e r [ ] l i s t e n e r s = new C l i c k L i s t e n e r [ 1 0 0 ] ;
int nbListeners = 0;
...
void a d d C l i c k L i s t e n e r ( C l i c k L i s t e n e r l ) {
l i s t e n e r s [ n b L i s t e n e r s ] = l ; n b L i s t e n e r s ++;
}
p u b l i c bo o le a n t r a i t e r C l i c ( i n t x , i n t y ) {
i f ( x>=x1 && x<=x2 && y>=y1 && y<=y2 ) {
f o r ( i n t i = 0 ; i < n b L i s t e n e r s ; i ++)
l i s t e n e r s [ i ] . onClick ( this );
return true ;
}
return f a l s e ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 70 / 379
Java Cours 2
p u b l i c bo o le a n t r a i t e r C l i c ( i n t x , i n t y ) {
i n t dx = x t h i s . x , dy = y t h i s . y ;
i f ( Math . s q r t ( dx dx + dy dy ) <= r ) {
f o r ( i n t i = 0 ; i < n b L i s t e n e r s ; i ++)
l i s t e n e r s [ i ] . onClick ( this );
return true ;
}
return f a l s e ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 71 / 379
Java Cours 2
p u b l i c c l a s s S i m p l e C l i c k L i s t e n e r implements C l i c k L i s t e n e r {
S t r i n g msg ;
p u b l i c S i m p l e C l i c k L i s t e n e r ( S t r i n g msg ) {
t h i s . msg = msg ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 72 / 379
Java Cours 2
g1 . t r a i t e r C l i c ( 2 0 , 2 0 ) ;
g1 . t r a i t e r C l i c ( 1 0 , 1 0 ) ;
Quaffiche ce programme ?
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 73 / 379
Java Cours 2
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 74 / 379
Java Cours 2
}
Exemple :
R e c t a n g l e r = new R e c t a n g l e ( 1 0 , 1 0 , 4 0 , 4 0 , 6 ) ;
r . a d d C l i c k L i s t e n e r ( new R e c t a n g l e C l i c k L i s t e n e r ( ) ) ;
r . t r a i t e r C l i c (20 , 20);
Quaffiche ce programme ?
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 75 / 379
Java Cours 2
D i s q u e d = new D i s q u e ( 2 0 , 2 0 , 2 0 , 4 ) ;
d . a d d C l i c k L i s t e n e r ( new R e c t a n g l e C l i c k L i s t e n e r ( ) ) ;
d . t r a i t e r C l i c (20 , 20);
Durant lexecution :
Exception in thread "main" java.lang.ClassCastException: Disque cannot be cast to Rectangle
at RectangleClickListener.onClick(RectangleClickListener.java:5)
at Disque.traiterClic(Disque.java:23)
at Test.main(Test.java:26)
Java Result: 1
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 76 / 379
Java Cours 2
Resume
I Polymorphisme dobjet
I Classe anonyme
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 77 / 379
Java Cours 3
Premier cours :
I Objets, classes et instances
I Variables de type reference
I Definir et instancier une classe
I Tableaux, chanes de caracteres
I Packages
Deuxieme cours :
I Description et implementation dinterfaces
I Polymorphisme dobjet
I Transtypage (vers le haut et vers le bas)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 78 / 379
Java Cours 3
G1
G2
D1 R2 R3
G3
D3 R5 D2
onClick()
onClick()
onClick()
onClick()
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 79 / 379
Java Cours 3
public interface C l i c k L i s t e n e r {
/
Invoquee quand lobjet est clique.
/
public void o n C l i c k ( C l i q u a b l e c ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 80 / 379
Java Cours 3
c l a s s R e c t a n g l e implements C l i q u a b l e {
....
C l i c k L i s t e n e r [ ] l i s t e n e r s = new C l i c k L i s t e n e r [ 1 0 0 ] ;
int nbListeners = 0;
...
void a d d C l i c k L i s t e n e r ( C l i c k L i s t e n e r l ) {
l i s t e n e r s [ n b L i s t e n e r s ] = l ; n b L i s t e n e r s ++;
}
p u b l i c bo o le a n t r a i t e r C l i c ( i n t x , i n t y ) {
i f ( x>=x1 && x<=x2 && y>=y1 && y<=y2 ) {
f o r ( i n t i = 0 ; i < n b L i s t e n e r s ; i ++)
l i s t e n e r s [ i ] . onClick ( this );
return true ;
}
return f a l s e ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 81 / 379
Java Cours 3
p u b l i c bo o le a n t r a i t e r C l i c ( i n t x , i n t y ) {
i n t dx = x t h i s . x , dy = y t h i s . y ;
i f ( Math . s q r t ( dx dx + dy dy ) <= r ) {
f o r ( i n t i = 0 ; i < n b L i s t e n e r s ; i ++)
l i s t e n e r s [ i ] . onClick ( this );
return true ;
}
return f a l s e ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 82 / 379
Java Cours 3
Abstraction et extension
Le service associe aux listeners est identique. On aimerait :
I definir une classe FormeCliquable qui implemente ce service ;
I sans perdre les specificites dun disque ou dun rectangle.
Interface Cliquable
traiterClic()
Observateur onClick()
addClickListener()
Interface ClickListener
traiterClic() traiterClic()
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 83 / 379
Java Cours 3
Extension
En Java :
I On utilise le mot-cle extends pour etendre une classe ;
I Une classe ne peut etendre quune seule classe.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 84 / 379
Java Cours 3
La classe FormeCliquable
p u b l i c c l a s s F o r m e C l i q u a b l e implements C l i q u a b l e {
private ClickListener [ ] l i s t e n e r s ;
private int nbListeners ;
public FormeCliquable () {
l i s t e n e r s = new C l i c k L i s t e n e r [ 1 0 0 ] ;
nbListeners = 0;
}
public void a d d C l i c k L i s t e n e r ( C l i c k L i s t e n e r l ) {
l i s t e n e r s [ n b L i s t e n e r s ] = l ; n b L i s t e n e r s ++;
}
p u b l i c b o o le a n t r a i t e r C l i c ( i n t x , i n t y ) {
f o r ( i n t i = 0 ; i < n b L i s t e n e r s ; i ++)
l i s t e n e r s [ i ] . onClick ( this );
return true ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 85 / 379
Java Cours 3
p u b l i c i n t x1 , y1 , x2 , y2 , numero ;
R e c t a n g l e ( i n t x1 , i n t y1 , i n t x2 , i n t y2 , i n t numero ) {
t h i s . x1 = x1 ; t h i s . x2 = x2 ;
t h i s . y1 = y1 ; t h i s . y2 = y2 ;
t h i s . numero = numero ;
}
p u b l i c b o o le a n t r a i t e r C l i c ( i n t x , i n t y ) {
i f ( x >= x1 && x <= x2 && y >= y1 && y <= y2 ) {
super . t r a i t e r C l i c (x , y ) ;
return true ;
}
return f a l s e ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 86 / 379
Java Cours 3
p u b l i c i n t x , y , r , numero ;
p u b l i c D i s q u e ( i n t x , i n t y , i n t r , i n t numero ) {
this . x = x ; this . y = y ;
this . r = r ;
t h i s . numero = numero ;
}
p u b l i c b o o le a n t r a i t e r C l i c ( i n t x , i n t y ) {
i n t dx = x t h i s . x , dy = y t h i s . y ;
i f ( Math . s q r t ( dx dx + dy dy ) <= r ) {
return super . t r a i t e r C l i c (x , y ) ;
}
return f a l s e ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 87 / 379
Java Cours 3
Exemple :
public class A {
p u b l i c v o i d method1 ( ) { System . o u t . p r i n t l n ( m1 ) ; }
}
p u b l i c c l a s s B extends A {
p u b l i c v o i d method2 ( ) { System . o u t . p r i n t l n ( m2 ) ; }
}
B b = new B ( ) ;
A a = b ; //upcasting
a . method1 ( ) ; // la methode est implemente
a . method2 ( ) ; // erreur a la compilation : la classe A ne definit pas cette methode
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 88 / 379
Java Cours 3
p u b l i c c l a s s B extends A {
p u b l i c v o i d method2 ( ) { System . o u t . p r i n t l n ( m2 ) ; }
}
A a1 = new A ( ) ;
B b1 = new B ( ) ;
A a2 = b1 ; // upcasting !
B b2 = (B) a2 ; // downcasting possible !
B b3 = (B) a1 ; // downcasting impossible (erreur a lexecution)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 89 / 379
Java Cours 3
public class A {
p u b l i c v o i d a f f i c h e r N o m ( ) { System . o u t . p r i n t l n ( A ) ; }
}
p u b l i c c l a s s B extends A {
p u b l i c v o i d a f f i c h e r N o m ( ) { System . o u t . p r i n t l n ( B ) ; }
}
B b = new B ( ) ; b . a f f i c h e r N o m ( ) ; // affiche B
A a = new A ( ) ; a . a f f i c h e r N o m ( ) ; // affiche A
a = b ; a . a f f i c h e r N o m ( ) ; // affiche B
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 90 / 379
Java Cours 3
Mot-cle super
p u b l i c c l a s s B1 e x t e n d s A {
p u b l i c S t r i n g t e s t ( ) { r e t u r n getName ( ) ; /A/ }
}
p u b l i c c l a s s B2 e x t e n d s A {
p u b l i c S t r i n g getName ( ) { r e t u r n B ; }
p u b l i c S t r i n g t e s t ( ) { r e t u r n getName ( ) ; /B/ }
}
p u b l i c c l a s s B3 e x t e n d s A {
p u b l i c S t r i n g getName ( ) { r e t u r n B ; }
p u b l i c S t r i n g t e s t ( ) { r e t u r n s u p e r . getName ( ) ; /A/ }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 91 / 379
Java Cours 3
Mot-cle super
p u b l i c c l a s s B1 e x t e n d s A {
p u b l i c S t r i n g t e s t ( ) { r e t u r n name ; /A/ }
}
p u b l i c c l a s s B2 e x t e n d s A {
p u b l i c S t r i n g name = B ;
p u b l i c S t r i n g t e s t ( ) { r e t u r n name ; /B/ }
}
p u b l i c c l a s s B3 e x t e n d s A {
p u b l i c S t r i n g name = B ;
p u b l i c S t r i n g t e s t ( ) { r e t u r n s u p e r . name ; /A/ }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 92 / 379
Java Cours 3
public class A {
int a = 1;
int b = 2;
}
est equivalent a
public class A {
int a ;
int b ;
p u b l i c A( ) {
a = 1;
a = 2;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 93 / 379
Java Cours 3
p u b l i c i n t x1 , y1 , x2 , y2 , numero ;
R e c t a n g l e ( i n t x1 , i n t y1 , i n t x2 , i n t y2 , i n t numero ) {
/ Appel du constructeur sans parametre de la classe FormeCliquable /
t h i s . x1 = x1 ; t h i s . x2 = x2 ;
t h i s . y1 = y1 ; t h i s . y2 = y2 ;
t h i s . numero = numero ;
}
...
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 94 / 379
Java Cours 3
p u b l i c c l a s s ClasseAvecNom {
S t r i n g nom ;
p u b l i c ClasseAvecNom ( S t r i n g nom ) { t h i s . nom = nom ; }
}
p u b l i c c l a s s M a Cl a sse e x t e n d s ClasseAvecNom {
p u b l i c M a Cl a sse ( ) {
s u p e r ( MaClasse ) ;
...
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 95 / 379
Java Cours 3
Modificateur final
Exemple 1 :
f i n a l public class A { }
Exemple 2 :
public class A { f i n a l p u b l i c v o i d method ( ) { } }
p u b l i c c l a s s B extends A {
p u b l i c v o i d method ( ) { }
/Impossible car la methode est finale dans A /
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 96 / 379
Java Cours 3
Classes abstraites
p u b l i c c l a s s A e x t e n d s SansNom {
S t r i n g o b t e n i r N o m ( ) { r e t u r n A ; }
}
A a = new A ( ) ;
a . a f f i c h e r N o m ( ) ; // affiche A
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 97 / 379
Java Cours 3
public FormeCliquable () {
l i s t e n e r s = new C l i c k L i s t e n e r [ 1 0 0 ] ;
nbListeners = 0;
}
void a d d C l i c k L i s t e n e r ( C l i c k L i s t e n e r l ) {
l i s t e n e r s [ n b L i s t e n e r s ] = l ; n b L i s t e n e r s ++;
}
public void p r o c e s s L i s t e n e r () {
f o r ( i n t i = 0 ; i < n b L i s t e n e r s ; i ++)
l i s t e n e r s [ i ] . onClick ( this );
}
i n t x , y , r , numero ;
p u b l i c D i s q u e ( i n t x , i n t y , i n t r , i n t numero ) {
this . x = x ;
this . y = y ;
this . r = r ;
t h i s . numero = numero ;
}
p u b l i c b o o le a n t r a i t e r C l i c ( i n t x , i n t y ) {
i n t dx = x t h i s . x , dy = y t h i s . y ;
i f ( Math . s q r t ( dx dx + dy dy ) <= r ) {
processListener ();
return true ;
}
return f a l s e ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 99 / 379
Java Cours 3
I Par defaut, une classe ou une methode est non-publique : elle nest
accessible que depuis les classes du meme paquet.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 100 / 379
Java Cours 3
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 101 / 379
Java Cours 3
Modificateur protected
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 102 / 379
Java Cours 3
public FormeCliquable () {
l i s t e n e r s = new C l i c k L i s t e n e r [ 1 0 0 ] ;
nbListeners = 0;
}
void a d d C l i c k L i s t e n e r ( C l i c k L i s t e n e r l ) {
l i s t e n e r s [ n b L i s t e n e r s ] = l ; n b L i s t e n e r s ++;
}
protected void p r o c e s s L i s t e n e r () {
f o r ( i n t i = 0 ; i < n b L i s t e n e r s ; i ++)
l i s t e n e r s [ i ] . onClick ( this );
}
p u b l i c a b s t r a c t b oolean t r a i t e r C l i c ( i n t x , i n t y ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 103 / 379
Java Cours 3
La classe Object
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 104 / 379
Java Cours 3
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 105 / 379
Java Cours 3
La methode toString()
Une implementation possible des String.valueOf(...)
class String {
...
public s t a t i c S t r i n g valueOf ( Object obj ) {
i f ( o b j==n u l l ) r e t u r n n u l l ;
else return obj . toString () ;
}
p u b l i c s t a t i c S t r i n g v a l u e O f ( boolean b ) {
i f ( b ) return t r u e ;
e l s e return f a l s e ;
}
...
}
Une implementation possible des System.out.print(...) :
p u b l i c v o i d p r i n t ( S t r i n g s ) { ecrire s sur la sortie ; }
p u b l i c v o i d p r i n t ( b o olean b ) { p r i n t ( S t r i n g . v a l u e O f ( b ) ) ; }
public void p r i n t ( Object obj ) { p r i n t ( S t r i n g . valueOf ( obj ) ) ; }
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 106 / 379
Java Cours 3
p r i v a t e i n t numero ;
Exemple dutilisation :
M a Classe c1 = new M a Classe ( 1 ) ;
M a Classe c2 = new M a Classe ( 2 ) ;
System . o u t . p r i n t l n ( c1 . t o S t r i n g ( ) + + c2 . t o S t r i n g ( ) ) ;
System . o u t . p r i n t l n ( c1 + + c2 ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 107 / 379
Java Cours 3
Extension dinterfaces
interface I t e r a t o r {
boolean h a s N e x t ( ) ;
Object next ( ) ;
}
i n t e r f a c e L i s t I t e r a t o r extends I t e r a t o r {
boolean h a s P r e v i o u s ( ) ;
Object p r e v i o u s ( ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 108 / 379
Java Cours 3
Extension dinterfaces
c l a s s V e c t o r I t e r a t o r implements L i s t I t e r a t o r {
f i n a l private Vector v ;
private int p ;
public V e c t o r I t e r a t o r ( Vector v ) {
this . v = v ; p = 0;
}
p u b l i c b o o le a n h a s P r e v i o u s ( ) { r e t u r n ( p > 0 ) ; }
p u b l i c O b j e c t p r e v i o u s ( ) { p; r e t u r n v . g e t ( p ) ; }
p u b l i c b o o le a n h a s N e x t ( ) { r e t u r n p < v . s i z e ( ) ; }
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 109 / 379
Java Cours 3
Extension dinterfaces
Exemples dutilisation :
V e c t o r v = new V e c t o r ( ) ;
...
L i s t I t e r a t o r i t e r a t o r = new V e c t o r I t e r a t o r ( v ) ;
while ( i t e r a t o r . hasNext ( ) )
System . o u t . p r i n t l n ( i t e r a t o r . n e x t ( ) ) ;
while ( i t e r a t o r . hasPrevious ( ) )
System . o u t . p r i n t l n ( i t e r a t o r . p r e v i o u s ( ) ) ;
...
I t e r a t o r i t e r a t o r = new V e c t o r I t e r a t o r ( v ) ;
while ( i t e r a t o r . hasNext ( ) )
System . o u t . p r i n t l n ( i t e r a t o r . n e x t ( ) ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 110 / 379
Java Cours 3
Resume
I Abstraction
I Etendre une classe
I Transtypage (vers le haut et vers le bas)
I Redefinition de methodes et polymorphisme
I mot-cle super
I Construction des instances
I Modificateurs final et protected
I Classes et methodes abstraites
I La classe Object
I La methode toString()
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 111 / 379
Java Cours 4
Premier cours :
I Objets, classes, instances, references
I Definir et instancier une classe
Deuxieme cours :
I Description et implementation dinterfaces
I Transtypage (vers le haut et vers le bas)
Troisieme cours :
I Extension de classe
I Redefinition de methodes et polymorphisme
I Classes et methodes abstraites
I La classe Object
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 112 / 379
Java Cours 4
Classes internes
p u b l i c v o i d add ( i n t v ) { t e t e = new M a i l l o n ( v , t e t e ) ; }
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 113 / 379
Java Cours 4
Classes internes
public c l a s s Vector {
p r i v a t e Object [ ] array ;
private int size ;
...
p u b l i c I t e r a t o r i t e r a t o r ( ) { r e t u r n new V e c t o r I t e r a t o r ( t h i s ) ; }
}
p u b l i c c l a s s V e c t o r I t e r a t o r implements I t e r a t o r {
private int position = 0;
p r i v a t e Vector v ;
public V e c t o r I t e r a t o r ( Vector v ) { t h i s . v = v ; }
p u b l ic Object next () {
Object o = v . array [ p o s i t i o n ] ; p o s i t i o n ++;
return o ;
}
p u b l i c v o i d remove ( ) { }
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 114 / 379
Java Cours 4
Classes internes
Une classe interne peut utiliser les membres de la classe qui la contient :
public c l a s s Vector {
p r i v a t e Object [ ] array ;
private int size ;
...
p u b l i c I t e r a t o r i t e r a t o r ( ) { r e t u r n new V e c t o r I t e r a t o r ( ) ; }
p r i v a t e c l a s s V e c t o r I t e r a t o r implements I t e r a t o r {
int position = 0;
p u b l ic Object next () {
Object o = array [ p o s i t i o n ] ; p o s i t i o n ++;
return o ;
}
p u b l i c v o i d remove ( ) { }
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 115 / 379
Java Cours 4
Classes anonymes
public c l a s s Vector {
...
public I t e r a t o r i t e r a t o r () {
r e t u r n new I t e r a t o r ( ) {
int position = 0;
p u b l i c Object next () {
Object o = array [ p o s i t i o n ] ; p o s i t i o n ++;
return o ;
}
p u b l i c v o i d remove ( ) { }
};
}
}
Introduction a AWT
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 117 / 379
Java Cours 4
Composants
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 118 / 379
Java Cours 4
Conteneurs
I Un conteneur est un composant ;
I Un conteneur peut contenir plusieurs composants ;
I Un conteneur etend la classe Container.
Les principaux conteneurs de AWT :
I Panel
I Applet
I Window
I Dialog
I FileDialog
I JDialog (Swing)
I Frame
I JFrame (Swing)
I JWindow (Swing)
I JComponent (Swing)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 119 / 379
Java Cours 4
Exemple
Frame f r a m e = new Frame ( Ma f r a m e ) ;
f r a m e . s e t P r e f e r r e d S i z e ( new D i m e n s i o n ( 2 0 0 , 2 0 0 ) ) ;
P a n e l p a n e l = new P a n e l ( ) ;
p a n e l . add ( new But ton ( Un b ou t o n ) ) ;
p a n e l . add ( new Checkbox ( Truc , f a l s e ) ) ;
C h o i c e c h o i c e = new C h o i c e ( ) ;
c h o i c e . add ( c h o i x 1 ) ; c h o i c e . add ( c h o i x 2 ) ;
p a n e l . add ( c h o i c e ) ;
p a n e l . add ( new L a b e l ( l a b e l ) ) ;
L i s t l i s t = new L i s t ( ) ;
l i s t . add ( c h o i x 1 ) ; l i s t . add ( c h o i x 2 ) ;
p a n e l . add ( l i s t ) ;
p a n e l . add ( new T e x t F i e l d ( Truc ) ) ;
T e x t A r e a t e x t A r e a = new T e x t A r e a ( Truc \ nTruc ) ;
t e x t A r e a . setColumns ( 1 0 ) ; t e x t A r e a . setRows ( 3 ) ;
p a n e l . add ( t e x t A r e a ) ;
f r a m e . add ( p a n e l ) ;
f r a m e . pack ( ) ;
frame . s e t V i s i b l e ( true ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 120 / 379
Java Cours 4
Exemple
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 121 / 379
Java Cours 4
Gestionnaires de presentation
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 122 / 379
Java Cours 4
Gestionnaires de presentation
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 123 / 379
Java Cours 4
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 124 / 379
Java Cours 4
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 125 / 379
Java Cours 4
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 126 / 379
Java Cours 4
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 127 / 379
Java Cours 4
Evenements
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 128 / 379
Java Cours 4
Evenements
Exemple :
p u b l i c c l a s s M y A c t i o n L i s t e n e r implements A c t i o n L i s t e n e r {
p u b l i c void a c t i o n P e r f o r m e d ( Action Event e ) {
System . o u t . p r i n t l n ( C l i c ! ! ) ;
}
}
Remarque :
I ActionListener est une extension de linterface EventListener
I ActionEvent est une extension de linterface AWTEvent
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 129 / 379
Java Cours 4
Evenements Source
p u b l i c c l a s s M y A c t i o n L i s t e n e r implements A c t i o n L i s t e n e r {
public void actionPerformed ( ActionEvent e ) {
System . o u t . p r i n t l n ( e . g e t S o u r c e ( ) / Soit b1, soit b2 / ) ;
}
}
A c t i o n L i s t e n e r l i s t e n e r = new new M y A c t i o n L i s t e n e r ( ) ;
But ton b1 = new But ton ( B1 ) ;
b1 . a d d A c t i o n L i s t e n e r ( l i s t e n e r ) ;
p a n e l . add ( b1 ) ;
But ton b2 = new But ton ( B2 ) ;
b2 . a d d A c t i o n L i s t e n e r ( l i s t e n e r ) ;
Exemple de sortie :
java.awt.Button[button0,39,5,29x23,label=B2]
java.awt.Button[button1,5,5,29x23,label=B1]
java.awt.Button[button0,39,5,29x23,label=B2]
java.awt.Button[button1,5,5,29x23,label=B1]
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 130 / 379
Java Cours 4
Evenements Souris
p u b l i c i n t e r f a c e MouseListener extends E v e n t L i s t e n e r {
p u b l i c v o i d m o u s e C l i c k e d ( MouseEvent e ) ;
p u b l i c v o i d m o u s e P r e s s e d ( MouseEvent e ) ;
p u b l i c v o i d m o u s e R e l e a s e d ( MouseEvent e ) ;
p u b l i c v o i d m o u s e E n t e r e d ( MouseEvent e ) ;
p u b l i c v o i d m o u s e E x i t e d ( MouseEvent e ) ;
}
p u b l i c i n t e r f a c e MouseMotionListener extends E v e n t L i s t e n e r {
p u b l i c v o i d mouseDragged ( MouseEvent e ) ;
p u b l i c v o i d mouseMoved ( MouseEvent e ) ;
}
p u b l i c i n t e r f a c e MouseWheelListener extends E v e n t L i s t e n e r {
p u b l i c v o i d mouseWheelMoved ( MouseWheelEvent e ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 131 / 379
Java Cours 4
Evenements Souris
Implementation de linterface MouseListener :
p u b l i c c l a s s S i m p l e M o u s e L i s t e n e r implements M o u s e L i s t e n e r {
p u b l i c v o i d m o u s e C l i c k e d ( MouseEvent e ) {
System . o u t . p r i n t l n ( e . getX ()+ +e . getY ( ) ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 132 / 379
Java Cours 4
Evenements Clavier
p u b l i c i n t e r f a c e K e y L i s t e n e r extends E v e n t L i s t e n e r {
p u b l i c v o i d keyTyped ( KeyEvent e ) ;
p u b l i c v o i d k e y P r e s s e d ( KeyEvent e ) ;
p u b l i c v o i d k e y R e l e a s e d ( KeyEvent e ) ;
}
Exemple dimplementation :
p u b l i c c l a s s S i m p l e K e y L i s t e n e r implements K e y L i s t e n e r {
p u b l i c v o i d keyTyped ( KeyEvent e ) { }
p u b l i c v o i d k e y P r e s s e d ( KeyEvent e ) {
System . o u t . p r i n t l n ( e . g e t K e y C h a r ( ) ) ;
}
p u b l i c v o i d k e y R e l e a s e d ( KeyEvent e ) { }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 133 / 379
Java Cours 4
Evenements Fenetre
p u b l i c i n t e r f a c e WindowListener extends E v e n t L i s t e n e r {
p u b l i c v o i d windowOpened ( WindowEvent e ) ;
p u b l i c v o i d w i n d o w C l o s i n g ( WindowEvent e ) ;
p u b l i c v o i d w i n d o w C l o s e d ( WindowEvent e ) ;
p u b l i c v o i d w i n d o w I c o n i f i e d ( WindowEvent e ) ;
p u b l i c v o i d w i n d o w D e i c o n i f i e d ( WindowEvent e ) ;
p u b l i c v o i d w i n d o w A c t i v a t e d ( WindowEvent e ) ;
p u b l i c v o i d w i n d o w D e a c t i v a t e d ( WindowEvent e ) ;
}
p u b l i c i n t e r f a c e WindowStateListener extends E v e n t L i s t e n e r {
p u b l i c v o i d w in dowStateChanged ( WindowEvent e ) ;
}
p u b l i c i n t e r f a c e WindowFocusListener extends E v e n t L i s t e n e r {
p u b l i c v o i d w i n d o w G a i n e d F o c u s ( WindowEvent e ) ;
p u b l i c v o i d w i n d o w L o s t F o c u s ( WindowEvent e ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 134 / 379
Java Cours 4
Evenements Adapteurs
I Un adapteur est une classe qui implemente un observateur en
associant un corps vide a chacune des methodes.
I Pour eviter dimplementer toutes les methodes dun observateur, on
peut etendre ladapteur et redefinir certaines de ses methodes.
p u b l i c c l a s s S i m p l e W i n d o w L i s t e n e r implements W i n d o w L i s t e n e r {
p u b l i c v o i d windowOpened ( WindowEvent e ) { }
p u b l i c v o i d w i n d o w C l o s i n g ( WindowEvent e ) { System . e x i t ( 0 ) ; }
p u b l i c v o i d w i n d o w C l o s e d ( WindowEvent e ) { }
p u b l i c v o i d w i n d o w I c o n i f i e d ( WindowEvent e ) { }
p u b l i c v o i d w i n d o w D e i c o n i f i e d ( WindowEvent e ) { }
p u b l i c v o i d w i n d o w A c t i v a t e d ( WindowEvent e ) { }
p u b l i c v o i d w i n d o w D e a c t i v a t e d ( WindowEvent e ) { }
}
est equivalent a
p u b l i c c l a s s S i m p l e W i n d o w L i s t e n e r e x t e n d s WindowAdapter {
p u b l i c v o i d w i n d o w C l o s i n g ( WindowEvent e ) { System . e x i t ( 0 ) ; }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 135 / 379
Java Cours 4
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 136 / 379
Java Cours 4
Exemple Calculette
p u b l i c c l a s s C a l c u l e t t e e x t e n d s Frame {
p r i v a t e T e x t F i e l d number1 , number2 ;
private Label r e s u l t ;
public Calculette () {
s e t P r e f e r r e d S i z e ( new D i m e n s i o n ( 2 0 0 , 8 0 ) ) ;
P a n e l p = new P a n e l ( ) ;
number1 = new T e x t F i e l d ( 5 ) ;
number2 = new T e x t F i e l d ( 5 ) ;
r e s u l t = new L a b e l ( 0 ) ;
But ton b = new Button ( Add ) ;
b . a d d A c t i o n L i s t e n e r ( new B o u t o n L i s t e n e r ( ) ) ;
p . add ( number1 ) ;
p . add ( number2 ) ;
p . add ( r e s u l t ) ;
p . add ( b ) ;
add ( p ) ;
pack ( ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 137 / 379
Java Cours 4
Exemple Calculette
p r i v a t e T e x t F i e l d number1 , number2 ;
private Label r e s u l t ;
....
p r i v a t e c l a s s B o u t o n L i s t e n e r implements A c t i o n L i s t e n e r {
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 138 / 379
Java Cours 4
Exemple Calculette
La methode main :
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
C a l c u l e t t e c a l c u l e t t e = new C a l c u l e t t e ( ) ;
c a l c u l e t t e . s e t V i s i b l e ( true ) ;
c a l c u l e t t e . a d d W i n d o w L i s t e n e r ( new WindowAdapter ( ) {
p u b l i c v o i d w i n d o w C l o s i n g ( WindowEvent e ) {
System . e x i t ( 0 ) ;
}
});
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 139 / 379
Java Cours 4
p u b l i c MyComponent ( ) {
s e t P r e f e r r e d S i z e ( new D i m e n s i o n ( 2 0 0 , 2 0 0 ) ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 141 / 379
Java Cours 4
p u b l i c c l a s s MyComponent e x t e n d s Component {
p u b l i c MyComponent ( ) {
s e t P r e f e r r e d S i z e ( new D i m e n s i o n ( 2 0 0 , 2 0 0 ) ) ;
a d d M o u s e L i s t e n e r ( new MyMouseAdapter ( ) ) ;
}
...
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 142 / 379
Java Cours 4
p u b l i c c l a s s MyComponent e x t e n d s Component {
...
p r i v a t e c l a s s MyMouseAdapter e x t e n d s MouseAdapter {
p u b l i c v o i d m o u s e C l i c k e d ( MouseEvent e ) {
i f ( c o l o r == C o l o r . w h i t e )
c o l o r = Color . gray ;
else co lo r = Color . white ;
repaint () ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 143 / 379
Java Cours 4
Swing
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 144 / 379
Java Cours 4
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 145 / 379
Java Cours 4
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 146 / 379
Java Cours 4
GTKLookAndFeel
MotifLookAndFeel
WindowsLookAndFeel
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 147 / 379
Java Cours 4
Au debut du programme :
try {
UIManager . s e t L o o k A n d F e e l (
com . sun . j a v a . s w i n g . p l a f . g t k . GTKLookAndFeel ) ;
} catch ( E x c e p t i o n e ) {
System . o u t . p r i n t l n ( E r r o r s e t t i n g LookAndFeel : + e ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 148 / 379
Java Cours 4
p u b l i c MyComponentSwing ( ) {
s e t P r e f e r r e d S i z e ( new D i m e n s i o n ( 2 0 0 , 2 0 0 ) ) ;
}
p u b l i c void paintComponent ( G r a p h i c s g ) {
s u p e r . paintComponent ( g ) ;
Dimension s i z e = g e t S i z e ( ) ;
g . setColor ( Color . white ) ;
g . f i l l O v a l ( 0 , 0 , s i z e . w idth , s i z e . h e i g h t ) ;
g . setColor ( Color . blue ) ;
g . d r a w L i n e ( 0 , s i z e . h e i g h t / 2 , s i z e . w idt h , s i z e . h e i g h t / 2 ) ;
g . drawLine ( s i z e . width /2 , 0 , s i z e . width /2 , s i z e . h e i g h t ) ;
g . setColor ( Color . red ) ;
g . drawOval ( 0 , 0 , s i z e . widt h , s i z e . h e i g h t ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 149 / 379
Java Cours 4
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 150 / 379
Java Cours 5
Premier cours :
I Objets, classes, instances, references
I Definir et instancier une classe
Deuxieme cours :
I Description et implementation dinterfaces
I Transtypage (vers le haut et vers le bas)
Troisieme cours :
I Extension de classe
I Redefinition de methodes et polymorphisme
I Classes et methodes abstraites
I La classe Object
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 151 / 379
Java Cours 5
Surcharge de methodes
class Additionneur {
public static int additionner ( int a , int b) {
System . o u t . p r i n t l n ( e n t i e r ) ; r e t u r n a+b ;
}
p u b l i c s t a t i c double a d d i t i o n n e r ( double a , double b ) {
System . o u t . p r i n t l n ( f l o t t a n t ) ; r e t u r n a+b ;
}
}
i n t i = 1; double d = 2 . 2 ;
d o u b l e r 1 = A d d i t i o n n e u r . a d d i t i o n n e r ( d , d ) ; // flottant
d o u b l e r 2 = A d d i t i o n n e u r . a d d i t i o n n e r ( i , d ) ; // flottant
i n t r 3 = A d d i t i o n n e u r . a d d i t i o n n e r ( i , i ) ; // entier
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 152 / 379
Java Cours 5
Surcharge de methodes
class Afficheur {
s t a t i c void A f f i c h e r ( Object o ) {
System . o u t . p r i n t l n ( O b j e c t : +o ) ;
}
s t a t i c void A f f i c h e r ( String s ) {
System . o u t . p r i n t l n ( S t r i n g : +s ) ;
}
}
S t r i n g s = m e s s a g e ;
Object o = s ;
A f f i c h e u r . A f f i c h e r ( s ) ; //String : message
A f f i c h e u r . A f f i c h e r ( o ) ; //Object : message
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 153 / 379
Java Cours 5
class A {
p u b l i c v o i d method (D d ) { System . o u t . p r i n t l n ( D ) ; }
}
c l a s s B extends A {
p u b l i c v o i d method (C c ) { System . o u t . p r i n t l n ( C ) ; }
}
B b = new B ( ) ;
b . method ( new C ( ) ) ; //affiche C
b . method ( new D ( ) ) ; //affiche D
Covariance
class A {
p u b l i c O b j e c t g e t ( ) { r e t u r n O b j e c t ; }
}
c l a s s B extends A {
p u b l i c S t r i n g get ( ) { return S t r i n g ; }
}
B b = new B ( ) ; A a = b ;
String s = b . get ( ) ;
System . o u t . p r i n t l n ( s ) ; // Affiche String
Object o = a . get ( ) ; // Le type de retour est Object (et pas String)
System . o u t . p r i n t l n ( o ) ; // Affiche String
a = new A ( ) ;
o = a . get ( ) ;
System . o u t . p r i n t l n ( o ) ; // Affiche Object
La classe Pile :
public class Pile {
private Object [ ] p i l e ;
private int t a i l l e ;
p u b l i c P i l e ( ) { p i l e = new O b j e c t [ 1 0 0 ] ; t a i l l e = 0; }
public Object d e p i l e r () {
t a i l l e ; O b j e c t o = p i l e [ t a i l l e ] ; p i l e [ t a i l l e ]= n u l l ;
return o ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 156 / 379
Java Cours 5
Deuxieme probleme :
P i l e p = new P i l e ( ) ;
I n t e g e r i = new I n t e g e r ( 2 ) ;
p . empiler ( i ); // Ok car Integer etend Object
S t r i n g s = ( S t r i n g ) p . d e p i l e r ( ) ; // Erreur a lexecution (String != Integer)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 157 / 379
Java Cours 5
p u b l i c P i l e ( ) { p i l e = new O b j e c t [ 1 0 0 ] ; t a i l l e = 0; }
p u b l i c v o i d e m p i l e r (T o ) {
p i l e [ t a i l l e ] = o ; t a i l l e ++;
}
public T depiler () {
t a i l l e ; T e = p i l e [ t a i l l e ] ; p i l e [ t a i l l e ]= n u l l ;
r e t u r n (T) e ;
}
}
En Java, les types parametres ajoutent des verifications de type et des
transtypages automatiques lors de la compilation mais ne modifient pas le
bytecode genere.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 158 / 379
Java Cours 5
Boxing et unboxing
Comment obtenir une pile dentiers ?
P i l e <i n t > p = new P i l e <i n t > ( ) ; Impossible : int nest pas le nom dune classe
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 160 / 379
Java Cours 5
Classes demballage
La classe Number :
I public abstract int intValue ()
I public abstract long longValue ()
I public abstract float floatValue ()
I public abstract double doubleValue ()
I public byte byteValue()
I public short shortValue()
Classes demballage
La classe Boolean :
I public Boolean(bool b)
I public boolean booleanValue()
La classe Character :
I public Character(char c)
I public char charValue()
I public static boolean isLowerCase (char ch)
I public static boolean isUpperCase (char ch)
I public static boolean isTitleCase (char ch)
I public static boolean isDefined (char ch)
I public static boolean isDigit (char ch)
I public static boolean isLetter (char ch)
I public static boolean isLetterOrDigit (char ch)
I public static char toLowerCase (char ch)
I public static char toUpperCase (char ch)
I public static char toTitleCase (char ch)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 162 / 379
Java Cours 5
p u b l i c i n t compareTo ( C a r t e c ) {
i n t cc = c o u l c . c o u l ;
i f ( c c !=0) r e t u r n c c ;
else return val c . val ;
}
p u b l i c S t r i n g t o S t r i n g ( ) { r e t u r n [ +c o u l+ , +v a l+ ] ; }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 163 / 379
Java Cours 5
c l a s s S o r t e d A r r a y <T e x t e n d s Comparable<T>> {
p r i v a t e Object [ ] tab ;
p u b l i c S o r t e d A r r a y ( ) { t a b = new O b j e c t [ 1 0 0 ] ; t a i l l e = 0; }
p u b l i c T g e t ( i n t p ) { r e t u r n (T) t a b [ p ] ; }
p u b l i c v o i d add (T e ) {
int p = 0;
w h i l e ( p < t a i l l e && e . compareTo ( g e t ( p ) ) >0) p++;
f o r ( i n t i = t a i l l e ; i > p ; i ) t a b [ i ] = t a b [ i 1 ] ;
tab [ p ] = e ;
t a i l l e ++;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 164 / 379
Java Cours 5
s t a t i c boolean i s S o r t e d ( S t r i n g [ ] t ) {
f o r ( i n t i = 0 ; i < t . l e n g t h 1; i ++)
i f ( t [ i ] . l e n g t h () > t [ i +1]. l e n g t h ( ) ) return f a l s e ;
return true ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 165 / 379
Java Cours 5
s t a t i c boolean i s S o r t e d ( S t r i n g [ ] t ) {
f o r ( i n t i = 0 ; i < t . l e n g t h 1; i ++)
i f ( t [ i ] . l e n g t h () > t [ i +1]. l e n g t h ( ) ) return f a l s e ;
return true ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 166 / 379
Java Cours 5
s t a t i c boolean i s S o r t e d ( S t r i n g [ ] t ) {
f o r ( i n t i = 0 ; i < t . l e n g t h 1; i ++)
i f ( t [ i ] . l e n g t h () > t [ i +1]. l e n g t h ( ) ) return f a l s e ;
return true ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 167 / 379
Java Cours 5
Les iterateurs
p u b l i c i n t e r f a c e I t e r a b l e <T> { (Interface Java)
p u b l i c I t e r a t o r <T> i t e r a t o r ( ) ;
}
Exemple dutilisation :
s t a t i c <T> v o i d p r i n t C o l l e c t i o n ( I t e r a b l e <T> t ) {
I t e r a t o r <T> i t = t . i t e r a t o r ( ) ;
while ( i t . hasNext ( ) ) {
T e = i t . next ( ) ;
System . o u t . p r i n t l n ( e ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 168 / 379
Java Cours 5
i n t pos ;
S o r t e d A r r a y <T> s a ;
S o r t e d A r r a y I t e r a t o r ( S o r t e d A r r a y <T> s a ) {
pos = 0 ; t h i s . sa = sa ;
}
p u b l i c b o o le a n h a s N e x t ( ) { r e t u r n p o s < s a . t a i l l e ; }
public T next () {
T e = s a . g e t ( p o s ) ; p o s++;
return e ;
}
p u b l i c v o i d remove ( ) { . . . }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 169 / 379
Java Cours 5
c l a s s S o r t e d A r r a y <T e x t e n d s Comparable<T>>
i m p l e m e n t s I t e r a b l e <T> {
p r i v a t e Object [ ] tab ;
p u b l i c S o r t e d A r r a y ( ) { t a b = new O b j e c t [ 1 0 0 ] ; t a i l l e = 0; }
p u b l i c T g e t ( i n t p ) { r e t u r n (T) t a b [ p ] ; }
...
p u b l i c S o r t e d A r r a y I t e r a t o r <T> i t e r a t o r ( ) {
r e t u r n new S o r t e d A r r a y I t e r a t o r ( t h i s ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 170 / 379
Java Cours 5
Exemple dutilisation :
S o r t e d A r r a y <C a r t e > a = new S o r t e d A r r a y <C a r t e > ( ) ;
a . add ( new C a r t e ( 1 , 2 ) ) ;
a . add ( new C a r t e ( 2 , 1 ) ) ;
a . add ( new C a r t e ( 1 , 4 ) ) ;
Tools . p r i n t C o l l e c t i o n ( a ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 171 / 379
Java Cours 5
public A f i r s t ;
public B second ;
p u b l i c P a i r (A f , B s ) { f i r s t = f ; s e c o n d = s ; }
}
Exemple dutilisation de la classe Pair :
P a i r <I n t e g e r , I n t e g e r > p = new P a i r <I n t e g e r , I n t e g e r > ( 2 , 3 ) ;
P a i r <I n t e g e r , I n t e g e r > p = P a i r . m a k e P a i r ( 2 , 3 ) ;
P a i r <I n t e g e r , I n t e g e r > p = P a i r . m a k e P a i r ( 2 . 5 , 3 ) ;
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Types incompatibles : Pair<Integer, Integer> != Pair<Double, Integer>
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 172 / 379
Java Cours 5
Probleme :
c l a s s C a r t e implements Comparable<C a r t e > { . . . }
c l a s s J o l i e C a r t e extends Carte { . . . }
c l a s s S o r t e d A r r a y <T e x t e n d s Comparable<T>> { . . . }
S o r t e d A r r a y < J o l i e C a r t e > s = new S o r t e d A r r a y < J o l i e C a r t e > ( ) ;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Erreur : JolieCarte nimplemente pas Comparable<JolieCarte>
Solution :
c l a s s S o r t e d A r r a y <T e x t e n d s Comparable<? s u p e r T>> { . . . }
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 173 / 379
Java Cours 5
p u b l i c v o i d add (T e ) {
int p = 0;
w h i l e ( p < t a i l l e && e . compareTo ( g e t ( p ) ) >0) p++;
f o r ( i n t i = t a i l l e ; i > p ; i ) t a b [ i ] = t a b [ i 1 ] ;
tab [ p ] = e ;
t a i l l e ++;
}
...
}
c . addAll ( jc ) ;
::::::::::::::::
// Erreur : SortedArray<JolieCarte> nimplemente pas Iterable<Carte>
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 175 / 379
Java Cours 5
Solution :
c l a s s C a r t e implements Comparable<C a r t e > { . . . }
c l a s s J o l i e C a r t e extends Carte { . . . }
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 176 / 379
Java Cours 5
Resume
I Surcharge de methodes
I Covariance
I Boxing, unboxing et classes demballage
I Classes parametrees
I Condition sur les parametres
I Methodes parametrees
I Iterateurs
I ? super et ? extends
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 178 / 379
Java Cours 6
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 180 / 379
Java Cours 6
Les collections
Les methodes de linterface Collection<V> :
I boolean add(V e)
I boolean addAll(Collection<? extends V> c)
I void clear()
I boolean contains(Object o)
I boolean containsAll(Collection<?> c)
I boolean isEmpty()
I boolean remove(Object o)
I boolean removeAll(Collection<?> c)
I boolean retainAll(Collection<?> c)
I int size()
I Object[] toArray()
I <T> T[] toArray(T[] a)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 181 / 379
Java Cours 6
Les listes
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 182 / 379
Java Cours 6
Les listes
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 183 / 379
Java Cours 6
Les maps
Les methodes de linterface Map<K,V> :
I clear()
I boolean containsKey(Object key)
I boolean containsValue(Object value)
I Set<Map.Entry<K,V>> entrySet()
I V get(Object key)
I boolean isEmpty()
I Set<K> keySet()
I V put(K key, V value)
I void putAll(Map<? extends K,? extends V> m)
I V remove(Object key)
I int size()
I Collection<V> values()
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 184 / 379
Java Cours 6
Les maps
Quelques implementations de linterface Map<K,V> :
I HashMap : table de hachage
I LinkedHashMap : table de hachage + listes chainees
I TreeMap : arbre rouge-noir (Les elements doivent etre comparables)
Table de hachage :
0
John Smith Lisa Smith +1-555-8976
1
872
Lisa Smith John Smith +1-555-1234
873
998
Sam Doe Sam Doe +1-555-5030
999
Les maps
Quelques implementations de linterface Map<K,V> :
I HashMap : table de hachage
I LinkedHashMap : table de hachage + listes chainees
I TreeMap : arbre rouge-noir (Les elements doivent etre comparables)
Arbre rouge-noir :
Complexite
Rechercher O(log n)
Inserer O(log n)
Supprimer O(log n)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 185 / 379
Java Cours 6
Les maps
Quelques implementations de linterface Map<K,V> :
I HashMap : table de hachage
I LinkedHashMap : table de hachage + listes chainees
I TreeMap : arbre rouge-noir (Les elements doivent etre comparables)
Arbre rouge-noir et interface Comparator :
new Comparator<Carte>() {
...
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 185 / 379
Java Cours 6
Les ensembles
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 186 / 379
Java Cours 6
Les files
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 187 / 379
Java Cours 6
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 188 / 379
Java Cours 6
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 189 / 379
Java Cours 6
Les iterateurs
Sortie : 1 2 1
C o l l e c t i o n <I n t e g e r > l = new HashSet<I n t e g e r > ( ) ;
l . add ( 1 ) ; l . add ( 2 ) ; l . add ( 1 ) ;
for ( Integer i : l )
System . o u t . p r i n t ( i+ ) ;
System . o u t . p r i n t l n ( ) ;
Sortie : 1 2
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 190 / 379
Java Cours 6
Les iterateurs
Sortie :
3 4 2
aaa toto bb
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 191 / 379
Java Cours 6
Exceptions
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 192 / 379
Java Cours 6
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 193 / 379
Java Cours 6
i n t number ;
p u b l i c MyException ( i n t number ) {
t h i s . number = number ;
}
p u b l i c S t r i n g getMessage ( ) {
r e t u r n E r r e u r numero +number ;
}
}
Convention de nommage : quelquechoseException
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 194 / 379
Java Cours 6
La syntaxe try/catch
public s t a t i c void t e s t ( i n t i ) {
System . o u t . p r i n t ( A ) ;
try {
System . o u t . p r i n t l n ( B ) ;
i f ( i > 1 2 ) throw new MyException ( i ) ;
System . o u t . p r i n t ( C ) ;
} catch ( MyException e ) {
System . o u t . p r i n t l n ( e ) ;
}
System . o u t . p r i n t l n ( D ) ;
}
test(11) : test(13) :
A B A B
C D MyException: Erreur numero 13
D
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 195 / 379
Java Cours 6
p u b l i c s t a t i c v o i d method1 ( i n t i ) t h r o w s M yE x ce pt i on {
method2 ( i ) ;
}
p u b l i c s t a t i c v o i d method2 ( i n t i ) t h r o w s M yE x ce pt i on {
i f ( i >12) throw new M y E xc ep t i o n ( i ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g [ ] ) {
t r y { method1 ( i ) ; }
c a t c h ( M y E x c e pt i o n e ) { e . p r i n t S t a c k T r a c e ( ) ; }
}
}
Une methode doit indiquer toutes les exceptions quelle peut generer et
quelle na pas traitees avec un bloc try/catch
(Partiellement vrai => voir plus loin)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 196 / 379
Java Cours 6
p u b l i c s t a t i c v o i d method2 ( i n t i ) t h r o w s M y E x c e p t i o n {
i f ( i >12) throw new M y E x c e p t i o n ( i ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g [ ] ) {
t r y { method1 ( i ) ; }
catch ( MyException e ) { e . p r i n t S t a c k T r a c e ( ) ; }
}
}
La classe RuntimeException
Une methode doit indiquer toutes les exceptions quelle peut generer
sauf si lexception etend la classe RuntimeException
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 198 / 379
Java Cours 6
}
diviser(12,0) : diviser(null,12) :
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 199 / 379
Java Cours 6
Le mot-cle finally
public s t a t i c void r e a d F i l e ( S t r i n g f i l e ) {
try {
F i l e R e a d e r f = new F i l e R e a d e r ( f i l e ) ;
(le constructeur de FileReader peut declencher une FileNotFoundException)
try {
i n t ch = f . r e a d ( ) ; (peut declencher une IOException)
w h i l e ( ch !=1) {
System . o u t . p r i n t l n ( ch ) ;
ch = f . r e a d ( ) ; (peut declencher une IOException)
}
} f i n a l l y { (a faire dans tous les cas)
f . close ();
}
} catch ( IOException e ) { e . p r i n t S t a c k T r a c e ( ) ; }
}
Le mot-cle finally
public s t a t i c void r e a d F i l e ( S t r i n g f i l e ) {
try {
F i l e R e a d e r f = new F i l e R e a d e r ( f i l e ) ;
(le constructeur de FileReader peut declencher une FileNotFoundException)
try {
i n t ch = f . r e a d ( ) ; (peut declencher une IOException)
w h i l e ( ch !=1) {
System . o u t . p r i n t l n ( ch ) ;
ch = f . r e a d ( ) ; (peut declencher une IOException)
}
} f i n a l l y { (a faire dans tous les cas)
f . close ();
}
}
catch ( FileNotFoundE xcept ion e ) {
System . o u t . p r i n t l n ( F i c h i e r + f i l e + i n t r o u v a b l e ) ;
}
catch ( IOException e ) { e . p r i n t S t a c k T r a c e ( ) ; }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 201 / 379
Java Cours 6
Exemple La pile
p u b l i c c l a s s P i l e <T> {
private Object [ ] p i l e ;
private int t a i l l e ;
p u b l i c v o i d e m p i l e r (T o ) throws P i l e P l e i n e E x c e p t i o n {
i f ( t a i l l e == p i l e . l e n g t h )
t h r o w new P i l e P l e i n e E x c e p t i o n ( ) ;
p i l e [ t a i l l e ] = o ; t a i l l e ++;
}
p u b l i c T d e p i l e r ( ) throws P i l e V i d e E x c e p t i o n {
i f ( t a i l l e == 0 ) t h r o w new P i l e V i d e E x c e p t i o n ( ) ;
t a i l l e ; T e = (T) p i l e [ t a i l l e ] ; p i l e [ t a i l l e ]= n u l l ;
r e t u r n (T) p i l e [ t a i l l e ] ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 202 / 379
Java Cours 6
Exemple La pile
p u b l i c c l a s s P i l e E x c e p t i o n extends Exception {
p u b l i c P i l e E x c e p t i o n ( S t r i n g msg ) { s u p e r ( msg ) ; }
}
p u b l i c c l a s s P i l e P l e i n e E x c e p t i o n extends P i l e E x c e p t i o n {
p u b l i c P i l e P l e i n e E x c e p t i o n ( ) { super ( P i l e p l e i n e ) ; }
}
p u b l i c c l a s s P i l e V i d e E x c e p t i o n extends P i l e E x c e p t i o n {
p u b l i c P i l e V i d e E x c e p t i o n ( ) { super ( P i l e v i d e ) ; }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 203 / 379
Java Cours 6
Exemple La pile
Exemples dutilisation :
try { try {
p . empiler (1); p . empiler (1);
p . empiler (2); p. depiler ();
p . empiler (3); p. depiler ();
} catch ( P i l e E x c e p t i o n e ) { } catch ( P i l e E x c e p t i o n e ) {
e . printStackTrace (); e . printStackTrace ();
} }
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 204 / 379
Java Cours 6
La classe Throwable
Throwable
Error Exception
RuntimeException
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 205 / 379
Java Cours 6
La classe Throwable
La classe Throwable fournit les methodes suivantes :
Throwable
Throwable()
Throwable(message:String)
Throwable(message:String, cause:Throwable)
Throwable(cause:Throwable)
fillInStackTrace():Throwable
getCause():Throwable
getLocalizedMessage():String
getMessage():String
getStackTrace():StackTraceElement[]
initCause(cause:Throwable):Throwable
printStackTrace():void
printStackTrace(s:PrintStream):void
printStackTrace(s:PrintWriter):void
setStackTrace(stackTrace:StackTraceElement[]):void
toString():String
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 206 / 379
Java Cours 6
Resume
I Structures de donnees
I Mecanisme de gestion des exceptions
I La classe Exception
I Definir un type dexception
I Mot-cle throw
I Utilisations de try, catch et finally
I Mot-cle throws
I Les classes RuntimeException, Throwable et Error
I Les exceptions de Java
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 208 / 379
Java Cours 7
UML
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 209 / 379
Java Cours 7
Diagramme de classes
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 210 / 379
Java Cours 7
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 211 / 379
Java Cours 7
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 212 / 379
Java Cours 7
Visibilite
- : prive (private)
# : protege (protected)
+ : publique (public)
: non-publique (default)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 213 / 379
Java Cours 7
Extensions et implementations
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 214 / 379
Java Cours 7
Implementation multiples
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 215 / 379
Java Cours 7
Dependances
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 216 / 379
Java Cours 7
Associations
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 217 / 379
Java Cours 7
Navigation
Multiplicite
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 219 / 379
Java Cours 7
Multiplicite
Notation Signification
0..1 Zero ou un
1 un uniquement
0..* (ou *) Zero ou plus
1..* Un ou plus
n Seulement n
0..n Zero a n
1..n Un a n
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 220 / 379
Java Cours 7
Agregations
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 221 / 379
Java Cours 7
Compositions
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 223 / 379
Java Cours 7
interface
Formule
+ afficher() : String
+ evaluer() : double
*
OperateurNAire
Variable + f : Formule[]
- name : String + OperateurNAire(f : Formule[])
- value : double + OperateurNAire(f1 : Formule, f2 : Formule)
+ afficher() : String
+ afficher() : String
+ evaluer() : double
+ evaluer() : double
# symbole() : char
+ set(v : double)
# evaluerBinaire(a : double, b : double) : double
# evaluerVide() : double
Produit Somme
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 224 / 379
Java Cours 7
JPanel
ShapePanel
- shapes : ArrayList<Shape>
+ ShapePanel()
+ add(p : Shape)
# paintComponent(g : Graphics)
*
interface
Shape
+ paint(g : Graphics)
+ getNumberOfPoints() : int
+ getPoint(i : int) : Point
AbstractShape Decorator
- points : List<Point> # decoratedShape : Shape
+ AbstractShape() + Decorator(decoratedShape : Shape)
# addPoint(p : Point) + paint(g : Graphics)
+ getPoint(i : int) : Point + getNumberOfPoints() : int
+ getNumberOfPoints() : int + getPoint(i : int) : Point
+ paint(g : Graphics) # paintDecoration(g : Graphics)
BorderDecorator
Polygon Rectangle *
- radius : int
- color : Color - color : Color
Point - color : Color
+ Polygon(color : Color, points : Point[]) + Rectangle(p0 : Point, p1 : Point, color : Color)
+ BorderDecorator(s : Shape, radius : int, color : Color)
+ paint(g : Graphics) + paint(g : Graphics)
# paintDecoration(g : Graphics)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 225 / 379
Java Cours 7
Les objectifs :
I Faciliter le developpement et levolution des applications ;
I Permettre le travail en equipe ;
I Augmenter la qualite des logiciels (moins de bugs).
Solutions proposees :
I Decoupler (separer) les parties des projets ;
I Limiter (et localiser) les modifications lors des evolutions ;
I Reutiliser facilement du code.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 226 / 379
Java Cours 7
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 227 / 379
Java Cours 7
Les evolutions
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 228 / 379
Java Cours 7
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 229 / 379
Java Cours 7
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 230 / 379
Java Cours 7
Pourquoi ?
I Si une classe a plusieurs responsabilites, elles sont couplees
I Dans ce cas, la modification dune des responsabilites necessite de :
I tester a nouveau limplementation des autres responsabilites
I modifier potentiellement les autres responsabilites
I deployer a nouveau les autres responsabilites
I Donc, vous risquez de :
I introduire des bugs
I rendre moins locales les modifications
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 231 / 379
Java Cours 7
Avantages :
I Diminution de la complexite du code
I Amelioration de la lisibilite du code
I Meilleure organisation du code
I Modification locale lors des evolutions
I Augmentation de la fiabilite
I Les classes ont plus de chance detre reutilisables
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 231 / 379
Java Cours 7
Rectangle
GraphicalInterface
GraphicalApplication
GraphicalRectangle
GeometryApplication
~ GraphicalRectangle()
Separation des responsabilites : + draw()
GeometricRectangle
GraphicalInterface
~ GeometricRectangle()
+ getArea() : double
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 232 / 379
Java Cours 7
Principe :
Signification :
Vous devez pouvoir ajouter une nouvelle fonctionnalite :
I en ajoutant des classes (Ouvert pour lextension)
I sans modifier le code existant (ferme a la modification)
Avantages :
I Le code existant nest pas modifie augmentation de la fiabilite
I Les classes ont plus de chance detre reutilisables
I Simplification de lajout de nouvelles fonctionnalites
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 233 / 379
Java Cours 7
class GraphicTools {
s t a t i c void drawRectangle ( Rectangle r ) { . . . }
s t a t i c void drawCircle ( C i r c l e c ) { . . . }
s t a t i c v o i d d r a w Sh ap es ( O b j e c t [ ] s h a p e s ) {
for ( Object o : shapes ) {
i f (o instanceof Rectangle ) {
Rectangle r = ( Rectangle )o ;
drawRectangle ( r ) ;
}
else i f (o instanceof C i r c l e ) {
Circle c = ( Circle )o ;
drawCircle (c );
}
}
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 234 / 379
Java Cours 7
c l a s s R e c t a n g l e implements Shape {
p u b l i c P o i n t p1 , p2 ;
v o i d draw ( ) { . . . }
}
c l a s s C i r c l e implements Shape {
public Point c ; public int r a d i u s ;
v o i d draw ( ) { . . . }
}
class GraphicTools {
s t a t i c v o i d d r a w S h ap es ( Shape [ ] s h a p e s ) {
f o r ( Shape s : s h a p e s )
s . draw ( ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 235 / 379
Java Cours 7
public v o i d s e t W i d t h ( double w) { t h i s . w = w ; }
public v o i d s e t H e i g h t ( double h ) { t h i s . h = h ; }
public double g e t W i d t h ( ) { r e t u r n w ; }
public double g e t H e i g h t ( ) { r e t u r n h ; }
public double g e t A r e a ( ) { r e t u r n wh ; }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 237 / 379
Java Cours 7
p u b l i c c l a s s S q u a r e extends R e c t a n g l e {
p u b l i c v o i d s e t W i d t h ( double w) {
super . s e t W i d t h (w ) ;
super . s e t H e i g h t (w ) ;
}
p u b l i c v o i d s e t H e i g h t ( double h ) {
super . s e t W i d t h ( h ) ;
super . s e t H e i g h t ( h ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 238 / 379
Java Cours 7
Une solution :
public abstract class RectangularShape {
p u b l i c a b s t r a c t d ouble g e t W i d t h ( ) ;
p u b l i c a b s t r a c t d ouble g e t H e i g h t ( ) ;
p u b l i c do u ble g e t A r e a ( ) { r e t u r n g e t W i d t h ( ) g e t H e i g h t ( ) ; }
}
public v o i d s e t W i d t h ( double w) { t h i s . w = w ; }
public v o i d s e t H e i g h t ( double h ) { t h i s . h = h ; }
public do u ble g e t W i d t h ( ) { r e t u r n w ; }
public do u ble g e t H e i g h t ( ) { r e t u r n h ; }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 240 / 379
Java Cours 7
}
Utilisation :
public void t e s t R e c t a n g l e ( Rectangle r ) {
r . setWidth ( 2 ) ; r . setHeight ( 3 ) ;
i f ( r . g e t A r e a ( ) ! = 3 2 ) System . o u t . p r i n t l n ( j a m a i s ! ) ;
}
p u b l i c void t e s t S q u a r e ( Square s ) {
s . setSideLength (2);
i f ( s . g e t A r e a ( ) ! = 2 2 ) System . o u t . p r i n t l n ( j a m a i s ! ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 241 / 379
Java Cours 8
Plan du cours
I Patrons de conception
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 242 / 379
Java Cours 8
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 243 / 379
Java Cours 8
Signification et objectifs :
Ent reprise
- employers : ArrayList <Employer>
+ Ent reprise()
+ employer(e : Employer)
+ licencier(e : Employer)
+ produire()
*
int erf ace
Employer
+ t ravailler()
+ manger()
+ dormir()
Homme Femme
+ Homme() + Femme()
+ t ravailler() + t ravailler()
+ manger() + manger()
+ dormir() + dormir()
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 245 / 379
Java Cours 8
p u b l i c i n t e r f a c e Employer {
void t r a v a i l l e r ( ) ;
v o i d manger ( ) ;
void dormir ( ) ;
}
p u b l i c c l a s s Femme implements E m p l o y e r {
public void t r a v a i l l e r () {
System . o u t . p r i n t l n ( Je s u i s une femme e t j e t r a v a i l l e ) ;
}
p u b l i c v o i d manger ( ) {
System . o u t . p r i n t l n ( Je s u i s une femme e t j e mange ) ;
}
public void dormir () {
System . o u t . p r i n t l n ( Je s u i s une femme e t j e d o r s ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 246 / 379
Java Cours 8
public Entreprise () {
e m p l o y e r s = new A r r a y L i s t <Employer > ( ) ;
}
p u b l i c void l i c e n c i e r ( Employer e ) {
e m p l o y e r s . remove ( e ) ;
}
public void p r o d u i r e () {
f o r ( Employer e : employers )
e. travailler ();
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 247 / 379
Java Cours 8
Ent reprise
- employers : ArrayList <Employer>
+ Ent reprise()
+ employer(e : Employer)
+ licencier(e : Employer)
+ produire()
*
int erf ace
Employer
+ t ravailler()
+ manger()
+ dormir()
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 248 / 379
Java Cours 8
p u b l i c c l a s s Robot implements E m p l o y e r {
public void t r a v a i l l e r () {
System . o u t . p r i n t l n ( j e t r a v a i l l e ) ;
}
p u b l i c v o i d manger ( ) {
System . o u t . p r i n t l n ( j e ne s a i s p a s l e f a i r e ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 249 / 379
Java Cours 8
Ent reprise
- employers : ArrayList <Employer>
+ Ent reprise()
+ employer(e : Employer)
+ licencier(e : Employer)
+ produire()
*
int erf ace
int erf ace
Humain
Employer
+ manger()
+ t ravailler()
+ dormir()
Femme Homme
Robot
+ Femme() + Homme()
+ Robot () + t ravailler() + t ravailler()
+ t ravailler() + manger() + manger()
+ dormir() + dormir()
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 250 / 379
Java Cours 8
Signification et objectifs :
ShapePanel
- shapes : ArrayList <Shape>
+ ShapePanel()
+ addShape(s : Shape)
*
int erf ace
Shape
+ draw(g : Graphics)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 252 / 379
Java Cours 8
ShapePanel
- shapes : ArrayList <Shape>
+ ShapePanel()
+ addShape(s : Shape)
*
int erf ace
FileLoader
Shape
+ FileLoader()
+ draw(g : Graphics)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 253 / 379
Java Cours 8
...
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 254 / 379
Java Cours 8
p u b l i c v o i d l o a d F i l e ( S h a p e P a n e l p , S t r i n g name ) {
S c a n n e r s = new S c a n n e r ( name ) ;
while ( s . hasNext ( ) ) {
switch ( s . n e x t I n t ( ) ) {
case 0 :
l oa dC i r cl e (p , s ) ;
case 1 :
loadRectangle (p , s ) ;
}
}
s . nextInt ();
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 255 / 379
Java Cours 8
*
int erf ace
Shape
+ draw(g : Graphics)
NewCircle
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 256 / 379
Java Cours 8
*
int erf ace
FileLoader
Shape
+ FileLoader()
+ draw(g : Graphics)
inst ancie
NewCircle
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 257 / 379
Java Cours 8
use
*
ShapeFact ory
int erf ace
Shape
+ ShapeFact ory()
+ creat eCircle(p : Point , radius : int ) : Shape
+ draw(g : Graphics)
+ creat eRect angle(p1 : Point , p2 : Point ) : Shape
inst ancie
NewCircle
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 259 / 379
Java Cours 8
public c l a s s ShapeFactory {
p u b l i c Shape c r e a t e C i r c l e ( P o i n t p , i n t r a d i u s ) {
r e t u r n new N e w C i r c l e ( p , r a d i u s ) ;
}
p u b l i c Shape c r e a t e R e c t a n g l e ( P o i n t p1 , P o i n t p2 ) {
r e t u r n new R e c t a n g l e ( p1 , p2 ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 260 / 379
Java Cours 8
p r i v a t e S h a p e F a c t o r y f a c t o r y = new S h a p e F a c t o r y ( ) ;
p u b l i c Shape c r e a t e R e c t a n g l e ( P o i n t p1 , P o i n t p2 ) {
r e t u r n new R e c t a n g l e ( p1 , p2 ) ;
}
}
p u b l i c Shape c r e a t e R e c t a n g l e ( P o i n t p1 , P o i n t p2 ) {
r e t u r n new R e c t a n g l e ( p1 , p2 ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 262 / 379
Java Cours 8
FileLoader
- f act ory : Abst ract ShapeFact ory
+ FileLoader(f act ory : Abst ract ShapeFact ory)
+ loadCircle(p : ShapePanel, s : Scanner)
+ loadRect angle(p : ShapePanel, s : Scanner)
+ loadFile(p : ShapePanel, name : St ring)
use
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 263 / 379
Java Cours 8
ShapeFactoryVersion2 ShapeFactoryVersion1
+ ShapeFactoryVersion2() + ShapeFactoryVersion1()
+ createCircle(p : Point, radius : int) : Circle + createCircle(p : Point, radius : int) : Circle
+ createRectangle(p1 : Point, p2 : Point) : Rectangle + createRectangle(p1 : Point, p2 : Point) : Rectangle
Rectangle Circle
- p1 : Point - c : Point
instancie - p2 : Point - radius : int
+ Rectangle(p1 : Point, p2 : Point) + Circle(c : Point, radius : int)
+ draw(g : Graphics) + draw(g : Graphics)
NewCircle
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 264 / 379
Java Cours 8
interface
ShapePanel
AbstractShapeFactory
- shapes : ArrayList<Shape>
+ ShapePanel()
+ createCircle(p : Point, radius : int) : Circle
+ addShape(s : Shape)
+ createRectangle(p1 : Point, p2 : Point) : Rectangle
*
ShapeFactoryVersion1 ShapeFactoryVersion2
interface
Shape
+ ShapeFactoryVersion1() + ShapeFactoryVersion2()
+ createCircle(p : Point, radius : int) : Circle + createCircle(p : Point, radius : int) : Circle
+ draw(g : Graphics)
+ createRectangle(p1 : Point, p2 : Point) : Rectangle + createRectangle(p1 : Point, p2 : Point) : Rectangle
Circle Rectangle
- c : Point - p1 : Point
- radius : int - p2 : Point instancie
+ Circle(c : Point, radius : int) + Rectangle(p1 : Point, p2 : Point)
+ draw(g : Graphics) + draw(g : Graphics)
NewCircle
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 265 / 379
Java Cours 8
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 266 / 379
Java Cours 8
p r i v a t e f i n a l A r r a y L i s t <Shape> s h a p e s ;
p u b l i c ShapePanel ( ) {
s h a p e s = new A r r a y L i s t <Shape > ( ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 267 / 379
Java Cours 8
add shape
*
int erf ace
ShapeFact oryVersion1 ShapeFact oryVersion2
Shape
NewCircle
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 269 / 379
Java Cours 8
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 270 / 379
Java Cours 8
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 271 / 379
Java Cours 8
I Adaptateur (Adapter)
I Pont (Bridge)
I Objet composite (Composite)
I Decorateur (Decorator)
I Facade (Facade)
I Poids-mouche ou poids-plume (Flyweight)
I Proxy (Proxy)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 272 / 379
Java Cours 8
Fabrique abstraite
Utilisateur
utilise utilise
interface
FabriqueAbstraite utilise
interface
ProduitAbstraitB
creerProduitA() : ProduitAbstraitA
creerProduitB() : ProduitAbstraitB
FabriqueConcrete2 FabriqueConcrete1
interface
ProduitAbstraitA
creerProduitA() : ProduitAbstraitA creerProduitA() : ProduitAbstraitA
creerProduitB() : ProduitAbstraitB creerProduitB() : ProduitAbstraitB
instancie instancie
instancie instancie
Adapteur
Utilisateur
+ Utilisateur()
utilise
interface
InterfaceCible
I methodA(a,b) method1(b,a)
I methodB(b,a) method2(b,a)
ClasseExistante
+ ClasseExistante()
+ method1(b : int, a : String)
+ method2(b : int, a : String)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 275 / 379
Java Cours 8
Adapteur
Utilisateur
+ Utilisateur()
public class Adapteur implements InterfaceCible {
utilise
ClasseExistante classeExistante;
interface
InterfaceCible
public Adapteur() {
+ methodA(a : String, b : int)
+ methodB(b : int, a : String) classeExistante = new ClasseExistante();
}
Adapteur
~ classeExistante : ClasseExistante
public void methodA(String a, int b) {
+ Adapteur() classeExistante.method1(b, a);
+ methodA(a : String, b : int)
+ methodB(b : int, a : String) }
dlgue
public void methodB(int b, String a) {
ClasseExistante
classeExistante.method2(b, a);
}
+ ClasseExistante()
+ method1(b : int, a : String)
}
+ method2(b : int, a : String)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 276 / 379
Java Cours 8
Proxy
Client
+ Client()
utilise
private int v;
+ set(v : int)
+ get() : int
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 277 / 379
Java Cours 8
Proxy
Client
+ Client()
public class ConteneurNonModifiable
utilise
implements IConteneur {
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 278 / 379
Java Cours 8
Patron de methode
Exemple dutilisation du patron de methode :
OperateurNAire
evaluer() : double
evaluerBinaire(a : int, b : int) : int
evaluerVide() : int
Produit Somme
int main(void) {
function(2, callbackImpl1);
function(4, callbackImpl2);
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 280 / 379
Java Cours 8
class MaClasse {
I Les classes qui gerent les flux sont dans le package java.io
I Certaines classes permettent de lire ou decrire dans ou vers differents
supports (fichier, chane de caracteres, tubes, socket, etc.)
I Dautres permettent de faire des traitements sur les donnees (filtres)
I InputStream, OutputStream, Reader, Writer = classes abstraites
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 283 / 379
Java Cours 9
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 284 / 379
Java Cours 9
I ...
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 285 / 379
Java Cours 9
Exemple :
byte [ ] b y t e s = { 1 0 , 1 1 , 1 2 } ;
I n p u t S t r e a m i s = new B y t e A r r a y I n p u t S t r e a m ( b y t e s ) ;
while ( i s . a v a i l a b l e ( ) > 0)
System . o u t . p r i n t l n ( i s . r e a d ( ) ) ;
Sortie :
10
11
12
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 286 / 379
Java Cours 9
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 287 / 379
Java Cours 9
Sortie :
97
98
99
10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 288 / 379
Java Cours 9
Sortie :
Contenu de abc.txt :
97 98 99 10
Contenu de truc.txt :
116 114 117 99 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 290 / 379
Java Cours 9
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 291 / 379
Java Cours 9
Exemple :
Reader r = new S t r i n g R e a d e r ( t o t o ) ;
int c ;
w h i l e ( ( c = r . r e a d ())!= 1)
System . o u t . p r i n t l n ( ( c h a r ) c ) ;
Sortie :
t
o
t
o
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 292 / 379
Java Cours 9
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 293 / 379
Java Cours 9
String ligne ;
while (( l i g n e = bufferedReader . readLine ())!= n u l l )
System . o u t . p r i n t l n ( l i g n e ) ;
Sortie :
L1
L2
L3
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 294 / 379
Java Cours 9
Autre exemple :
String ligne ;
while (( l i g n e = lineNumberReader . readLine ())!= n u l l ) {
System . o u t . p r i n t l n ( l i n e N u m b e r R e a d e r . g e t L i n e N u m b e r ()+ : ) ;
System . o u t . p r i n t l n ( l i g n e ) ;
}
Sortie :
1 : L1
2 : L2
3 : L3
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 295 / 379
Java Cours 9
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 296 / 379
Java Cours 9
I ...
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 297 / 379
Java Cours 9
Exemple :
B y t e A r r a y O u t p u t S t r e a m o u t p u t = new B y t e A r r a y O u t p u t S t r e a m ( ) ;
output . w r i t e ( 9 7 ) ;
output . w r i t e ( 9 8 ) ;
output . w r i t e ( 9 9 ) ;
byte [ ] b y t e s = o u t p u t . t o B y t e A r r a y ( ) ;
f o r ( byte b : b y t e s )
System . o u t . p r i n t l n ( b ) ;
System . o u t . p r i n t l n ( o u t p u t ) ;
Sortie :
97
98
99
abc
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 298 / 379
Java Cours 9
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 299 / 379
Java Cours 9
I n p u t S t r e a m i n p u t = new B u f f e r e d I n p u t S t r e a m (
new F i l e I n p u t S t r e a m ( o r i g i n a l . p d f )
);
OutputStream o u t p u t = new B u f f e r e d O u t p u t S t r e a m (
new F i l e O u t p u t S t r e a m ( c o p i e . p d f )
);
input . close ( ) ;
output . c l o s e ( ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 300 / 379
Java Cours 9
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 301 / 379
Java Cours 9
input . close ( ) ;
output . c l o s e ( ) ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 302 / 379
Java Cours 9
P r i n t W r i t e r o u t p u t = new P r i n t W r i t e r ( t e x t e . t x t ) ;
o u t p u t . p r i n t l n ( b o n j o u r ) ;
o u t p u t . p r i n t f ( %d %d %d\n , 1 2 , 1 4 , 1 5 ) ;
o u t p u t . p r i n t f ( %s %.2 f %04d\n , aaaa , 1 2 . 4 4 5 5 7 , 4 5 ) ;
output . c l o s e ( ) ;
bonjour
12 -- 14 -- 15
aaaa 12,45 0045
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 303 / 379
Java Cours 9
System.{out,in,err}
Dans la classe System :
I public static final InputStream in : entree standard
I public static final PrintStream out : sortie standard
I public static final PrintStream err : sortie derreur
I public static Console console() : retourne la console
Exemple :
System . o u t . p r i n t l n ( e c r i t u r e s u r l a s o r t i e s t a n d a r d ) ;
System . e r r . p r i n t l n ( e c r i t u r e s u r l a s o r t i e d e r r e u r ) ;
B u f f e r e d R e a d e r i n p u t = new B u f f e r e d R e a d e r (
new I n p u t S t r e a m R e a d e r ( System . i n ) ) ;
String s = input . readLine ( ) ;
w h i l e ( s != n u l l ) {
System . o u t . p r i n t l n ( s ) ;
s = input . readLine ( ) ;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 304 / 379
Java Cours 9
System.console()
Dans la classe Console :
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 305 / 379
Java Cours 9
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 307 / 379
Java Cours 9
Java et le multitache
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 308 / 379
Java Cours 9
Linterface Runnable
p u b l i c c l a s s Compteur implements R u n n a b l e {
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 309 / 379
Java Cours 9
La classe Thread
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 310 / 379
Java Cours 9
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Thread t h 1 = new Thread ( new Compteur ( ) ) ;
th1 . s t a r t ( ) ;
Thread t h 2 = new Thread ( new Compteur ( ) ) ;
th2 . s t a r t ( ) ;
}
}
Sortie :
0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 311 / 379
Java Cours 9
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Thread t h 1 = new Compteur ( ) ;
th1 . s t a r t ( ) ;
Thread t h 2 = new Compteur ( ) ;
th2 . s t a r t ( ) ;
}
}
Sortie :
0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 312 / 379
Java Cours 9
La classe Thread
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 313 / 379
Java Cours 9
Letat dinterruption
p u b l i c c l a s s Compteur e x t e n d s Thread {
p u b l i c void run ( ) {
int cpt = 0;
w h i l e ( ! i n t e r r u p t e d ( ) ) c p t ++;
System . o u t . p r i n t l n ( c p t ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 314 / 379
Java Cours 9
Letat dinterruption
Utilisation du compteur :
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Thread t h 1 = new Compteur ( ) ;
th1 . s t a r t ( ) ; . . . ; th1 . i n t e r r u p t ( ) ;
}
}
Compteur qui compte moins vite :
p u b l i c c l a s s Compteur e x t e n d s Thread {
p u b l i c void run ( ) {
int cpt = 0;
while ( ! i n t e r r u p t e d ()) {
c p t ++;
Thread . s l e e p ( 1 0 0 ) ;
// Probleme : interruption pendant que je dors !
}
System . o u t . p r i n t l n ( c p t ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 315 / 379
Java Cours 9
Letat dinterruption
Utilisation du compteur :
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Thread t h 1 = new Compteur ( ) ;
th1 . s t a r t ( ) ; . . . ; th1 . i n t e r r u p t ( ) ;
}
}
Compteur qui compte moins vite :
p u b l i c c l a s s Compteur e x t e n d s Thread {
p u b l i c void run ( ) {
int cpt = 0;
while ( ! i n t e r r u p t e d ()) {
c p t ++;
t r y { Thread . s l e e p ( 1 0 0 ) ; }
catch ( InterruptedException e ) { break ; }
}
System . o u t . p r i n t l n ( c p t ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 316 / 379
Java Cours 9
Letat dinterruption
Thread qui interrompt un autre thread apres un certain temps :
p u b l i c c l a s s S t o p p e r e x t e n d s Thread {
Thread t h r e a d ;
int timeLimit ;
p u b l i c S t o p p e r ( Thread t h r e a d , i n t t i m e L i m i t ) {
this . thread = thread ;
this . timeLimit = timeLimit ;
}
void run ( ) {
try {
sleep ( timeLimit );
} c a t c h ( I n t e r r u p t e d E x c e p t i o n ex ) {
return ;
}
thread . i n t e r r u p t ( ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 317 / 379
Java Cours 9
Letat dinterruption
Exemple dutilisation des classes precedentes :
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Thread t h 1 = new Compteur ( ) ;
th1 . s t a r t ( ) ;
Thread t h 2 = new S t o p p e r ( th1 , 3 5 0 ) ;
th2 . s t a r t ( ) ;
}
Execution :
4
1 2 3 4
Compteur temps
0 100 200 300
interrupt()
1
Stopper temps
0 350
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 318 / 379
Java Cours 9
Probleme de synchronisation
Une pile dentiers avec une temporisation lors des empilements :
public class PileLente {
p r i v a t e i n t [ ] tab ; p r i v a t e i n t s i z e ;
public PileLente () {
t a b = new i n t [ 1 0 0 0 ] ; s i z e = 0 ;
}
v o i d push ( i n t p ) throws I n t e r r u p t e d E x c e p t i o n {
i n t s = s i z e ; tab [ s ] = p ;
Thread . s l e e p ( 1 0 0 ) ;
size = s + 1;
}
i n t pop ( ) { s i z e ; r e t u r n t a b [ s i z e ] ; }
int s i z e () { return s i z e ; }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 319 / 379
Java Cours 9
Synchronisation
Un thread qui empile 10 entiers :
p u b l i c c l a s s E m p i l e u r e x t e n d s Thread {
private PileLente p i l e ;
p u b l i c void run ( ) {
f o r ( i n t i = 0 ; i < 1 0 ; i ++)
try {
p i l e . push ( i ) ;
} c a t c h ( I n t e r r u p t e d E x c e p t i o n ex ) {
return ;
}
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 320 / 379
Java Cours 9
Synchronisation
Execution en parallele de deux empileurs sur une meme pile :
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
throws I n t e r r u p t e d E x c e p t i o n {
P i l e L e n t e p i l e = new P i l e L e n t e ( ) ;
Thread t h 1 = new E m p i l e u r ( p i l e ) ;
Thread t h 2 = new E m p i l e u r ( p i l e ) ;
th1 . s t a r t ( ) ;
th2 . s t a r t ( ) ;
t h 1 . j o i n ( ) ; // On attend la fin du thread 1
t h 2 . j o i n ( ) ; // On attend la fin du thread 2
w h i l e ( p i l e . s i z e () >0) {
System . o u t . p r i n t ( p i l e . pop ()+ ) ;
}
Sortie :
9 8 7 6 5 4 3 2 1 0
Question : Ou sont passes les 10 elements manquants ?
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 321 / 379
Java Cours 9
Synchronisation
Thread 1 Thread 2
int s = size ; (s = x)
tab[s] = p ;
sleep(100) ; int s = size ; (s = x)
tab[s] = p ;
sleep(100) ;
size = s + 1 ; (size = x + 1)
size = s + 1 ; (size = x + 1)
Probleme : Deux entiers ont ete empiles et la pile et la taille de la pile est
passee de x a x + 1
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 322 / 379
Java Cours 9
Le mot-cle Synchronized
Interdire que deux invocations sur une meme instance sentremelent :
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 324 / 379
Java Cours 9
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S w i n g U t i l i t i e s . i n v o k e L a t e r ( new R u n n a b l e ( ) {
p u b l i c void run ( ) {
creerEtMontrer ( ) ;
}
});
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 325 / 379
Java Cours 10
JavaDoc
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 326 / 379
Java Cours 10
Exemple de JavaDoc
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 327 / 379
Java Cours 10
Exemple de JavaDoc
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 328 / 379
Java Cours 10
Exemple de JavaDoc
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 329 / 379
Java Cours 10
/**
* Cette classe contient une methode additionner.
*/
public class Truc {
/**
* Retourne la somme des deux valeurs
* specifiees en parametre.
* @param a la premiere valeur.
* @param b la deuxieme valeur.
* @return la somme des deux valeurs.
*/
public int additionner(int a, int b) {
return a+b;
}
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 330 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 331 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 332 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 333 / 379
Java Cours 10
/**
* Une classe truc qui permet dadditionner
* Par exemple :
* <pre>
* Truc truc = new Truc();
* int b = truc.additionner(3, 6);
* </pre>
*
* @author Bertrand Estellon
* @version 1.0
* @see java.lang.String
*/
public class Truc {
....
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 334 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 335 / 379
Java Cours 10
/**
* La coordonnee Y.
*
* @see #getPoint()
*/
public int y = 40;
....
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 336 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 337 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 339 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 340 / 379
Java Cours 10
Generation de la documentation
I Avec Eclipse ou NetBeans :
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 342 / 379
Java Cours 10
Heritage :
extends
implements
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 343 / 379
Java Cours 10
Revision Classes
class UneClasse {
int field1;
String field2;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 344 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 345 / 379
Java Cours 10
Revision Interfaces
interface UneInterface {
String nom;
UneClasse(String nom) {
this.nom = nom;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 347 / 379
Java Cours 10
String nom;
int prix;
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 348 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 349 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 350 / 379
Java Cours 10
Revision Polymorphisme
Une interface et deux classes qui limplementent :
interface I { void method(); }
class A implements I {
void method() { System.out.println("A"); }
}
class B implements I {
void method() { System.out.println("B"); }
}
Laquelle des deux methodes est appelee ?
int test(boolean choix)
I i; if (choix) i = new A(); else i = new B();
i.method();
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 351 / 379
Java Cours 10
Revision Polymorphisme
Une classe qui etend une classe :
class A {
void method() { System.out.println("A"); }
}
class B extends A {
void method() { System.out.println("B"); }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 352 / 379
Java Cours 10
Revision Enumeration
enum Bidule {
Truc,
Machin,
Chose
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 353 / 379
Java Cours 10
Utilisation :
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 355 / 379
Java Cours 10
public/protected/private
static
final
abstract
synchronized
throws
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 356 / 379
Java Cours 10
I Par defaut, une classe ou une methode est non-publique : elle nest
accessible que depuis les classes du meme paquet.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 357 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 358 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 359 / 379
Java Cours 10
- : prive (private)
# : protege (protected)
+ : publique (public)
: non-publique (default)
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 360 / 379
Java Cours 10
class Compteur {
private static int cpt = 0;
static void compte() { cpt++; }
static int valeur() { return cpt; }
}
Utilisation :
Compteur.compte();
Compteur.compte();
Compteur.compte();
System.out.println(Compteur.valeur());
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 361 / 379
Java Cours 10
abstract class A {
abstract int prix();
void afficherPrix() { System.out.println(prix()); };
}
class B extends A {
int prix() { return 5; }
}
class C extends A {
int prix() { return 10; }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 362 / 379
Java Cours 10
B b = new B();
C c = new C();
b.afficherPrix();
c.afficherPrix();
A a;
if (choix) a = new B(); else a = new C();
a.afficherPrix();
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 363 / 379
Java Cours 10
if/else
for
do/while
switch/case/default
continue/break
try/catch/finally
throw
return
new
null
false
true
this
super
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 364 / 379
Java Cours 10
Revision this
class Machin {
private String nom;
private int prix;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 365 / 379
Java Cours 10
Revision super
class Truc {
private String nom;
public Truc(String nom) { this.nom = nom; }
public String toString() { return nom; }
}
Revision Exceptions
Elles etendent Exception et on les jette avec le mot-cle throw :
throw new MyException("truc");
On les captures avec try/catch :
try {
...
} catch(MyException e) {
...
}
boolean
byte
char
short
int
long
double
float
void
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 368 / 379
Java Cours 10
int a = 12;
double b = 1 3 . 5 ;
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 369 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 370 / 379
Java Cours 10
Implementations de Map<K,V> :
I HashMap : table de hachage
I LinkedHashMap : table de hachage + listes chainees
I TreeMap : arbre rouge-noir (Les elements doivent etre comparables)
Implementations de Set<V> :
I HashSet<V> : avec une HashMap.
I LinkedHashSet<V> : avec une LinkedHashMap.
I TreeSet<V> : avec une TreeMap.
Implementations de Deque<V> :
I ArrayDeque<V> : avec un tableau dynamique.
I LinkedList<V> : avec une liste chanee.
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 371 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 373 / 379
Java Cours 10
}
Condition sur les parametres :
class MaClasseGenerique<T> {
void method1(? super T p1, ? extends T p2) { ... }
}
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 374 / 379
Java Cours 10
assert
goto
native
assert
strictfp
volatile
instanceof
transient
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 375 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 376 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 377 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 378 / 379
Java Cours 10
Bertrand Estellon (DIL Aix-Marseille) Programmation Orientee Objet 29 mai 2012 379 / 379