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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| public abstract Animal
{
// on se contente de ces attributs pour des raisons de brievete
private String nom;
private double consommationJournaliere;
public Animal(String nom, double consommationJournaliere)
{
this.nom = nom;
this.consommationJournaliere = consommationJournaliere;
}
public String getNom()
{
return this.nom;
}
// methode abstraite car chaque animal crier a sa maniere
public abstract void crier();
}
public class Pingouin extends Animal
{
// tant qu'a faire, on ajoute un attribut a notre sous-classe
boolean estParent;
public Pingouin(String nom, double consommationJournaliere, boolean estParent)
{
super(nom, consommationJournaliere);
this.estParent = estParent;
}
public void crier()
{
System.out.println("AAaanananandsis Gusfdnsd!");
}
}
public class Elephant extends Animal
{
private double tailleDeTrompette;
public Elephant(String nom, double consommationJournaliere, double tailleDeTrompette)
{
super(nom, consommationJournaliere);
this.tailleDeTrompette = tailleDeTrompette;
}
public void crier()
{
System.out.println("Suuuuuuuuuuuu!");
}
}
public class TestExemple
{
public static void main(String[] args)
{
Elephant bob = new Elephant("Bob", 140.7, 3.3);
Elephant bleu = new Elephant("Bleu", 180.5, 6.2);
Pingouin tux = new Pingouin("Tux", 43, false);
Pingouin pere = new Pingouin("Jimmy", 72.1, true);
Animal[] animaux = {bob, bleu, tux, pere};
for (Animal animal : animaux)
{
System.out.println(animal.getNom() + "crie...");
animal.crier();
}
}
} |
Partager