Académique Documents
Professionnel Documents
Culture Documents
Bertrand Estellon
D
epartement dInformatique de Luminy
Aix-Marseille Universit
e
29 mai 2012
Programmation Orient
ee Objet
29 mai 2012
1 / 379
Java
Cours 1
Les objectifs :
I
Solutions proposees :
I
Programmation Orient
ee Objet
29 mai 2012
2 / 379
Java
Cours 1
robuste et s
ur ;
tr`es performant ;
Programmation Orient
ee Objet
29 mai 2012
3 / 379
Java
Cours 1
Python
Ruby
Eiffel
Ada
Smalltalk
...
La syntaxe change mais le concept objet est le m
eme !
Programmation Orient
ee Objet
29 mai 2012
4 / 379
Java
Cours 1
Programmation Orient
ee 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 ! ) ;
}
}
Programmation Orient
ee Objet
29 mai 2012
6 / 379
Java
Cours 1
byte
short
int
long
float
double
char
boolean
entier
entier
entier
entier
flotant
flotant
caract`ere
boolean
8 bits
16 bits
32 bits
64 bits
32 bits
64 bits
16 bits
1 bit
-128 `a 127
-32768 `a 32767
231 `a 231 1
263 `a 263 1
caract`eres Unicode
false ou true
0
0
0
0
0.0
0.0
\u0000
false
int a = 12;
double b = 1 3 . 5 ;
Programmation Orient
ee Objet
29 mai 2012
7 / 379
Java
Cours 1
Syntaxe
Tr`es proche de celle du C :
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 Orient
ee 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 ) ;
Acc`es aux donnees des structures :
p r i n t f ( %d %d , p1>x , p1>y ) ;
Il ny a aucun comportement ou service associ
e aux structures
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
9 / 379
Java
Cours 1
Les structures du C
70
20
50
30
10
20
20
90
20
20
20
40
50
Programme
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
10 / 379
Java
Cours 1
70
20
1
20
50
30
10
20
20
90
20
20
50
40
Programmation Orient
ee Objet
29 mai 2012
11 / 379
Java
Cours 1
10
20
CalculerSomme
Programmation Orient
ee Objet
29 mai 2012
12 / 379
Java
Cours 1
Programmation Orient
ee Objet
29 mai 2012
13 / 379
Java
Cours 1
Programmation Orient
ee Objet
29 mai 2012
14 / 379
Java
Cours 1
Programmation Orient
ee Objet
29 mai 2012
15 / 379
Java
Cours 1
Reference null
Les variables de type reference contienne la valeur null par defaut. La
valeur null signifie quelle ne pointe vers aucune instance.
Affectation de la valeur null `a une variable :
A d d i t i o n n e u r add = n u l l ;
Lutilisation dune methode (ou dune donnee) `a partir dune variable de
type reference `a null provoque une erreur `a lex
ecution :
A d d i t i o n n e u r add = n u l l ;
add . a f f e c t e r V a l e u r s ( 1 0 , 3 0 ) ;
Exception in thread "main" java.lang.NullPointerException
at ProgrammePrincipal.main(ProgrammePrincipal.java:4)
Programmation Orient
ee 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 ;
i f ( add==add2 ) System . o u t . p r i n t l n ( add==add2 ) ;
e l s e System . o u t . p r i n t l n ( add != add2 ) ;
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 ( ) ;
i f ( add==add3 ) System . o u t . p r i n t l n ( add==add3 ) ;
e l s e System . o u t . p r i n t l n ( add != add3 ) ;
}
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
17 / 379
Java
Cours 1
Programmation Orient
ee Objet
29 mai 2012
18 / 379
Java
Cours 1
Programmation Orient
ee Objet
29 mai 2012
19 / 379
Java
Cours 1
Constructeur (Declaration)
class Additionneur {
i n t v1 , v2 ;
Additionneur ( int valeur1 , int valeur2 ) {
v1 = v a l e u r 1 ; v2 = v a l e u r 2 ;
}
Additionneur ( int valeur ) {
v1 = v a l e u r ; v2 = v a l e u r ;
}
in t calculerSomme () {
r e t u r n v1+v2 ;
}
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
20 / 379
Java
Cours 1
est equivalent `a
public class A {
int a ;
int b ;
p u b l i c A( ) {
a = 1;
a = 2;
}
}
Programmation Orient
ee Objet
29 mai 2012
21 / 379
Java
Cours 1
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
}
}
Programmation Orient
ee Objet
29 mai 2012
22 / 379
Java
Cours 1
Mot-cle this
class Additionneur {
int valeur1 , valeur2 ;
Additionneur ( int valeur1 , int valeur2 ) {
this . valeur1 = valeur1 ; this . valeur2 = valeur2 ;
}
Additionneur ( int valeur ) {
valeur1 = valeur ; valeur2 = valeur ;
}
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 Orient
ee Objet
29 mai 2012
23 / 379
Java
Cours 1
Mot-cle this
class Additionneur {
int valeur1 , valeur2 ;
Additionneur ( int valeur1 , int valeur2 ) {
this . valeur1 = valeur1 ; this . valeur2 = valeur2 ;
}
Additionneur ( int valeur ) {
this ( valeur , valeur ) ;
}
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 Orient
ee Objet
29 mai 2012
24 / 379
Java
Cours 1
Programmation Orient
ee Objet
29 mai 2012
25 / 379
Java
Cours 1
affiche 21
affiche 41
affiche 22
affiche 42
Programmation Orient
ee Objet
29 mai 2012
26 / 379
Java
Cours 1
Programmation Orient
ee Objet
29 mai 2012
27 / 379
Java
Cours 1
Les tableaux
Declaration dune variable de type reference vers un tableau de :
int [ ] tableauDEntiers ;
A d d i t i o n n e u r [ ] tableauDAdds ;
Creation dun tableau :
t a b l e a u D E n t i e r s = new i n t [ 1 0 ] ;
t a b l e a u D A d d s = new A d d i t i o n n e u r [ 1 0 ] ;
Utilisation dun tableau :
f o r ( i n t i = 0 ; i < t a b l e a u D A d d s . l e n g t h ; i ++)
t a b l e a u D A d d s [ i ] = new A d d i t i o n n e u r ( i ,10 i ) ;
int r = 1;
f o r ( i n t i = t a b l e a u D A d d s . l e n g t h 1; i >= 0 ; i )
r = r tableauDAdds [ i ] . calculerSomme ( ) ;
Programmation Orient
ee Objet
29 mai 2012
28 / 379
Java
Cours 1
Programmation Orient
ee Objet
29 mai 2012
29 / 379
Java
Cours 1
i n t [ ] tab = n u l l ;
System . o u t . p r i n t l n ( t a b [ 1 2 ] ) ;
Exception in thread "main" java.lang.NullPointerException
at ProgrammePrincipal.main(ProgrammePrincipal.java:4)
Programmation Orient
ee Objet
29 mai 2012
30 / 379
Java
Cours 1
Chanes de caract`eres
Trois classes permettent de gerer les chanes de caract`eres :
I
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 Orient
ee Objet
29 mai 2012
31 / 379
Java
Cours 1
Chanes de caract`eres
Affichage :
System . o u t . p r i n t ( hw ) ;
System . o u t . p r i n t l n ( hw12c ) ;
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
Programmation Orient
ee Objet
29 mai 2012
32 / 379
Java
Cours 1
Programmation Orient
ee Objet
29 mai 2012
33 / 379
Java
Cours 1
Programmation Orient
ee Objet
29 mai 2012
34 / 379
Java
Cours 1
i n t x1 , i n t y1 , i n t x2 , i n t y2 ) {
;
. y1 = y1 ;
. 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 Orient
ee Objet
29 mai 2012
35 / 379
Java
Cours 1
Packages
I
Par defaut, les classes sont placees dans le paquet sans nom.
Les noms de paquet qui commencent par java. sont reserves `a Java.
Programmation Orient
ee Objet
29 mai 2012
36 / 379
Java
Cours 1
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).
Une classe C du paquet nomPaquet peut etre designee par son nom :
I
I
I
Programmation Orient
ee Objet
29 mai 2012
37 / 379
Java
Cours 1
Par defaut, une classe ou une methode est non-publique : elle nest
accessible que depuis les classes du m
eme paquet.
Programmation Orient
ee Objet
29 mai 2012
38 / 379
Java
Cours 1
Un membre priv
e nest accessible que par les methodes de la classe
qui le contient.
Programmation Orient
ee Objet
29 mai 2012
39 / 379
Java
Cours 1
Programmation Orient
ee Objet
29 mai 2012
40 / 379
Java
Cours 1
c l a s s MaP i l e {
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;
public void e mpilerE n t i e r ( i n t e n t i e r ) {
m a P i l e [ t a i l l e ] = e n t i e r ; t a i l l e ++;
}
public int depi ler Enti er () {
t a i l l e ; r e t u r n m a P i l e [ t a i l l e ] ;
}
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
41 / 379
Java
Cours 1
Resume du cours
Les points abordes pendant ce cours :
I
Mot-cle this
Membres statiques
Les tableaux
Organisation en packages
Convention de nommage
Programmation Orient
ee Objet
29 mai 2012
42 / 379
Java
Cours 2
Mot-cle this
Membres statiques
Les tableaux
Organisation en packages
Convention de nommage
Programmation Orient
ee Objet
29 mai 2012
43 / 379
Java
Cours 2
70
20
1
20
50
30
10
20
20
90
20
20
50
40
Programmation Orient
ee Objet
29 mai 2012
44 / 379
Java
Cours 2
10
20
CalculerSomme
Programmation Orient
ee Objet
29 mai 2012
45 / 379
Java
Cours 2
Abstraction
Des objets implementant des services identiques de facons differentes :
acher(String s)
Telephone
alerter()
acher(String s)
sonner()
alerter()
acher(String s)
Ordinateur
alerter()
Programmation Orient
ee Objet
29 mai 2012
46 / 379
Java
Cours 2
Abstraction
Objectifs :
I
Ecrire
un programme en supposant que les objets manipules
implementent certains services :
boolean e s t O r d o n n e ( Comparable t a b [ ] ) {
f o r ( i n t i = 0 ; i < t a b . l e n g t h 1 ; i ++)
i f ( t a b [ i ] . compareTo ( t a b [ i +1]) > 0 ) r e t u r n f a l s e ;
return true ;
}
Si le tableau contient des objets comparables alors cette methode
determine si le tableau est ordonne
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
47 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
48 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
49 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
50 / 379
Java
Cours 2
References et interfaces
Declaration dune variable de type reference vers une instance dune
classe qui impl
emente 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 ! !
Transtypage vers le haut (upcasting)
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
51 / 379
Java
Cours 2
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 Orient
ee Objet
29 mai 2012
52 / 379
Java
Cours 2
Polymorphisme
Le choix de la methode `a executer ne peut etre fait qu`a lexecution :
A f f i c h e u r [ ] a f f i c h e u r s = new A f f i c h e u r [ 2 ] ;
a f f i c h e u r s [ 0 ] = new T e l e p h o n e ( ) ;
a f f i c h e u r s [ 1 ] = new O r d i n a t e u r ( pc ) ;
Random r = new Random ( ) ; // instanciation dun generateur aleatoire.
i n t i = r . n e x t I n t ( 2 ) ; // tirage dun nombre en 0 et 1.
a f f i c h e u r s [ i ] . a f f i c h e r ( mon message ) ;
I
(En C, l
editeur de liens determine `a la compilation les appels `a effectuer)
Programmation Orient
ee Objet
29 mai 2012
53 / 379
Java
Cours 2
Resume
I
Java verifie `
a la compilation que toutes les affectations et les appels
de methodes sont corrects.
Programmation Orient
ee Objet
29 mai 2012
54 / 379
Java
Cours 2
Implementations multiples
interface Visualisable {
/
Affiche une chane de caract`eres 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 Orient
ee 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 ;
public P i l e V i s u a l i s a b l e ( int capacite ) {
t a b = new i n t [ c a p a c i t e ] ; s i z e = 0 ;
}
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 Orient
ee Objet
29 mai 2012
56 / 379
Java
Cours 2
Implementations multiples
Implementation dune des deux interfaces :
c l a s s S t r i n g 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 {
String s ;
public Str ingVisualisable ( String s ) {
this . s = 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 ) ;
}
}
Programmation Orient
ee 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 ( ) ;
Quaffiche ce programme `
a l
ecran ?
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
58 / 379
Java
Cours 2
Implementations multiples
Verification des types `a la compilation :
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 p = new P i l e V i s u a l i s a b l e ( 1 0 ) ;
t a b [ 1 ] = p ; Erreur `a la compilation (Une pile nest pas visualisable) !
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 ( ) ;
Programmation Orient
ee Objet
29 mai 2012
59 / 379
Java
Cours 2
R2
R1
G3
R4
I
I
I
R5
R6
Programmation Orient
ee Objet
29 mai 2012
60 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
61 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
62 / 379
Java
Cours 2
size = 0;
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 Orient
ee Objet
29 mai 2012
63 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
64 / 379
Java
Cours 2
G1
G2
R3
R2
D1
G3
D3
R5
D2
Programmation Orient
ee Objet
29 mai 2012
65 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
66 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
67 / 379
Java
Cours 2
R2
D1
G3
R5
D3
onClick()
D2
onClick()
onClick()
onClick()
Observeur1
Observeur2
Observeur3
Programmation Orient
ee Objet
29 mai 2012
68 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
69 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
70 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
71 / 379
Java
Cours 2
Programmation Orient
ee Objet
29 mai 2012
72 / 379
Java
Cours 2
Quaffiche ce programme ?
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
73 / 379
Java
Cours 2
Programmation Orient
ee 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 ?
Programmation Orient
ee Objet
29 mai 2012
75 / 379
Java
Cours 2
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 Orient
ee Objet
29 mai 2012
76 / 379
Java
Cours 2
Resume
I
Polymorphisme dobjet
Classe anonyme
Programmation Orient
ee Objet
29 mai 2012
77 / 379
Java
Cours 3
Packages
Deuxi`eme cours :
I
Polymorphisme dobjet
Programmation Orient
ee Objet
29 mai 2012
78 / 379
Java
Cours 3
R2
D1
G3
R5
D3
onClick()
D2
onClick()
onClick()
onClick()
Observeur1
Observeur2
Observeur3
Programmation Orient
ee Objet
29 mai 2012
79 / 379
Java
Cours 3
Programmation Orient
ee Objet
29 mai 2012
80 / 379
Java
Cours 3
Programmation Orient
ee Objet
29 mai 2012
81 / 379
Java
Cours 3
Programmation Orient
ee Objet
29 mai 2012
82 / 379
Java
Cours 3
Abstraction et extension
Le service associe aux listeners est identique. On aimerait :
I
traiterClic()
Observateur
Interface Cliquable
onClick()
addClickListener()
Interface ClickListener
traiterClic()
Programmation Orient
ee Objet
traiterClic()
29 mai 2012
83 / 379
Java
Cours 3
Extension
Lextension permet de creer une classe qui :
I
En Java :
I
Ne pas
etendre quand une impl
ementation dinterface suffit !
Programmation Orient
ee 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 Orient
ee Objet
29 mai 2012
85 / 379
Java
Cours 3
i n t y1 , i n t x2 , i n t y2 , i n t numero ) {
t h i s . x2 = x2 ;
t h i s . y2 = y2 ;
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 Orient
ee Objet
29 mai 2012
86 / 379
Java
Cours 3
Programmation Orient
ee 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 Orient
ee Objet
29 mai 2012
88 / 379
Java
Cours 3
a1
b1
a2
b2
b3
=
=
=
=
=
new A ( ) ;
new B ( ) ;
b1 ; // upcasting !
(B) a2 ; // downcasting possible !
(B) a1 ; // downcasting impossible (erreur `
a lexecution)
Programmation Orient
ee 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
Programmation Orient
ee Objet
29 mai 2012
90 / 379
Java
Cours 3
Mot-cle super
Le mot-cle super permet dutiliser une methode definie au dessus :
public class A
{ p u b l i c S t r i n g getName ( ) { r e t u r n A ; } }
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/ }
}
Programmation Orient
ee Objet
29 mai 2012
91 / 379
Java
Cours 3
Mot-cle super
Le mot-cle super permet dutiliser une propriete definie au dessus :
public class A
{ p u b l i c S t r i n g name = A ; }
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/ }
}
Programmation Orient
ee Objet
29 mai 2012
92 / 379
Java
Cours 3
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 Orient
ee Objet
29 mai 2012
93 / 379
Java
Cours 3
Programmation Orient
ee Objet
29 mai 2012
94 / 379
Java
Cours 3
Programmation Orient
ee Objet
29 mai 2012
95 / 379
Java
Cours 3
Modificateur final
Le modificateur final permet de bloquer lextension dune classe ou
la red
efinition dune m
ethode.
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 /
}
Programmation Orient
ee Objet
29 mai 2012
96 / 379
Java
Cours 3
Classes abstraites
I
Exemple :
p u b l i c a b s t r a c t c l a s s SansNom {
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 ( obt eni r Nom ( ) ) ; }
a b s t r a c t S t r i n g o b t e ni r Nom ( ) ;
}
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 Orient
ee Objet
29 mai 2012
97 / 379
Java
Cours 3
Programmation Orient
ee Objet
29 mai 2012
98 / 379
Java
Cours 3
Programmation Orient
ee Objet
29 mai 2012
99 / 379
Java
Cours 3
Par defaut, une classe ou une methode est non-publique : elle nest
accessible que depuis les classes du m
eme paquet.
Programmation Orient
ee Objet
29 mai 2012
100 / 379
Java
Cours 3
Un membre priv
e nest accessible que par les methodes de la classe
qui le contient.
Programmation Orient
ee Objet
29 mai 2012
101 / 379
Java
Cours 3
Modificateur protected
I
Un membre prot
eg
e est accessible depuis :
I
I
Programmation Orient
ee Objet
29 mai 2012
102 / 379
Java
Cours 3
Programmation Orient
ee Objet
29 mai 2012
103 / 379
Java
Cours 3
La classe Object
Par d
efaut, les classes
etendent la classe Object
Consequence : le upcasting vers Object est toujours possible
MaCla sse
Object o
Object [ ]
for ( int
c = new MaClasse ( ) ;
= c;
t = new O b j e c t [ 1 0 ] ;
i = 0 ; i < t ; i ++) {
i f ( i %2==0) t [ i ] = new U n e C l a s s e ( ) ;
e l s e t [ i ] = new U n e D e u x i e m e C l a s s e ( ) ;
Programmation Orient
ee Objet
29 mai 2012
104 / 379
Java
Cours 3
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 Orient
ee 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 ;
}
...
}
Programmation Orient
ee Objet
29 mai 2012
106 / 379
Java
Cours 3
r e t u r n MaClasse +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 ) ;
Programmation Orient
ee Objet
29 mai 2012
107 / 379
Java
Cours 3
Extension dinterfaces
Il est egalement possible detendre une interface :
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 ( ) ;
}
Programmation Orient
ee 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 ( ) ; }
public Object next () {
Object o = v . get (p ) ;
}
p++; r e t u r n o ;
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee 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 ( ) ) ;
Programmation Orient
ee Objet
29 mai 2012
110 / 379
Java
Cours 3
Resume
I
Abstraction
mot-cle super
La classe Object
La methode toString()
Programmation Orient
ee Objet
29 mai 2012
111 / 379
Java
Cours 4
Deuxi`eme cours :
I
Troisi`eme cours :
I
Extension de classe
La classe Object
Programmation Orient
ee Objet
29 mai 2012
112 / 379
Java
Cours 4
Classes internes
I
Programmation Orient
ee 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 i c boolean hasNext ( ) { r e t u r n p o s i t i o n < v . s i z e ; }
p u b l ic Object next () {
Object o = v . array [ p o s i t i o n ] ;
return o ;
}
p o s i t i o n ++;
p u b l i c v o i d remove ( ) { }
}
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee 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 i c boolean hasNext ( ) { r e t u r n p o s i t i o n < s i z e ; }
p u b l ic Object next () {
Object o = array [ p o s i t i o n ] ;
return o ;
}
p o s i t i o n ++;
p u b l i c v o i d remove ( ) { }
}
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee 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 boolean hasNext ( ) { r e t u r n p o s i t i o n < s i z e ; }
p u b l i c Object next () {
Object o = array [ p o s i t i o n ] ;
return o ;
}
p o s i t i o n ++;
p u b l i c v o i d remove ( ) { }
};
}
}
Programmation Orient
ee Objet
29 mai 2012
116 / 379
Java
Cours 4
Introduction `a AWT
I
Programmation Orient
ee Objet
29 mai 2012
117 / 379
Java
Cours 4
Composants
I
TextComponent
Button
Canvas
Checkbox
List
Choice
Scrollbar
Label
Container
I
I
Programmation Orient
ee Objet
TextArea
TextField
29 mai 2012
118 / 379
Java
Cours 4
Conteneurs
I
Panel
I
Applet
Window
I
Dialog
I
I
Frame
I
FileDialog
JDialog (Swing)
JFrame (Swing)
JWindow (Swing)
JComponent (Swing)
Programmation Orient
ee 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 Orient
ee Objet
29 mai 2012
120 / 379
Java
Cours 4
Exemple
Programmation Orient
ee Objet
29 mai 2012
121 / 379
Java
Cours 4
Gestionnaires de presentation
I
BorderLayout
BoxLayout
CardLayout
FlowLayout
GridBagLayout
GridLayout
...
Programmation Orient
ee Objet
29 mai 2012
122 / 379
Java
Cours 4
Gestionnaires de presentation
I
Panel FlowLayout
Window BorderLayout
Programmation Orient
ee Objet
29 mai 2012
123 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
124 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
125 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
126 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
127 / 379
Java
Cours 4
enements
Ev`
Programmation Orient
ee Objet
29 mai 2012
128 / 379
Java
Cours 4
enements
Ev`
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 ! ! ) ;
}
}
But ton b = new But ton ( B ) ;
b . add A c t i o n L i s t e n e r ( new M y A c t i o n L i s t e n e r ( ) ) ;
p a n e l . add ( b ) ;
Remarque :
I
Programmation Orient
ee Objet
29 mai 2012
129 / 379
Java
Cours 4
enements Source
Ev`
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 Orient
ee Objet
29 mai 2012
130 / 379
Java
Cours 4
enements Souris
Ev`
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 ) ;
}
Programmation Orient
ee Objet
29 mai 2012
131 / 379
Java
Cours 4
enements Souris
Ev`
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 ( ) ) ;
}
public
public
public
public
void
void
void
void
m o u s e P r e s s e d ( MouseEvent e ) {
m o u s e R e l e a s e d ( MouseEvent e ) {
m o u s e E n t e r e d ( MouseEvent e ) {
m o u s e E x i t e d ( MouseEvent e ) {
}
}
}
}
Programmation Orient
ee Objet
29 mai 2012
132 / 379
Java
Cours 4
enements Clavier
Ev`
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 ) { }
}
Programmation Orient
ee Objet
29 mai 2012
133 / 379
Java
Cours 4
enements Fenetre
Ev`
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 Orient
ee Objet
29 mai 2012
134 / 379
Java
Cours 4
enements Adapteurs
Ev`
I
I
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 Orient
ee Objet
29 mai 2012
135 / 379
Java
Cours 4
Programmation Orient
ee 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 Orient
ee Objet
29 mai 2012
137 / 379
Java
Cours 4
Exemple Calculette
Suite de la classe 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 ;
....
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 {
public void actionPerformed ( ActionEvent e ) {
i n t v1 = I n t e g e r . p a r s e I n t ( number1 . g e t T e x t ( ) ) ;
i n t v2 = I n t e g e r . p a r s e I n t ( number2 . g e t T e x t ( ) ) ;
r e s u l t . s e t T e x t ( +(v1+v2 ) ) ;
}
}
}
Programmation Orient
ee 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 ) ;
}
});
}
Programmation Orient
ee Objet
29 mai 2012
139 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
140 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
141 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
142 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
143 / 379
Java
Cours 4
Swing
I
Programmation Orient
ee Objet
29 mai 2012
144 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
145 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
146 / 379
Java
Cours 4
GTKLookAndFeel
MotifLookAndFeel
WindowsLookAndFeel
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
147 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
148 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
149 / 379
Java
Cours 4
Programmation Orient
ee Objet
29 mai 2012
150 / 379
Java
Cours 5
Deuxi`eme cours :
I
Troisi`eme cours :
I
Extension de classe
La classe Object
Programmation Orient
ee Objet
29 mai 2012
151 / 379
Java
Cours 5
Surcharge de methodes
I
I
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 Orient
ee Objet
29 mai 2012
152 / 379
Java
Cours 5
Surcharge de methodes
I
I
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
}
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
}
}
{
: +o ) ;
{
: +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 Orient
ee Objet
29 mai 2012
153 / 379
Java
Cours 5
Programmation Orient
ee Objet
29 mai 2012
154 / 379
Java
Cours 5
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 ) ; //
Object o = a . get ( ) ;
//
System . o u t . p r i n t l n ( o ) ; //
a = new A ( ) ;
o = a . get ( ) ;
System . o u t . p r i n t l n ( o ) ; //
Affiche String
Le type de retour est Object (et pas String)
Affiche String
Affiche Object
Programmation Orient
ee Objet
29 mai 2012
155 / 379
Java
Cours 5
t a i l l e = 0; }
p i l e [ t a i l l e ]= n u l l ;
Programmation Orient
ee Objet
29 mai 2012
156 / 379
Java
Cours 5
Deuxi`eme probl`eme :
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)
Solution : pr
eciser le type des
el
ements autoris
es dans la pile
P i l e<S t r i n g > p = new P i l e<S t r i n g >( ) ;
S t r i n g s = t r u c ;
p . empiler ( s );
// Ok car s est de type String
S t r i n g s = p . d e p i l e r ( ) ; // Ok car la pile ne contient que des String
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee 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 ] ;
r e t u r n (T) e ;
}
p i l e [ t a i l l e ]= n u l l ;
Programmation Orient
ee 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
Programmation Orient
ee Objet
29 mai 2012
159 / 379
Java
Cours 5
byte
short
int
long
float
double
char
boolean
entier
entier
entier
entier
flotant
flotant
caract`ere
boolean
8 bits
16 bits
32 bits
64 bits
32 bits
64 bits
16 bits
1 bit
-128 `a 127
-32768 `a 32767
231 `a 231 1
263 `a 263 1
caract`eres Unicode
false ou true
Programmation Orient
ee Objet
0
0
0
0
0.0
0.0
\u0000
false
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()
Les classes demballage qui etendent Number :
I
I
I
I
I
I
Programmation Orient
ee Objet
29 mai 2012
161 / 379
Java
Cours 5
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 Orient
ee Objet
29 mai 2012
162 / 379
Java
Cours 5
Programmation Orient
ee Objet
29 mai 2012
163 / 379
Java
Cours 5
taille ;
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 Orient
ee Objet
29 mai 2012
164 / 379
Java
Cours 5
Exemple 1 :
C a r t e [ ] t a b = new C a r t e [ 3 ] ; t a b [ 0 ] = new C a r t e ( 1 , 2 ) ;
t a b [ 1 ] = new C a r t e ( 2 , 3 ) ; t a b [ 2 ] = new C a r t e ( 2 , 4 ) ;
System . o u t . p r i n t l n ( T o o l s . i s S o r t e d ( t a b ) ) ;
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
165 / 379
Java
Cours 5
Exemple 2 :
S t r i n g [ ] t a b = new S t r i n g [ 3 ] ; t a b [ 0 ] = t o t o ;
t a b [ 1 ] = t r u c ; t a b [ 2 ] = abc ;
System . o u t . p r i n t l n ( T o o l s . i s S o r t e d ( t a b ) ) ;
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
166 / 379
Java
Cours 5
Exemple 3 :
O b j e c t [ ] t a b = new S t r i n g [ 3 ] ; t a b [ 0 ] = t o t o ;
t a b [ 1 ] = t r u c ; t a b [ 2 ] = abc ;
System . o u t . p r i n t l n ( T o o l s . i s S o r t e d ( t a b ) ) ; Erreur de compilation
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee 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 ( ) ;
}
p u b l i c i n t e r f a c e I t e r a t o r <T> { (Interface Java)
p u b l i c b o o le a n h a s N e x t ( ) ;
public T next ( ) ;
p u b l i c v o i d remove ( ) ;
}
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 Orient
ee Objet
29 mai 2012
168 / 379
Java
Cours 5
Programmation Orient
ee Objet
29 mai 2012
169 / 379
Java
Cours 5
p r i v a t e Object [ ] tab ;
public int
taille ;
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 Orient
ee 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 ) ;
Programmation Orient
ee Objet
29 mai 2012
171 / 379
Java
Cours 5
Programmation Orient
ee Objet
29 mai 2012
172 / 379
Java
Cours 5
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>> { . . . }
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 > ( ) ;
// Ok : JolieCarte implemente Comparable<Carte>
Programmation Orient
ee 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 ++;
}
...
}
I
I
I
Programmation Orient
ee Objet
29 mai 2012
174 / 379
Java
Cours 5
::::::::::::::::
Programmation Orient
ee Objet
29 mai 2012
175 / 379
Java
Cours 5
Programmation Orient
ee Objet
29 mai 2012
176 / 379
Java
Cours 5
Programmation Orient
ee Objet
29 mai 2012
177 / 379
Java
Cours 5
Resume
Surcharge de methodes
Covariance
Classes parametrees
Methodes parametrees
Iterateurs
? super et ? extends
Programmation Orient
ee Objet
29 mai 2012
178 / 379
Java
Cours 6
Deuxi`eme cours :
I
Troisi`eme cours :
I
Cinqui`eme cours :
I
Surcharge de methodes
Types parametres
Programmation Orient
ee Objet
29 mai 2012
179 / 379
Java
Cours 6
Programmation Orient
ee Objet
29 mai 2012
180 / 379
Java
Cours 6
Les collections
Les methodes de linterface Collection<V> :
I
boolean add(V e)
void clear()
boolean contains(Object o)
boolean containsAll(Collection<?> c)
boolean isEmpty()
boolean remove(Object o)
boolean removeAll(Collection<?> c)
boolean retainAll(Collection<?> c)
int size()
Object[] toArray()
Programmation Orient
ee Objet
29 mai 2012
181 / 379
Java
Cours 6
Les listes
Les methodes de linterface List<V> :
I
V get(int index)
int indexOf(Object o)
int lastIndexOf(Object o)
V remove(int index)
int indexOf(Object o)
Programmation Orient
ee Objet
29 mai 2012
182 / 379
Java
Cours 6
Les listes
Programmation Orient
ee Objet
29 mai 2012
183 / 379
Java
Cours 6
Les maps
Les methodes de linterface Map<K,V> :
I
clear()
Set<Map.Entry<K,V>> entrySet()
V get(Object key)
boolean isEmpty()
Set<K> keySet()
V remove(Object key)
int size()
Collection<V> values()
Programmation Orient
ee Objet
29 mai 2012
184 / 379
Java
Cours 6
Les maps
Quelques implementations de linterface Map<K,V> :
I
Table de hachage :
John Smith
Lisa Smith
Sam Doe
0
1
872
873
998
999
Lisa Smith
+1-555-8976
John Smith
+1-555-1234
Sam Doe
+1-555-5030
Programmation Orient
ee Objet
29 mai 2012
185 / 379
Java
Cours 6
Les maps
Quelques implementations de linterface Map<K,V> :
I
Arbre rouge-noir :
Rechercher
Inserer
Supprimer
Complexite
O(log n)
O(log n)
O(log n)
Programmation Orient
ee Objet
29 mai 2012
185 / 379
Java
Cours 6
Les maps
Quelques implementations de linterface Map<K,V> :
I
Programmation Orient
ee Objet
29 mai 2012
185 / 379
Java
Cours 6
Les ensembles
Programmation Orient
ee Objet
29 mai 2012
186 / 379
Java
Cours 6
Les files
V element()
boolean offer(V e)
V peek()
V poll()
V remove()
Programmation Orient
ee Objet
29 mai 2012
187 / 379
Java
Cours 6
Ins
erer
Supprimer
Consulter
Premier
el
ement
avec exception avec null
addFirst(e)
offerFirst(e)
removeFirst()
pollFirst()
getFirst()
peekFirst()
Dernier
el
ement
avec exception avec null
addLast(e)
offerLast(e)
removeLast()
pollLast()
getLast()
peekLast()
Dans Deque
addLast(e)
offerLast(e)
emoveFirst()
pollFirst()
getFirst()
peekFirst()
Programmation Orient
ee Objet
29 mai 2012
188 / 379
Java
Cours 6
Programmation Orient
ee Objet
29 mai 2012
189 / 379
Java
Cours 6
Les iterateurs
Linterface Collection<V> etend Iterable<V> :
C o l l e c t i o n <I n t e g e r > l = new A r r a y L i s t <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 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
Programmation Orient
ee Objet
29 mai 2012
190 / 379
Java
Cours 6
Les iterateurs
Exemple avec une HashMap :
Map<S t r i n g , I n t e g e r > m = new HashMap<S t r i n g , I n t e g e r > ( ) ;
m. p u t ( t o t o , 4 ) ;
m. p u t ( aaa , 3 ) ;
m. p u t ( bb , 2 ) ;
f o r ( I n t e g e r i : m. v a l u e s ( ) )
System . o u t . p r i n t ( i+ ) ;
System . o u t . p r i n t l n ( ) ;
f o r ( S t r i n g k : m. k e y S e t ( ) )
System . o u t . p r i n t ( k+ ) ;
System . o u t . p r i n t l n ( ) ;
Sortie :
3 4 2
aaa toto bb
Programmation Orient
ee Objet
29 mai 2012
191 / 379
Java
Cours 6
Exceptions
Exemple :
I
etc.
Programmation Orient
ee Objet
29 mai 2012
192 / 379
Java
Cours 6
Programmation Orient
ee Objet
29 mai 2012
193 / 379
Java
Cours 6
Programmation Orient
ee 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) :
A B
C D
Bertrand Estellon (DIL Aix-Marseille)
test(13) :
A B
MyException: Erreur num
ero 13
D
Programmation Orient
ee Objet
29 mai 2012
195 / 379
Java
Cours 6
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 Orient
ee Objet
29 mai 2012
196 / 379
Java
Cours 6
i ) throws MyException {
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 ( ) ; }
}
}
Programmation Orient
ee Objet
29 mai 2012
197 / 379
Java
Cours 6
La classe RuntimeException
Une methode doit indiquer toutes les exceptions quelle peut generer
sauf si lexception etend la classe RuntimeException
Bien evidemment, la classe RuntimeException etend Exception
Quelques classes Java qui etendent RuntimeException :
I
ArithmeticException
ClassCastException
IllegalArgumentException
IndexOutOfBoundsException
NegativeArraySizeException
NullPointerException
Programmation Orient
ee Objet
29 mai 2012
198 / 379
Java
Cours 6
diviser(null,12) :
java.lang.ArithmeticException: / by zero
at Test.diviser(Test.java:17)
at Test.main(Test.java:28)
2147483647
java.lang.NullPointerException
at Test.diviser(Test.java:17)
at Test.main(Test.java:28)
0
Programmation Orient
ee 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 ( ) ; }
}
Programmation Orient
ee Objet
29 mai 2012
200 / 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 ( 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 Orient
ee 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 ;
public Pile ( int capacity ) {
p i l e = new O b j e c t [ c a p a c i t y ] ;
}
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 ) 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 Orient
ee Objet
29 mai 2012
202 / 379
Java
Cours 6
Exemple La pile
Definition des exceptions :
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 ) ; }
}
Programmation Orient
ee Objet
29 mai 2012
203 / 379
Java
Cours 6
Exemple La pile
Exemples dutilisation :
P i l e <I n t e g e r > p = new P i l e <I n t e g e r > ( 2 ) ;
try {
p . empiler (1);
p . empiler (2);
p . empiler (3);
} catch ( P i l e E x c e p t i o n e ) {
e . printStackTrace ();
}
try {
p . empiler (1);
p. depiler ();
p. depiler ();
} catch ( P i l e E x c e p t i o n e ) {
e . printStackTrace ();
}
Programmation Orient
ee Objet
29 mai 2012
204 / 379
Java
Cours 6
La classe Throwable
En Java, toutes les instances des classes
qui etendent Throwable peuvent etre jetees et capturees
Throwable
Error
Exception
RuntimeException
Programmation Orient
ee 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 Orient
ee Objet
29 mai 2012
206 / 379
Java
Cours 6
VirtualMachineError
I
I
I
I
ThreadDeath
InternalError
OutOfMemoryError
StackOverflowError
UnknownError
ArithmeticException
ClassCastException
IllegalArgumentException
IndexOutOfBoundsException
NegativeArraySizeException
NullPointerException
Exception :
I
IOException
I
I
FileNotFoundException
SocketException
Programmation Orient
ee Objet
29 mai 2012
207 / 379
Java
Cours 6
Resume
Structures de donnees
La classe Exception
Mot-cle throw
Mot-cle throws
Programmation Orient
ee Objet
29 mai 2012
208 / 379
Java
Cours 7
UML
Programmation Orient
ee Objet
29 mai 2012
209 / 379
Java
Cours 7
Diagramme de classes
Programmation Orient
ee Objet
29 mai 2012
210 / 379
Java
Cours 7
M
ethode nom ( arguments ) : type
Programmation Orient
ee Objet
29 mai 2012
211 / 379
Java
Cours 7
Programmation Orient
ee Objet
29 mai 2012
212 / 379
Java
Cours 7
Visibilite
#
+
:
:
:
:
prive (private)
protege (protected)
publique (public)
non-publique (default)
Programmation Orient
ee Objet
29 mai 2012
213 / 379
Java
Cours 7
Extensions et implementations
Programmation Orient
ee Objet
29 mai 2012
214 / 379
Java
Cours 7
Implementation multiples
Programmation Orient
ee Objet
29 mai 2012
215 / 379
Java
Cours 7
Dependances
Programmation Orient
ee Objet
29 mai 2012
216 / 379
Java
Cours 7
Associations
Programmation Orient
ee Objet
29 mai 2012
217 / 379
Java
Cours 7
Navigation
I
I
I
Programmation Orient
ee Objet
29 mai 2012
218 / 379
Java
Cours 7
Multiplicite
Programmation Orient
ee Objet
29 mai 2012
219 / 379
Java
Cours 7
Multiplicite
Notation
0..1
1
0..* (ou *)
1..*
n
0..n
1..n
Signification
Zero ou un
un uniquement
Zero ou plus
Un ou plus
Seulement n
Zero `a n
Un `a n
Programmation Orient
ee Objet
29 mai 2012
220 / 379
Java
Cours 7
Agregations
Programmation Orient
ee Objet
29 mai 2012
221 / 379
Java
Cours 7
Compositions
I
I
Programmation Orient
ee Objet
29 mai 2012
222 / 379
Java
Cours 7
Programmation Orient
ee Objet
29 mai 2012
223 / 379
Java
Cours 7
OperateurNAire
Variable
+ f : Formule[]
- name : String
- value : double
+ OperateurNAire(f : Formule[])
+ OperateurNAire(f1 : Formule, f2 : Formule)
+ afficher() : String
+ evaluer() : double
# symbole() : char
# evaluerBinaire(a : double, b : double) : double
# evaluerVide() : double
+ afficher() : String
+ evaluer() : double
+ set(v : double)
Produit
Somme
+ Produit(f : Formule[])
+ Produit(f1 : Formule, f2 : Formule)
# symbole() : char
# evaluerBinaire(a : double, b : double) : double
# evaluerVide() : double
+ Somme(f : Formule[])
+ Somme(f1 : Formule, f2 : Formule)
# symbole() : char
# evaluerBinaire(a : double, b : double) : double
# evaluerVide() : double
Programmation Orient
ee Objet
29 mai 2012
224 / 379
Java
Cours 7
ShapePanel
- shapes : ArrayList<Shape>
+ ShapePanel()
+ add(p : Shape)
# paintComponent(g : Graphics)
*
interface
Shape
+ paint(g : Graphics)
+ getNumberOfPoints() : int
+ getPoint(i : int) : Point
AbstractShape
Polygon
# decoratedShape : Shape
+ AbstractShape()
# addPoint(p : Point)
+ getPoint(i : int) : Point
+ getNumberOfPoints() : int
+ paint(g : Graphics)
+ Decorator(decoratedShape : Shape)
+ paint(g : Graphics)
+ getNumberOfPoints() : int
+ getPoint(i : int) : Point
# paintDecoration(g : Graphics)
Rectangle
- color : Color
- color : Color
Decorator
- points : List<Point>
*
Point
Programmation Orient
ee Objet
BorderDecorator
- radius : int
- color : Color
+ BorderDecorator(s : Shape, radius : int, color : Color)
# paintDecoration(g : Graphics)
29 mai 2012
225 / 379
Java
Cours 7
Les objectifs :
I
Solutions proposees :
I
Programmation Orient
ee Objet
29 mai 2012
226 / 379
Java
Cours 7
Conception detaillee
Implementation
Tests unitaires
Integration
Mise en production
Programmation Orient
ee Objet
29 mai 2012
227 / 379
Java
Cours 7
Les evolutions
Adaptation de larchitecture
Tests unitaires
Integration
Mise en production
Programmation Orient
ee Objet
29 mai 2012
228 / 379
Java
Cours 7
Objectifs :
I
Gagner du temps
Programmation Orient
ee Objet
29 mai 2012
229 / 379
Java
Cours 7
Eviter
les interfaces qui contiennent beaucoup de methodes
Programmation Orient
ee Objet
29 mai 2012
230 / 379
Java
Cours 7
Programmation Orient
ee Objet
29 mai 2012
231 / 379
Java
Cours 7
Augmentation de la fiabilite
Programmation Orient
ee Objet
29 mai 2012
231 / 379
Java
Cours 7
GeometricApplication
Rectangle
Violation de SRP :
+ Rectangle()
+ draw()
+ getArea() : double
GraphicalInterface
GraphicalApplication
GraphicalRectangle
GeometryApplication
~ GraphicalRectangle()
+ draw()
GraphicalInterface
~ GeometricRectangle()
+ getArea() : double
Programmation Orient
ee Objet
29 mai 2012
232 / 379
Java
Cours 7
Avantages :
I
Programmation Orient
ee Objet
29 mai 2012
233 / 379
Java
Cours 7
Programmation Orient
ee Objet
29 mai 2012
234 / 379
Java
Cours 7
Programmation Orient
ee Objet
29 mai 2012
235 / 379
Java
Cours 7
Programmation Orient
ee Objet
29 mai 2012
236 / 379
Java
Cours 7
v o i d s e t W i d t h ( double w) { t h i s . w = w ; }
v o i d s e t H e i g h t ( double h ) { t h i s . h = h ; }
double g e t W i d t h ( ) { r e t u r n w ; }
double g e t H e i g h t ( ) { r e t u r n h ; }
double g e t A r e a ( ) { r e t u r n wh ; }
Programmation Orient
ee Objet
29 mai 2012
237 / 379
Java
Cours 7
Programmation Orient
ee Objet
29 mai 2012
238 / 379
Java
Cours 7
La mauvaise question :
Un carre est-il un rectangle ?
La bonne question :
Pour les utilisateurs,
votre carre a-t-il le meme comportement que votre rectangle ?
La r
eponse :
Dans ce cas, NON
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
239 / 379
Java
Cours 7
v o i d s e t W i d t h ( double w) { t h i s . w = w ; }
v o i d s e t H e i g h t ( double h ) { t h i s . h = h ; }
do u ble g e t W i d t h ( ) { r e t u r n w ; }
do u ble g e t H e i g h t ( ) { r e t u r n h ; }
Programmation Orient
ee 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 Orient
ee Objet
29 mai 2012
241 / 379
Java
Cours 8
Plan du cours
Principes SOLID :
Patrons de conception
Programmation Orient
ee Objet
29 mai 2012
242 / 379
Java
Cours 8
Eviter
les interfaces qui contiennent beaucoup de methodes
Programmation Orient
ee Objet
29 mai 2012
243 / 379
Java
Cours 8
Eviter
les interfaces qui contiennent beaucoup de methodes
Signification et objectifs :
I
I
I
Eviter
de devoir implementer des services qui nont pas `a etre
proposes par la classe qui implemente linterface
Limiter les modifications lors de la modification de linterface
Avantages :
I
I
I
Programmation Orient
ee Objet
29 mai 2012
244 / 379
Java
Cours 8
Ent reprise()
employer(e : Employer)
licencier(e : Employer)
produire()
*
int erf ace
Employer
+ t ravailler()
+ manger()
+ dormir()
Homme
+
+
+
+
Homme()
t ravailler()
manger()
dormir()
Femme
+
+
+
+
Femme()
t ravailler()
manger()
dormir()
Programmation Orient
ee Objet
29 mai 2012
245 / 379
Java
Cours 8
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 ) ;
}
}
Programmation Orient
ee Objet
29 mai 2012
246 / 379
Java
Cours 8
Programmation Orient
ee Objet
29 mai 2012
247 / 379
Java
Cours 8
Ent reprise()
employer(e : Employer)
licencier(e : Employer)
produire()
*
int erf ace
Employer
+ t ravailler()
+ manger()
+ dormir()
Robot
+
+
+
+
Homme
Robot ()
t ravailler()
manger()
dormir()
+
+
+
+
Homme()
t ravailler()
manger()
dormir()
Femme
+
+
+
+
Programmation Orient
ee Objet
Femme()
t ravailler()
manger()
dormir()
29 mai 2012
248 / 379
Java
Cours 8
Programmation Orient
ee Objet
29 mai 2012
249 / 379
Java
Cours 8
Ent reprise()
employer(e : Employer)
licencier(e : Employer)
produire()
*
int erf ace
Employer
+ t ravailler()
+ manger()
+ dormir()
Femme
Homme
Robot
+ Robot ()
+ t ravailler()
+
+
+
+
Femme()
t ravailler()
manger()
dormir()
+
+
+
+
Programmation Orient
ee Objet
Homme()
t ravailler()
manger()
dormir()
29 mai 2012
250 / 379
Java
Cours 8
Avantages :
I
I
I
Programmation Orient
ee Objet
29 mai 2012
251 / 379
Java
Cours 8
ShapePanel
- shapes : ArrayList <Shape>
+ ShapePanel()
+ addShape(s : Shape)
*
int erf ace
Shape
+ draw(g : Graphics)
Rect angle
Circle
- p1 : Point
- p2 : Point
~ c : Point
~ radius : int
Programmation Orient
ee Objet
29 mai 2012
252 / 379
Java
Cours 8
ShapePanel
- shapes : ArrayList <Shape>
+ ShapePanel()
+ addShape(s : Shape)
*
int erf ace
Shape
+ draw(g : Graphics)
FileLoader
+ FileLoader()
inst ancie
Rect angle
inst ancie
Circle
- p1 : Point
- p2 : Point
~ c : Point
~ radius : int
Programmation Orient
ee Objet
29 mai 2012
253 / 379
Java
Cours 8
Programmation Orient
ee 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 ();
}
}
Programmation Orient
ee Objet
29 mai 2012
255 / 379
Java
Cours 8
*
int erf ace
Shape
+ draw(g : Graphics)
Rect angle
Circle
- p1 : Point
- p2 : Point
~ c : Point
~ radius : int
NewCircle
Programmation Orient
ee Objet
29 mai 2012
256 / 379
Java
Cours 8
*
int erf ace
Shape
FileLoader
+ FileLoader()
+ draw(g : Graphics)
inst ancie
Circle
Rect angle
~ c : Point
~ radius : int
- p1 : Point
- p2 : Point
inst ancie
NewCircle
Programmation Orient
ee Objet
29 mai 2012
257 / 379
Java
Cours 8
Programmation Orient
ee Objet
29 mai 2012
258 / 379
Java
Cours 8
+
+
+
+
+ ShapePanel()
+ addShape(s : Shape)
FileLoader()
loadCircle(p : ShapePanel, s : Scanner)
loadRect angle(p : ShapePanel, s : Scanner)
loadFile(p : ShapePanel, name : St ring)
use
*
ShapeFact ory
+ ShapeFact ory()
+ creat eCircle(p : Point , radius : int ) : Shape
+ creat eRect angle(p1 : Point , p2 : Point ) : Shape
+ draw(g : Graphics)
inst ancie
Circle
Rect angle
~ c : Point
~ radius : int
- p1 : Point
- p2 : Point
inst ancie
NewCircle
Programmation Orient
ee 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 ) ;
}
}
Programmation Orient
ee Objet
29 mai 2012
260 / 379
Java
Cours 8
Programmation Orient
ee Objet
29 mai 2012
261 / 379
Java
Cours 8
Programmation Orient
ee Objet
29 mai 2012
262 / 379
Java
Cours 8
FileLoader
- f act ory : Abst ract ShapeFact ory
+
+
+
+
use
ShapeFact oryVersion2
ShapeFact oryVersion1
+ ShapeFact oryVersion2()
+ creat eCircle(p : Point , radius : int ) : Circle
+ creat eRect angle(p1 : Point , p2 : Point ) : Rect angle
+ ShapeFact oryVersion1()
+ creat eCircle(p : Point , radius : int ) : Circle
+ creat eRect angle(p1 : Point , p2 : Point ) : Rect angle
Programmation Orient
ee Objet
29 mai 2012
263 / 379
Java
Cours 8
ShapeFactoryVersion2
ShapeFactoryVersion1
+ ShapeFactoryVersion2()
+ createCircle(p : Point, radius : int) : Circle
+ createRectangle(p1 : Point, p2 : Point) : Rectangle
+ ShapeFactoryVersion1()
+ createCircle(p : Point, radius : int) : Circle
+ createRectangle(p1 : Point, p2 : Point) : Rectangle
instancie
instancie
instancie
Rectangle
instancie
Circle
- p1 : Point
- p2 : Point
- c : Point
- radius : int
NewCircle
+ NewCircle(c : Point, radius : int)
+ draw(g : Graphics)
Programmation Orient
ee Objet
29 mai 2012
264 / 379
Java
Cours 8
FileLoader(factory : AbstractShapeFactory)
loadCircle(p : ShapePanel, s : Scanner)
loadRectangle(p : ShapePanel, s : Scanner)
loadFile(p : ShapePanel, name : String)
add shape
ShapePanel
- shapes : ArrayList<Shape>
+ ShapePanel()
+ addShape(s : Shape)
use
interface
AbstractShapeFactory
+ createCircle(p : Point, radius : int) : Circle
+ createRectangle(p1 : Point, p2 : Point) : Rectangle
*
ShapeFactoryVersion1
ShapeFactoryVersion2
+ ShapeFactoryVersion1()
+ createCircle(p : Point, radius : int) : Circle
+ createRectangle(p1 : Point, p2 : Point) : Rectangle
+ ShapeFactoryVersion2()
+ createCircle(p : Point, radius : int) : Circle
+ createRectangle(p1 : Point, p2 : Point) : Rectangle
interface
Shape
+ draw(g : Graphics)
instancie
Circle
instancie
instancie
Rectangle
- c : Point
- radius : int
- p1 : Point
- p2 : Point
instancie
NewCircle
+ NewCircle(c : Point, radius : int)
+ draw(g : Graphics)
Programmation Orient
ee Objet
29 mai 2012
265 / 379
Java
Cours 8
FileLoader
add shape
add shape
+ addShape(s : Shape)
+ removeShape(s : Shape)
ShapePanel
- shapes : ArrayList <Shape>
+ ShapePanel()
+ addShape(s : Shape)
ShapePanel
- shapes : ArrayList <Shape>
+ ShapePanel()
+ addShape(s : Shape)
+ removeShape(s : Shape)
Programmation Orient
ee Objet
29 mai 2012
266 / 379
Java
Cours 8
Programmation Orient
ee Objet
29 mai 2012
267 / 379
Java
Cours 8
Programmation Orient
ee Objet
29 mai 2012
268 / 379
Java
Cours 8
add shape
use
ShapePanel
*
int erf ace
Shape
ShapeFact oryVersion1
Circle
ShapeFact oryVersion2
inst ancie
Rect angle
inst ancie
NewCircle
Programmation Orient
ee Objet
29 mai 2012
269 / 379
Java
Cours 8
Ils ont ete formalises dans le livre du Gang of Four ( Erich Gamma,
Richard Helm, Ralph Johnson et John Vlissides 1995)
Programmation Orient
ee Objet
29 mai 2012
270 / 379
Java
Cours 8
Monteur (Builder)
Prototype (Prototype)
Singleton (Singleton)
Programmation Orient
ee Objet
29 mai 2012
271 / 379
Java
Cours 8
Adaptateur (Adapter)
Pont (Bridge)
Decorateur (Decorator)
Facade (Facade)
Proxy (Proxy)
Programmation Orient
ee Objet
29 mai 2012
272 / 379
Java
Cours 8
Commande (Command)
Interpreteur (Interpreter)
Iterateur (Iterator)
Mediateur (Mediator)
Memento (Memento)
I
I
Observateur (Observer)
Etat
(State)
Strategie (Strategy)
Visiteur (Visitor)
Programmation Orient
ee Objet
29 mai 2012
273 / 379
Java
Cours 8
Fabrique abstraite
Utilisateur
utilise
interface
FabriqueAbstraite
interface
ProduitAbstraitB
utilise
creerProduitA() : ProduitAbstraitA
creerProduitB() : ProduitAbstraitB
FabriqueConcrete2
FabriqueConcrete1
creerProduitA() : ProduitAbstraitA
creerProduitB() : ProduitAbstraitB
creerProduitA() : ProduitAbstraitA
creerProduitB() : ProduitAbstraitB
instancie
ProduitB2
utilise
ProduitB1
instancie
instancie
instancie
ProduitA2
interface
ProduitAbstraitA
ProduitA1
Programmation Orient
ee Objet
29 mai 2012
274 / 379
Java
Cours 8
Adapteur
Utilisateur
+ Utilisateur()
utilise
interface
InterfaceCible
+ methodA(a : String, b : int)
+ methodB(b : int, a : String)
methodA(a,b) method1(b,a)
methodB(b,a) method2(b,a)
ClasseExistante
+ ClasseExistante()
+ method1(b : int, a : String)
+ method2(b : int, a : String)
Programmation Orient
ee Objet
29 mai 2012
275 / 379
Java
Cours 8
Adapteur
Utilisateur
+ Utilisateur()
ClasseExistante classeExistante;
interface
InterfaceCible
public Adapteur() {
classeExistante = new ClasseExistante();
}
Adapteur
~ classeExistante : ClasseExistante
+ Adapteur()
+ methodA(a : String, b : int)
+ methodB(b : int, a : String)
dlgue
ClasseExistante
+ ClasseExistante()
+ method1(b : int, a : String)
+ method2(b : int, a : String)
Programmation Orient
ee Objet
29 mai 2012
276 / 379
Java
Cours 8
Proxy
Client
+ Client()
utilise
interface
IConteneur
+ set(v : int)
+ get() : int
- v : int
+ Conteneur()
+ set(v : int)
+ get() : int
Programmation Orient
ee Objet
29 mai 2012
277 / 379
Java
Cours 8
Proxy
Client
+ Client()
utilise
interface
IConteneur
public ConteneurNonModifiable(Conteneur c) {
classeExistante = c;
}
+ set(v : int)
+ get() : int
ConteneurNonModifiable (Proxy)
- classeExistante : Conteneur
+ ConteneurNonModifiable(c : Conteneur)
+ set(v : int)
+ get() : int
dlgue
Conteneur
- v : int
+ Conteneur()
+ set(v : int)
+ get() : int
}
Programmation Orient
ee Objet
29 mai 2012
278 / 379
Java
Cours 8
Patron de methode
Exemple dutilisation du patron de m
ethode :
OperateurNAire
evaluer() : double
evaluerBinaire(a : int, b : int) : int
evaluerVide() : int
I
I
Produit
Somme
Programmation Orient
ee Objet
29 mai 2012
279 / 379
Java
Cours 8
Programmation Orient
ee Objet
29 mai 2012
280 / 379
Java
Cours 8
Programmation Orient
ee Objet
29 mai 2012
281 / 379
Java
Cours 8
Programmation Orient
ee Objet
29 mai 2012
282 / 379
Java
Cours 9
Les noms des classes qui g`erent les flux se terminent par :
Flux dentree
Flux de sortie
Flux doctets
InputStream
OutputStream
Flux de caract`eres
Reader
Writer
Les classes qui g`erent les flux sont dans le package java.io
Programmation Orient
ee Objet
29 mai 2012
283 / 379
Java
Cours 9
Programmation Orient
ee Objet
29 mai 2012
284 / 379
Java
Cours 9
...
Programmation Orient
ee 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
Programmation Orient
ee Objet
29 mai 2012
286 / 379
Java
Cours 9
int read(byte[] b, int off, int len) : lit une sequence doctets
Programmation Orient
ee Objet
29 mai 2012
287 / 379
Java
Cours 9
Sortie :
97
98
99
10
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
288 / 379
Java
Cours 9
Programmation Orient
ee Objet
29 mai 2012
289 / 379
Java
Cours 9
Programmation Orient
ee Objet
29 mai 2012
290 / 379
Java
Cours 9
Programmation Orient
ee 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
Programmation Orient
ee Objet
29 mai 2012
292 / 379
Java
Cours 9
int read(char[] b, int off, int len) : lit une sequence de caract`eres
Programmation Orient
ee Objet
29 mai 2012
293 / 379
Java
Cours 9
Sortie :
L1
L2
L3
Programmation Orient
ee Objet
29 mai 2012
294 / 379
Java
Cours 9
Sortie :
1 : L1
2 : L2
3 : L3
Programmation Orient
ee Objet
29 mai 2012
295 / 379
Java
Cours 9
ByteArrayOutputStream : Ecriture
dans un tableau doctets
FileOutputStream : Ecriture
dans un fichier
ObjectOutputStream : Ecriture
dobjets dans un OutputStream
PipedOutputStream : Ecriture
dans un tube
Programmation Orient
ee Objet
29 mai 2012
296 / 379
Java
Cours 9
...
Programmation Orient
ee Objet
29 mai 2012
297 / 379
Java
Cours 9
Sortie :
97
98
99
abc
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
298 / 379
Java
Cours 9
int write(byte[] b, int off, int len) : ecrit une sequence doctets
Programmation Orient
ee Objet
29 mai 2012
299 / 379
Java
Cours 9
Programmation Orient
ee Objet
29 mai 2012
300 / 379
Java
Cours 9
CharArrayWriter : Ecriture
dans un tableau de caract`eres
BufferedWriter : Ecriture
avec un tapon
OutputStreamWriter : Ecriture
dans un flux doctets
I
FileWriter : Ecriture
dans un fichier
PipedWriter : Ecriture
dans un tube
StringWriter : Ecriture
dans une chane de caract`eres
PrintWriter : Ecriture
avec des methodes de formatage
Programmation Orient
ee Objet
29 mai 2012
301 / 379
Java
Cours 9
Programmation Orient
ee Objet
29 mai 2012
302 / 379
Java
Cours 9
Ecriture
formatee dans un fichier :
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 ( ) ;
Programmation Orient
ee Objet
29 mai 2012
303 / 379
Java
Cours 9
System.{out,in,err}
Dans la classe System :
I
I
I
I
public
public
public
public
static
static
static
static
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 Orient
ee Objet
29 mai 2012
304 / 379
Java
Cours 9
System.console()
Dans la classe Console :
I
Programmation Orient
ee Objet
29 mai 2012
305 / 379
Java
Cours 9
IOError
IOException
I
I
I
I
EOFException
FileNotFoundException
ZipException
CharacterCodingException
I
I
MalformedInputException
UnmappableCharacterException
...
IllegalFormatException
Exemple :
try {
F i l e R e a d e r r e a d e r = new F i l e R e a d e r ( t r u c . t x t ) ;
} c a t c h ( F i l e N o t F o u n d E x c e p t i o n ex ) {
System . o u t . p r i n t l n ( t r u c . t x t n e x i s t e p a s ! ) ;
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
306 / 379
Java
Cours 9
Programmation Orient
ee Objet
29 mai 2012
307 / 379
Java
Cours 9
Java et le multitache
Programmation Orient
ee Objet
29 mai 2012
308 / 379
Java
Cours 9
Linterface Runnable
I
Programmation Orient
ee Objet
29 mai 2012
309 / 379
Java
Cours 9
La classe Thread
I
Programmation Orient
ee Objet
29 mai 2012
310 / 379
Java
Cours 9
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 Orient
ee Objet
29 mai 2012
311 / 379
Java
Cours 9
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 Orient
ee Objet
29 mai 2012
312 / 379
Java
Cours 9
La classe Thread
Programmation Orient
ee Objet
29 mai 2012
313 / 379
Java
Cours 9
Letat dinterruption
I
Programmation Orient
ee 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 ( ) ;
}
}
Programmation Orient
ee 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 ( ) ;
}
}
Programmation Orient
ee Objet
29 mai 2012
316 / 379
Java
Cours 9
Letat dinterruption
Thread qui interrompt un autre thread apr`es 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 Orient
ee 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
Compteur
100
200
300
interrupt()
1
Stopper
temps
350
Programmation Orient
ee Objet
temps
29 mai 2012
318 / 379
Java
Cours 9
Probl`eme 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 Orient
ee 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 ;
public Empileur ( PileLente p i l e ) {
this . pile = pile ;
}
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 Orient
ee Objet
29 mai 2012
320 / 379
Java
Cours 9
Synchronisation
Execution en parall`ele 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 : O`
u sont passes les 10 elements manquants ?
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
321 / 379
Java
Cours 9
Synchronisation
Execution (en parall`ele) de la methode push dans deux threads :
Thread 1
int s = size ; (s = x)
tab[s] = p ;
sleep(100) ;
size = s + 1 ; (size = x + 1)
Thread 2
int s = size ; (s = x)
tab[s] = p ;
sleep(100) ;
size = s + 1 ; (size = x + 1)
Probl`eme : Deux entiers ont ete empiles et la pile et la taille de la pile est
passee de x `a x + 1
Programmation Orient
ee Objet
29 mai 2012
322 / 379
Java
Cours 9
Le mot-cle Synchronized
Interdire que deux invocations sur une meme instance sentremelent :
public class PileLente {
...
s y n c h r o n i z e d 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;
}
s y n c h r o n i z e d i n t pop ( ) { s i z e ; r e t u r n t a b [ s i z e ] ; }
...
}
Programmation Orient
ee Objet
29 mai 2012
323 / 379
Java
Cours 9
Programmation Orient
ee Objet
29 mai 2012
324 / 379
Java
Cours 9
Programmation Orient
ee Objet
29 mai 2012
325 / 379
Java
Cours 10
JavaDoc
I
Le document contient :
I
Programmation Orient
ee Objet
29 mai 2012
326 / 379
Java
Cours 10
Exemple de JavaDoc
Programmation Orient
ee Objet
29 mai 2012
327 / 379
Java
Cours 10
Exemple de JavaDoc
Programmation Orient
ee Objet
29 mai 2012
328 / 379
Java
Cours 10
Exemple de JavaDoc
Programmation Orient
ee Objet
29 mai 2012
329 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
330 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
331 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
332 / 379
Java
Cours 10
1.0
1.3
1.0
1.2
1.0
1.0
1.0
1.1
1.2
1.0
1.0
1.2
1.2
1.2
Programmation Orient
ee Objet
29 mai 2012
333 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
334 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
335 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
336 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
337 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
338 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
339 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
340 / 379
Java
Cours 10
Generation de la documentation
I
Programmation Orient
ee Objet
29 mai 2012
341 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
342 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
343 / 379
Java
Cours 10
Revision Classes
class UneClasse {
int field1;
String field2;
UneClasse(int p1) { /* Constructeur de Truc */
field1 = p1;
}
int method(int p1, int p2) {
return p1 + p2 + field1;
}
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
344 / 379
Java
Cours 10
M
ethode nom ( arguments ) : type
Programmation Orient
ee Objet
29 mai 2012
345 / 379
Java
Cours 10
Revision Interfaces
interface UneInterface {
/** Description du comportement de la m
ethode.
*
* @param p1 ...
*/
void method1(int p1);
/** Description du comportement de la m
ethode.
*
* @param p1 ...
* @param p2 ...
* @return ....
*/
int method2(int p2, int p3);
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
346 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
347 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
348 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
349 / 379
Java
Cours 10
Programmation Orient
ee 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 Orient
ee 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"); }
}
Laquelle des deux methodes est appelee ?
int test(boolean choix)
A a; if (choix) a = new A(); else i = new B();
a.method();
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
352 / 379
Java
Cours 10
Revision Enum
eration
enum Bidule {
Truc,
Machin,
Chose
}
Programmation Orient
ee Objet
29 mai 2012
353 / 379
Java
Cours 10
Enum
eration (la suite)
Il est possible dassocier de linformation aux valeurs :
enum Bidule {
Truc ("Truc", 5),
Machin ("Machin", 4),
Chose ("Chose", 8)
private final double nom;
private final double prix;
Bidule(String nom, int prix) {
this.nom = nom;
this.prix = prix;
}
private double nom()
{ return nom; }
private double prix() { return prix; }
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
354 / 379
Java
Cours 10
Enum
eration (la suite)
Utilisation :
public static void main(String[] args) {
for (Bidule b : Bidule.values())
System.out.printf("Le prix de %s est %d",
b.nom(),
b.prix());
}
Programmation Orient
ee Objet
29 mai 2012
355 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
356 / 379
Java
Cours 10
Par defaut, une classe ou une methode est non-publique : elle nest
accessible que depuis les classes du m
eme paquet.
Programmation Orient
ee Objet
29 mai 2012
357 / 379
Java
Cours 10
Un membre priv
e nest accessible que par les methodes de la classe
qui le contient.
Programmation Orient
ee Objet
29 mai 2012
358 / 379
Java
Cours 10
Un membre prot
eg
e est accessible depuis :
I
I
Programmation Orient
ee Objet
29 mai 2012
359 / 379
Java
Cours 10
#
+
:
:
:
:
prive (private)
protege (protected)
publique (public)
non-publique (default)
Programmation Orient
ee Objet
29 mai 2012
360 / 379
Java
Cours 10
Programmation Orient
ee 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; }
}
Programmation Orient
ee Objet
29 mai 2012
362 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
363 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
364 / 379
Java
Cours 10
Revision this
class Machin {
private String nom;
private int prix;
public Machin(String nom, int prix) {
this.nom = nom;
this.prix = prix;
}
int ajouterPrix(int prix) {
return prix + this.prix;
}
}
Programmation Orient
ee 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; }
}
class Machin extends Truc {
private int prix;
public Machin(String nom, int prix) {
super(nom); this.prix = prix;
}
public String toString() {
return super.toString() + " "+prix;
}
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
366 / 379
Java
Cours 10
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) {
...
}
On les fait remonter avec throws :
void method() throws MyException {
...
}
Bertrand Estellon (DIL Aix-Marseille)
Programmation Orient
ee Objet
29 mai 2012
367 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
368 / 379
Java
Cours 10
byte
short
int
long
float
double
char
boolean
entier
entier
entier
entier
flotant
flotant
caract`ere
boolean
8 bits
16 bits
32 bits
64 bits
32 bits
64 bits
16 bits
1 bit
-128 `a 127
-32768 `a 32767
231 231 1
263 263 1
caract`eres Unicode
false ou true
0
0
0
0
0.0
0.0
\u0000
false
int a = 12;
double b = 1 3 . 5 ;
Programmation Orient
ee Objet
29 mai 2012
369 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
370 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
371 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
372 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
373 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
374 / 379
Java
Cours 10
Programmation Orient
ee Objet
29 mai 2012
375 / 379
Java
Cours 10
Eviter
les interfaces qui contiennent beaucoup de methodes
Programmation Orient
ee Objet
29 mai 2012
376 / 379
Java
Cours 10
Ils ont ete formalises dans le livre du Gang of Four ( Erich Gamma,
Richard Helm, Ralph Johnson et John Vlissides 1995)
Programmation Orient
ee Objet
29 mai 2012
377 / 379
Java
Cours 10
...
Programmation Orient
ee Objet
29 mai 2012
378 / 379
Java
Cours 10
Python
Ruby
Programmation Orient
ee Objet
29 mai 2012
379 / 379