Licence 1 MASS, parcours SEMS Introduction ` a Java et ` a l’algorithmique
S´ebastien Verel
verel@i3s.unice.fr www.i3s.unice.fr/∼verel
´Equipe ScoBi - Universit´e Nice Sophia Antipolis
12 avril 2013
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable
Synth` ese
El` eves de synth` ese du jour ?
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable
Objectifs de la s´ eance 8
Savoir utiliser ”this” en Processing
Savoir utiliser un h´ eritage simple entre classes en Processing Savoir utiliser la classe ArrayList
Comment h´ eriter sans soucis ?
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable
Objectifs de la s´ eance 8
Savoir utiliser ”this” en Processing
Savoir utiliser un h´ eritage simple entre classes en Processing Savoir utiliser la classe ArrayList
Question principale du jour :
Comment h´ eriter sans soucis ?
Plan
1
Utilisation de ”this”
2
H´ eritage avec Processing
3
Donn´ ees index´ ees de taille variable
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable
Utilisation de This
Voir la premi` ere partie du cours J.P. Roy :
Compl´ ements sur les objets
La classe Balle (CM 07)
Champs :
float x, y float vx, vy float rfloat friction
Constructeur :
Balle(int rInit, float fInit, float xInit, float yInit, float vxInit, float vyInit)
M´ ethodes :
void dessineToi() void bouge()
public String toString()
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable
Programme principal
Balle b1, b2;
void setup() { size(400, 400);
fill(0, 0, 255);
smooth();
b1 = new Balle(10, 0.95, 0, 0, 2, 0);
b2 = new Balle(20, 0.98, 50, 0, 0.0, -0.5);
println("Balle initiale : " + b1);
println("Balle initiale : " + b2);
}
Programme principal
void draw() {
background(255, 255, 0);
b1.dessineToi();
b2.dessineToi();
b1.bouge();
b2.bouge();
}
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable
Balle cubique
Et si on cr´ eait une balle cubique ? ou balle ` a image ?
Id´ ee
R´ ecup´ erer l’expertise de la classe
BalleChanger seulement ce qui est sp´ ecifique ` a une balle cubique
Balle cubique
Et si on cr´ eait une balle cubique ? ou balle ` a image ?
Id´ ee
R´ ecup´ erer l’expertise de la classe
BalleChanger seulement ce qui est sp´ ecifique ` a une balle cubique
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable
La classe Cube
class Cube extends Balle { ...
...
...
}
Le mot cl´ e
extendspermet d’utiliser toutes les champs et m´ ethodes de la classe
Balledepuis la classe
Cube:
la classe Cube
h´eritede la classe
Balle Balleest la classe m` ere de la classe
Cube Cubeest la classe fille de la classe
BalleLa classe Cube : constructeur
class Cube extends Balle {
Cube(int rInit, float fInit, float xInit, float yInit, float vxInit, float vyInit) { super(rInit, fInit, xInit, yInit, vxInit, vyInit);
} }
Le mot cl´ e
superfait r´ ef´ erence ` a la classe m` ere (sup´ erieure)
Donc
super(...)fait appel au constructeur de la classe
m` ere (sup´ erieure)
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable
La classe Cube : 2 constructeurs
class Cube extends Balle {
Cube(int rInit, float fInit, float xInit, float yInit, float vxInit, float vyInit) { super(rInit, fInit, xInit, yInit, vxInit, vyInit);
}
Cube(int rInit, float fInit) { this(rInit, fInit,
random(width), random(height / 4, height), random(-2, 2), random(-2, 0));
} }
La classe Cube : m´ ethode sp´ ecifique
class Cube extends Balle {
Cube(int rInit, float fInit, float xInit, float yInit, float vxInit, float vyInit) { super(rInit, fInit, xInit, yInit, vxInit, vyInit);
}
Cube(int rInit, float fInit) { this(rInit, fInit,
random(width), random(height / 4, height), random(-2, 2), random(-2, 0));
}
void dessineToi() { rect(x, y, r, r);
}
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable
Barre
Et si on cr´ eait une barre qui rebondit (bizarrement) ?
La classe Barre
class Barre extends Balle { ...
...
...
}
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable
La classe Barre : champ sp´ ecifique
class Barre extends Balle { float orientation;
...
...
}
La barre a une orientation (un angle).
La classe Barre : constructeurs
class Barre extends Balle { float orientation;
Barre(int rInit, float fInit, float orientationInit,
float xInit, float yInit, float vxInit, float vyInit) { super(rInit, fInit, xInit, yInit, vxInit, vyInit);
orientation = orientationInit;
}
Barre(int rInit, float fInit) {
this(rInit, fInit, random(0, TWO_PI),
random(width), random(height / 2, height), random(-2, 2), random(-2, 0));
} }
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable
La classe Barre : 1 m´ ethode
class Barre extends Balle { (...)
void dessineToi() { line(x, y,
x + r * cos(orientation), y + r * sin(orientation));
} }
La classe Barre : une autre m´ ethode sp´ ecifique
class Barre extends Balle { (...)
void dessineToi() { line(x, y,
x + r * cos(orientation), y + r * sin(orientation));
}
void bouge() { super.bouge();
orientation = orientation + PI / 50;
} }
Utilisation de ”this”
H´eritage avec Processing Donn´ees index´ees de taille variable