iti 1521. introduction à l'informatique ii -...

61
ITI 1521. Introduction à l’informatique II Programmation orientée objet : visibilité, variables et méthodes de classe by Marcel Turcotte Version du 20 janvier 2020

Upload: others

Post on 20-Jan-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

ITI 1521. Introduction à l’informatique IIProgrammation orientée objet : visibilité, variables et méthodes de classe

by

Marcel Turcotte

Version du 20 janvier 2020

Page 2: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Préambule

Page 3: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Préambule

Aperçu

Page 4: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Programmation orientée objet : visibilité, variables et méthodes de classe

Nous découvrons le rôle des modificateurs de visibilité afin de favoriser l’encapsulation.Nous ajoutons à nos connaissances les concepts de variables et méthodes de classe.Finalement, nous verrons le rôle d’une variable référence prédéfinie, this.

Objectif général :Cette semaine, vous serez en mesure de comparer les concepts de variable d’instanceet variables de classes, ainsi que de méthodes d’instance et de méthodes de classe.

Une vidéo d’introduction :https://www.youtube.com/watch?v=OFFzkropD1A

Page 5: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Préambule

Objectifs d’apprentissage

Page 6: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Objectifs d’apprentissage

Décriver les mécanismes de Java qui favorisent l’encapsulation.Expliquer dans vos propres mots les concepts suivants : variable d’instance etvariable de classe, méthode d’instance et méthode de classe.Concevoir un programme Java simple illustrant les concepts de base de laprogrammation orientée objet.

Lectures :Pages 573-579 de E. Koffman et P. Wolfgang.

2 51

Page 7: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Lectures (suite) :

Entrée en matièredocs.oracle.com/javase/tutorial/java/concepts/object.htmldocs.oracle.com/javase/tutorial/java/concepts/class.html

Informations détailléesdocs.oracle.com/javase/tutorial/java/javaOO/classes.htmldocs.oracle.com/javase/tutorial/java/javaOO/objects.htmldocs.oracle.com/javase/tutorial/java/javaOO/more.htmldocs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Exercisesdocs.oracle.com/javase/tutorial/java/concepts/QandE/questions.html

docs.oracle.com/javase/tutorial/java/javaOO/QandE/creating-questions.html

3 51

Page 8: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Préambule

Plan du module

Page 9: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Plan

1 Préambule

2 Encapsulation

3 Contexte de la classe

4 « What is this ? »

5 Final

6 Prologue

4 51

Page 10: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Encapsulation

Page 11: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Définition : encapsulation

En programmation orientée objet, l’encapsulation consiste à regrouper en une mêmeunité (l’objet) les données et les méthodes qui les manipulent.

5 51

Page 12: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Java : modificateurs de visibilité

En Java, les modificateurs de visibilité nous permettent de contrôler l’accès desvariables et des méthodes.pub l i c c l a s s Po in t {

p r i v a t e i n t x , y ;

pub l i c i n t getX ( ) {re tu rn x ;

}

pub l i c vo id setX ( i n t v a l u e ) {x = v a l u e ;

}

}

6 51

Page 13: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Java : modificateurs de visibilité

pub l i c c l a s s Po in t {

p r i v a t e i n t x , y ;

pub l i c i n t getX ( ) { re tu rn x ; }

pub l i c i n t getY ( ) { re tu rn y ; }

pub l i c boolean e q u a l s ( Po in t o t h e r ) {i f ( o t h e r == nu l l ) {

re tu rn f a l s e ;} e l s e {

re tu rn x == o t h e r . getX ( ) && y == o t h e r . getY ( ) ;}

}

}

7 51

Page 14: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Java : modificateurs de visibilité

Est-ce que la déclaration de la méthode equals est valide ?

pub l i c c l a s s Po in t {

p r i v a t e i n t x , y ;

pub l i c i n t getX ( ) { re tu rn x ; }

pub l i c i n t getY ( ) { re tu rn y ; }

pub l i c boolean e q u a l s ( Po in t o t h e r ) {i f ( o t h e r == nu l l ) {

re tu rn f a l s e ;} e l s e {

re tu rn x == o t h e r . x && y == o t h e r . y ;}

}}

8 51

Page 15: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Exemple : encapsulation

pub l i c c l a s s Test {pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

Po in t p ;p = new Po in t ( ) ;p . x = 4 ;

}}

javac Test.javaTest.java:5: error: x has private access in Point

p.x = 4;^

1 error

9 51

Page 16: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Java : modificateurs de visibilité

En Java, les modificateurs de visibilité nous permettent de contrôler l’accès desvariables et des méthodes.

Une variable ou une méthode dont la visibilité est private n’est accessible que dansle corps de la classe où elle est définie.Une variable ou une méthode dont la visibilité est public est accessible dans lecorps de la classe où elle est définie, mais aussi dans toutes les autres classes duprogramme.

En ITI1521, les variables d’instance devraient toujours être déclarées de visibilitéprivate !

10 51

Page 17: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Discussion : encapsulation

Analysez attentivement les deux implémentations de la classe Pair qui suivent.Peut-on remplacer une implémentation par l’autre sans causer d’erreurs decompilation ou d’exécution pour les autres classes d’une application ?

11 51

Page 18: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

pub l i c c l a s s P a i r {

p r i v a t e i n t f i r s t ;p r i v a t e i n t second ;

pub l i c P a i r ( i n t f i r s t I n i t , i n t s e c o n d I n i t ) {f i r s t = f i r s t I n i t ;second = s e c o n d I n i t ;

}pub l i c i n t g e t F i r s t ( ) {

re tu rn f i r s t ;}pub l i c i n t getSecond ( ) {

re tu rn second ;}pub l i c vo id s e t F i r s t ( i n t v a l u e ) {

f i r s t = v a l u e ;}pub l i c vo id se tSecond ( i n t v a l u e ) {

second = v a l u e ;}

}

12 51

Page 19: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

pub l i c c l a s s P a i r {

p r i v a t e i n t [ ] e l ems ;

pub l i c P a i r ( i n t f i r s t , i n t second ) {e lems = new i n t [ 2 ] ;e l ems [ 0 ] = f i r s t ;e l ems [ 1 ] = second ;

}pub l i c i n t g e t F i r s t ( ) {

re tu rn e lems [ 0 ] ;}pub l i c i n t getSecond ( ) {

re tu rn e lems [ 1 ] ;}pub l i c vo id s e t F i r s t ( i n t v a l u e ) {

e lems [ 0 ] = v a l u e ;}pub l i c vo id se tSecond ( i n t v a l u e ) {

e lems [1]= v a l u e ;}

}

13 51

Page 20: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Discussion : encapsulation

Peut-on remplacer une implémentation par l’autre sans causer d’erreurs decompilation ou d’exécution pour les énoncés suivants ?

P a i r p ;p = new P a i r (2 , 4 ) ;

System . out . p r i n t l n ( p . g e t F i r s t ( ) ) ;

p . s e tSecond ( 1 2 ) ;

14 51

Page 21: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Discussion : encapsulation (prise 2)

pub l i c c l a s s Po in t {

p r i v a t e i n t xp r i v a t e i n t y ;

pub l i c vo id setX ( i n t v a l u e ) {i f ( v a l u e < 0 | | v a l u e > 1024) {

x = 0 ;} e l s e {

x = v a l u e ;}

}}

15 51

Page 22: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Discussion : méthode private

Pouvez-vous imaginer une situation où l’on souhaiterait déclarer une méthode«private» ?

16 51

Page 23: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Discussion : méthode privatepub l i c c l a s s Po in t {

p r i v a t e i n t xp r i v a t e i n t y ;

p r i v a t e boolean i s V a l i d ( i n t v a l u e ) {i f ( v a l u e < 0 | | v a l u e > 1024) {

re tu rn t rue ;} e l s e {

re tu rn f a l s e ;}

}pub l i c vo id setX ( i n t v a l u e ) {

i f ( i s V a l i d ( v a l u e ) ) {x = v a l u e ;

} e l s e {x = 0 ;

}}

}

17 51

Page 24: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Discussion : interface

L’«interface» d’une classe est constituée des méthodes publiquesLes méthodes qui ne devraient pas faire partie de l’interface seront déclarées«private»

18 51

Page 25: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Contexte de la classe

Page 26: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Définition : variable de classe

Une variable de classe est une variable définie dans le corps de la classe et qui estpartagée par les instances (objets) de cette classe.pub l i c c l a s s Po in t {

pub l i c s t a t i c i n t MAX_VALUE = 100 ;p r i v a t e i n t xp r i v a t e i n t y ;

pub l i c vo id moveRight ( ) {x = x + 1 ;i f ( x > MAX_VALUE) {

x = 0 ;}

}}

19 51

Page 27: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Définition : variable de classe

Le mot clé static introduit une variable de classe. Comme vous le voyez, il faut faireun effort afin de créer une variable de classe, ce n’est pas ce que l’on obtient pardéfaut.

pub l i c c l a s s Po in t {pub l i c s t a t i c i n t MAX_VALUE = 100 ;p r i v a t e i n t xp r i v a t e i n t y ;

pub l i c vo id moveRight ( ) {x = x + 1 ;i f ( x > MAX_VALUE) {

x = 0 ;}

}}

20 51

Page 28: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Variable de classe

La déclaration d’une constant est bon exemple d’une situation où les variables declasses sont utiles.Pouvez-vous trouver d’autres exemples où les variables de classes pourraient êtreutiles ?

21 51

Page 29: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

pub l i c c l a s s Ticke t {

p r i v a t e s t a t i c i n t l a s t = 100 ;p r i v a t e i n t number ;

pub l i c Ticke t ( ) {number = l a s t ;l a s t=l a s t +1;

}

pub l i c i n t ge tSe r i a lNumbe r ( ) {re tu rn number ;

}}

22 51

Page 30: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

pub l i c c l a s s Ticke t {

p r i v a t e s t a t i c i n t l a s t = 100 ;p r i v a t e i n t number ;

pub l i c Ticke t ( ) {number = l a s t ;l a s t=l a s t +1;

}

pub l i c i n t ge tSe r i a lNumbe r ( ) {re tu rn number ;

}}

23 51

Page 31: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Définition : méthode de classe

Les méthodes de classe n’appartiennent pas à un objet en particulier. Elles sont doncpartagées par les instances de la classe. Puisqu’elle ne sont pas associées à un objet, ellesn’ont pas accès aux variables d’instance.

24 51

Page 32: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Méthode de classe

Est-ce que la méthode isValid utilise une variable d’instance ?

pub l i c c l a s s Po in t {

p r i v a t e i n t xp r i v a t e i n t y ;

p r i v a t e boolean i s V a l i d ( i n t v a l u e ) {i f ( v a l u e < 0 | | v a l u e > 1024) {

re tu rn t rue ;} e l s e {

re tu rn f a l s e ;}

}}

25 51

Page 33: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Méthode de classe

La méthode isValid devrait donc être une méthode de classe (static)

pub l i c c l a s s Po in t {

p r i v a t e i n t xp r i v a t e i n t y ;

p r i v a t e s t a t i c boolean i s V a l i d ( i n t v a l u e ) {i f ( v a l u e < 0 | | v a l u e > 1024) {

re tu rn t rue ;} e l s e {

re tu rn f a l s e ;}

}}

26 51

Page 34: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Méthode de classe

pub l i c c l a s s Po in t {

p r i v a t e i n t xp r i v a t e i n t y ;

pub l i c s t a t i c i n t compareTo ( i n t a , i n t b ) {i n t r e s u l t ;i f ( a < b ) {

r e s u l t = −1;} e l s e i f ( a == b ) {

r e s u l t = 0 ;} e l s e {

r e s u l t = 1 ;}

re tu rn r e s u l t ;}

}

27 51

Page 35: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Exemples de méthodes de classe

http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html

http://docs.oracle.com/javase/8/docs/api/java/lang/System.html

28 51

Page 36: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Appel à une méthode de classe («staticcontext»)

On utilise le nom de la classe suivi du nom de la méthode pour faire un appel àune méthode de classe.On utilise le nom de la classe suivi du nom de la variable pour accéder à unevariable de classe.

double a ;

a = Math . abs ( −1 .6) ;

a = Math . max (1024 . 0 , Double .MAX_VALUE) ;

a = Math . random ( ) ;

a = Math . s q r t ( Double .MAX_VALUE) ;

a = Math . pow ( 2 . 0 , 3 2 . 0 ) ;

29 51

Page 37: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

« What is this ? »

Page 38: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

« What is this ? »

Chaque objet possède une référence this. Elle désigne l’objet lui-même.

pub l i c c l a s s BankAccount {

p r i v a t e double ba l ance ;

// . . .

pub l i c boolean t r a n s f e r ( BankAccount other , double a ) {

i f ( t h i s == o t h e r ) {re tu rn f a l s e ;

}

. . .}

}

30 51

Page 39: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

45,000,000

angelina

balance

transfer( )

angelina.transfer( brad, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

args

...

31 51

Page 40: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

45,000,000

angelina

balance

transfer( )

angelina.transfer( brad, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

this

Activation Framefor transfer

args

...

othera

32 51

Page 41: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

45,000,000

angelina

balance

transfer( )

angelina.transfer( brad, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

this

Activation Framefor transfer

args

...

other100a

33 51

Page 42: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

45,000,000

angelina

balance

transfer( )

brad.transfer( angelina, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

args

...

34 51

Page 43: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

45,000,000

angelina

balance

transfer( )

brad.transfer( angelina, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

this

Activation Framefor transfer

args

...

othera

35 51

Page 44: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

45,000,000

angelina

balance

transfer( )

brad.transfer( angelina, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

this

Activation Framefor transfer

args

...

other100a

36 51

Page 45: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

45,000,000

angelina

balance

transfer( )

angelina.transfer( angelina, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

args

...

37 51

Page 46: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

45,000,000

angelina

balance

transfer( )

angelina.transfer( angelina, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

this

Activation Framefor transfer

args

...

other100a

38 51

Page 47: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

« What is this »

pub l i c c l a s s Date {p r i v a t e i n t day ;p r i v a t e i n t month ;pub l i c Date ( i n t day , i n t month ) {

t h i s . day = day ;t h i s . month = month ;

}// . . .pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

Date d ;i n t day , month ;day = 17 ; month = 1 ;d = new Date ( day , month ) ;

}}

39 51

Page 48: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

d

1

17Activation Frame(working memory)for main

args

...

monthday

⇒ Mémoire de travail de la méthode principale (main)40 51

Page 49: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

d

day

1

17Activation Frame(working memory)for main

args

...

monthday

month

⇒ On exécute “new Date(day,month)”, création d’un objet41 51

Page 50: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

d

day

1

17Activation Frame(working memory)for main

this

Activation Frame(working memory)for transfer

args

...

daymonth

monthday

month

⇒ Appel au constructeur, création de la mémoire de travail associée42 51

Page 51: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

d

day

1

17

1

Activation Frame(working memory)for main

this

Activation Frame(working memory)for transfer

args

...

17daymonth

monthday

month

⇒ Copie les valeurs de paramètres actuels dans les paramètres formels43 51

Page 52: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

17

d

day

1

17

1

Activation Frame(working memory)for main

this

Activation Frame(working memory)for transfer

args

...

17daymonth

monthday

1

month

⇒ On exécute le corps du constructeur, on copie day dans this.day, month dansthis.month

44 51

Page 53: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

17

d

day

1

17Activation Frame(working memory)for main

args

...

monthday

1

month

⇒ Le constructeur termine, on détruit le bloc de mémoire de travail, on assigne laréférence à la variable d

45 51

Page 54: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

« What is this ? »

pub l i c c l a s s Date {p r i v a t e i n t day ;p r i v a t e i n t month ;pub l i c Date ( i n t day , i n t month ) {

t h i s . day = day ;t h i s . month = month ;

}// . . .pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

Date d ;i n t day , month ;day = 17 ; month = 1 ;d = new Date ( day , month ) ;

}}

⇒ this lève l’ambiguïté, il permet de distinguer le paramètre day de la variable d’instanceday.

46 51

Page 55: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Final

Page 56: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Java : final

Pour une variable, le mot-clé final signifie qu’on ne peut changer sa valeur.

pub l i c c l a s s Po in t {pub l i c s t a t i c f i n a l i n t MAX_VALUE = 100 ;p r i v a t e i n t xp r i v a t e i n t y ;

pub l i c vo id moveRight ( ) {x = x + 1 ;i f ( x > MAX_VALUE) {

x =0;}

}}

47 51

Page 57: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Prologue

Page 58: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Résumé

L’encapsulation consiste à mettre les données et les opérations qui les transformentdans une même unité (l’objet)Les modificateurs de visibilité supportent l’encapsulationLes variables et les méthodes de classe sont partagées par les instances de laclasse, et de toutes autres classes si leur visibilité est publicChaque objet possède une référence this. Elle désigne l’objet lui-même.

48 51

Page 59: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Prochain module

Interface

49 51

Page 60: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

References I

E. B. Koffman and Wolfgang P. A. T.Data Structures : Abstraction and Design Using Java.John Wiley & Sons, 3e edition, 2016.

50 51

Page 61: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures... · 2020. 1. 20. · ITI1521.Introductionàl’informatiqueII Programmationorientéeobjet:

Marcel [email protected]

École de science informatique et de génie électrique (SIGE)Université d’Ottawa

51 / 51