Bonjour !
Voici un programme :
Après compil puis exéc, j' obtien ceci :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public class TesterPoints2 { public static void main(String [] args) { Cercle A = new Cercle(1, 2, 3); Cercle B = new Cercle(21, 22, 23); Cercle C = new Cercle(31, 32, 33); } } class Cercle { private double centreX; private double centreY; private double rayon; private static int nombreObjets; public Cercle(double x, double y, double r) { centreX = x; centreY = y; rayon = r; System.out.println("nombre total d' objets = " + nombreObjets++); } public void afficherCoord() { System.out.println("coordonnées du cercle : " + centreX + ", " + centreY + " et " + rayon); } }
nombre total d' objets = 0
nombre total d' objets = 1
nombre total d' objets = 2
Ce qui me paraît anormal. En écrivant ceci : Cercle A = new Cercle(1, 2, 3),
Le champs static nombreObjets est initialisé à zéro.
Donc, l' instruction : System.out.println("nombre total d' objets " + nombreObjets++); permet d' afficher : nombre total d' objets = 1.
Pourquoi c' est 0 au lieu de 1 ?
Partager