Licence 1 MASS - Introduction programmation JAVA
Sébastien Verel [email protected] www.i3s.unice.fr/
∼verel
Équipe ScoBi - Université de Nice Sophia-Antipolis
26 mars 2012
Objectifs de la séance 9
Savoir utiliser "this" en Processing
Savoir utiliser un héritage simple entre classes en Processing Savoir utiliser la classe ArrayList
Question principale du jour :
Comment hériter sans soucis ?
Plan
1
Utilisation de "this"
2
Héritage avec Processing
3
Données indexées de taille variable
Utilisation de This
Voir la première partie du cours J.P. Roy :
Compléments sur les objets
La classe Balle (CM et TP 07 exo 3)
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éthodes :
void dessineToi() void bouge()
public String toString()
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();
}
Balle cubique
Et si on créait une balle cubique ?
Idée
Récupérer l'expertise de la classe Balle
Changer seulement ce qui est spécique à une balle cubique
La classe Cube
class Cube extends Balle { ...
...}
Le mot clé extends permet d'utiliser toutes les champs et méthodes de la classe Balle depuis la classe Cube :
la classe Cube hérite de la classe Balle
Balle est la classe mère de la classe Cube
Cube est la classe lle de la classe Balle
La 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é super fait référence à la classe mère (supérieure)
Donc super(...) fait appel au construsteur de la classe mère
(supérieure)
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éthode spécique
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);
Barre
Et si on créait une barre qui rebondit (bizarrement) ?
La classe Barre
class Barre extends Balle { ...
...}
La classe Barre : champ spécique
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));
}
La classe Barre : 1 méthode
class Barre extends Balle { (...)
void dessineToi() { line(x, y,
x + r * cos(orientation), y + r * sin(orientation));
} }
La classe Barre : une autre méthode spécique
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;
}