Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Polymorphisme. 2 // Polymorphisme: illustre le polymorphisme et Vector import java.util.*; public class TestPolymorphisme { public static void main.

Similar presentations


Presentation on theme: "1 Polymorphisme. 2 // Polymorphisme: illustre le polymorphisme et Vector import java.util.*; public class TestPolymorphisme { public static void main."— Presentation transcript:

1 1 Polymorphisme

2 2 // Polymorphisme: illustre le polymorphisme et Vector import java.util.*; public class TestPolymorphisme { public static void main (String[] args) { Vector animaux = new Vector(); Animal unAnimal; Chien unChien = new Chien("Snoopy"); animaux.add(unChien); Chat unChat = new Chat("Felix"); animaux.add(unChat); animaux.add(new Chat("Garfield")); animaux.add(new Chien("Pluto")); for (int i= 0; i < animaux.size(); i++){ unAnimal = (Animal)animaux.get(i); unAnimal.afficher(); unAnimal.parle(); }

3 3 // Animal avec nom obtenir nom, afficher abstract class Animal{ private String nom; Animal(String chaine){ nom= chaine; } String getNom(){return nom;} abstract void parle(); void afficher(){ System.out.println(nom); }

4 4 class Chien extends Animal{ Chien(String chaine){ super(chaine); System.out.println("\nCreer le chien " + chaine); } void afficher(){ System.out.print("\nJe suis le chien "); super.afficher(); } void parle(){ System.out.println(" Ouarf! Ouarf!"); }

5 5 class Chat extends Animal{ Chat(String chaine){ super(chaine);// constructeur de Animal System.out.println("\nCreer le chat " + chaine); } void afficher(){ System.out.print("\nJe suis le chat "); super.afficher( ); } void parle(){ System.out.println(" Miaou! Miaou!"); }

6 6 Exécution du programme: Creer le chien Snoopy Creer le chat Felix Creer le chat Garfield Creer le chien Pluto Je suis le chien Snoopy Ouarf! Ouarf! Je suis le chat Felix Miaou! Miaou! Je suis le chat Garfield Miaou! Miaou! Je suis le chien Pluto Ouarf! Ouarf!


Download ppt "1 Polymorphisme. 2 // Polymorphisme: illustre le polymorphisme et Vector import java.util.*; public class TestPolymorphisme { public static void main."

Similar presentations


Ads by Google