next up previous contents
suivant: Méthodes monter: Création d'objets précédent: Création d'objets   Table des matières

Constructeur




Exemple: On peut améliorer l'exemple, en définissant des constructeurs :

Notons aussi la méthode toString qui redéfinit la méthode de Object.



transparent
constructeurs


class Astre {
    public long idNum;
    public String nom= "<pasdenom>";
    public Astre orbite = null;
    private static long nextID = 0;
    Astre(){
        idNum = nextID++;
    }
    Astre(String nomAstre, Astre orbiteAstre) {
        this();
        nom = nomAstre;
        orbite = orbiteAstre;
    }


transparent
Suite ...


    public String toString(){
        String d = idNum + "(" + nom + ")";
        if (orbite != null)
            d += " autour de " + 
                  orbite.toString();
        return d;
    }
}





Hugues FAUCONNIER 2003-01-09