Classe

publicité
Notions de
classes et objets
en Java™
Type primitif
Classe
Notions de
classes et objets
en Java™
Attributs
Méthode
Constructeur
Objets
Balle rebondissante
V1
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Variable et type primitif
Notions de
classes et objets
en Java™
Type primitif
int a;
type (primitif)
variable
Classe
Attributs
Méthode
Constructeur
Objets
Balle rebondissante
V1
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Variable et type primitif
Notions de
classes et objets
en Java™
Type primitif
int a;
type (primitif)
Classe
variable
Attributs
Méthode
Constructeur
Objets
CPU
Balle rebondissante
V1
V2
Changement scène
void main(String[] args) {
int note1 = 15;
int note2 = 18;
double moyenne = (note1+note2)/2.0;
}
Plus loin
Héritage
Contrat
static
main
note1
note2
Variable et type primitif
Notions de
classes et objets
en Java™
Type primitif
int a;
type (primitif)
Classe
variable
Attributs
Méthode
Constructeur
Objets
CPU
Balle rebondissante
V1
V2
Changement scène
void main(String[] args) {
int note1 = 15;
int note2 = 18;
double moyenne = (note1+note2)/2.0;
}
Plus loin
Héritage
Contrat
static
main
note1
note2
type primitif=mot clé du langage, débute par minuscule
Classe et objets
Notions de
classes et objets
en Java™
Type primitif
ImageIcon img;
Classe
Attributs
Méthode
classe
objet
Constructeur
Objets
Balle rebondissante
V1
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Classe et objets
Notions de
classes et objets
en Java™
Type primitif
ImageIcon img;
Classe
Attributs
Méthode
Constructeur
objet
classe
Objets
Balle rebondissante
void main(String[] args) {
ImageIcon i1 = new ImageIcon("im1.png");
ImageIcon i2 = new ImageIcon("im2.png");
int width = img.getWidth();
}
classe
ImageIcon
V1
objets
i1 : ImageIcon
i2 : ImageIcon
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Classe et objets
Notions de
classes et objets
en Java™
Type primitif
ImageIcon img;
Classe
Attributs
Méthode
Constructeur
objet
classe
Objets
Balle rebondissante
void main(String[] args) {
ImageIcon i1 = new ImageIcon("im1.png");
ImageIcon i2 = new ImageIcon("im2.png");
int width = img.getWidth();
}
classe
ImageIcon
V1
objets
i1 : ImageIcon
i2 : ImageIcon
V2
Changement scène
Plus loin
Héritage
Contrat
static
Classe=hors langage, débute par majuscule
main
Classe et objets
Notions de
classes et objets
en Java™
Type primitif
ImageIcon img;
Classe
Attributs
Méthode
Constructeur
objet
classe
Objets
Balle rebondissante
void main(String[] args) {
ImageIcon i1 = new ImageIcon("im1.png");
ImageIcon i2 = new ImageIcon("im2.png");
int width = img.getWidth();
}
classe
ImageIcon
V1
objets
i1 : ImageIcon
i2 : ImageIcon
V2
Changement scène
Plus loin
Héritage
Contrat
static
Classe=hors langage, débute par majuscule
I
Sert à fabriquer les objets.
I
Structure le code-source.
main
Création d’une nouvelle classe
Notions de
classes et objets
en Java™
Attribut
Type primitif
Classe
Attributs
Méthode
Constructeur
Objets
En Java™ : 1 classe = 1 fichier
Balle rebondissante
V1
Personnage.java
public class Personnage {
double x; // attribut
}
Personnage
x : double
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Création d’une classe
Notions de
classes et objets
en Java™
Attributs
Type primitif
Classe
Attributs
Méthode
Constructeur
Objets
Personnage.java
public class Personnage {
/* Attributs */
double x;
ImageIcon img;
}
Balle rebondissante
Personnage
x : double
img : ImageIcon
V1
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Création d’une classe
Notions de
classes et objets
en Java™
Méthode
Type primitif
Classe
Attributs
Méthode
Constructeur
Objets
Personnage.java
public class Personnage {
/* Attributs */
double x;
ImageIcon img;
}
void avancer(double dx) { // Méthode
x = x+dx;
}
Balle rebondissante
Personnage
V1
V2
x : double
img : ImageIcon
Changement scène
avancer(dx : double)
Plus loin
Héritage
Contrat
static
main
Création d’une classe
Notions de
classes et objets
en Java™
Constructeur(s)
Type primitif
Classe
Attributs
Personnage.java
Méthode
Constructeur
public class Personnage {
/* Attributs */
double x;
ImageIcon img;
Personnage() { // Constructeur 1
x = 10;
img = new ImageIcon("personnage.jpg");
}
Personnage(double abscisse) { // Constructeur 2
x = abscisse;
img = new ImageIcon("personnage.jpg");
}
}
void avancer(double dx) { // Méthode
x = x+dx;
}
Objets
Balle rebondissante
V1
Personnage
x : double
img : ImageIcon
avancer(dx : double)
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Objets
Notions de
classes et objets
en Java™
Type primitif
Objet = 1 réalisation (instance) de la classe
Classe
Attributs
Méthode
Personnage p1 = new Personnage(); // Constructeur 1
p1.avancer(3); // appel méthode
Personnage p2 = new Personnage(10.2); // Constructeur 2
p2.x = 18; // modif attribut
p2.avancer(2.1); // appel méthode
Constructeur
Objets
Balle rebondissante
V1
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Objets
Notions de
classes et objets
en Java™
Type primitif
Objet = 1 réalisation (instance) de la classe
Classe
Attributs
Méthode
Personnage p1 = new Personnage(); // Constructeur 1
p1.avancer(3); // appel méthode
Personnage p2 = new Personnage(10.2); // Constructeur 2
p2.x = 18; // modif attribut
p2.avancer(2.1); // appel méthode
Constructeur
Objets
Balle rebondissante
V1
V2
Changement scène
Personnage
x : double
img : ImageIcon
avancer(dx : double)
objets (à la fin)
p1 : Personnage
x = 13
Plus loin
Héritage
Contrat
static
main
p2 : Personnage
x = 20.1
Exemple : balle rebondissante
Notions de
classes et objets
en Java™
Version 1 seule balle
Type primitif
Classe
Attributs
Méthode
Constructeur
Objets
Main
void start(primaryStage :Stage)
static main()
SampleController
width,height : int
balle : Balle
vX, vY : double;
avancer()
Balle rebondissante
V1
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Exemple : balles rebondissantes
Notions de
classes et objets
en Java™
Version plusieurs balles
Type primitif
Classe
Attributs
Méthode
Constructeur
Objets
Balle rebondissante
Main
void start(primaryStage :Stage)
static main()
SampleController
width,height : int
balle : Balle[]
Balle
vX, vY : double;
avancer(…)
V1
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Exemple : changement de scène
Notions de
classes et objets
en Java™
Type primitif
Classe
Attributs
Méthode
Main
Constructeur
Objets
void start(primaryStage :Stage)
void chargerScene(index : int,titre : String)
static main()
Balle rebondissante
V1
V2
Changement scène
SampleController1
SampleController2
main : Main
void setMainInstance(m : Main)
main : Main
void setMainInstance(m : Main)
Plus loin
Héritage
Contrat
static
main
Héritage
Notions de
classes et objets
en Java™
Oiseau.java
Personnage
x : double
img : ImageIcon
public class Oiseau extends Personnage {
double y;
avancer(dx : double)
y : double
Oiseau(double abscisse,double ordonnee) {
super(abscisse);
y = ordonnee;
}
Oiseau
voler(dx : double,dy : double)
}
void voler(double dx, double dy) {
x += dx;
y += dy;
}
Utilisation :
Type primitif
Classe
Attributs
Méthode
Constructeur
Objets
Balle rebondissante
V1
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Oiseau o = new Oiseau(10,0);
o.x = 3; // modif attribut
o.y = 2;
o.voler(-2,3.1); // appel méthode
o.avancer(3.2);
Contrat/Interface
Notions de
classes et objets
en Java™
Dessinable.java
interface
Dessinable
dessiner(g : Graphics2D)
public interface Dessinable {
void dessiner(Graphics2D g);
}
Type primitif
Classe
Attributs
Méthode
Constructeur
Objets
y : double
Oiseau
voler(dx : double,dy : double)
dessiner(g: Graphics2D)
Oiseau.java
public class Oiseau implements Dessinable {
...
}
void dessiner(Graphics2D g) {
...
}
Utilisation :
Oiseau o = new Oiseau(10,0);
o.dessiner(g)
Balle rebondissante
V1
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Attribut/méthode de classe
Mot clé static
Personnage.java
public class Personnage {
// Attribut de classe
static int nbPersonnage = 0;
// Attributs d'instances
double x;
Personnage() { // Constructeur
x = 10;
nbPersonnage++;
}
}
void avancer(double dx) { // Méthode
x = x+dx;
}
Notions de
classes et objets
en Java™
Type primitif
Classe
Attributs
Méthode
Constructeur
Objets
Balle rebondissante
V1
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Utilisation :
Personnage p1 = new Personnage();
Personnage p2 = new Personnage();
System.out.println("Nombre␣total="+Personnage.nbPersonnage);
Fonction main
Notions de
classes et objets
en Java™
Type primitif
Fonction unique de démarrage d’une application
Classe
Attributs
Méthode
Constructeur
Application.java
public class Application {
public Application() { // constructeur
...
}
}
static void main(String[] args) { // Méthode principale du projet
Application a = new Application();
}
Objets
Balle rebondissante
V1
V2
Changement scène
Plus loin
Héritage
Contrat
static
main
Téléchargement