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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
import java.util.ArrayList;
public class Test{
/**
* Interface Item
*/
static interface Item {
public void affiche();
}
/**
* Abstract Carre
*/
static abstract class Carre implements Item{
public Carre(){
}
public abstract void affiche();
public void bingo(){
System.out.println("Bingoooooooo Carre");
}
}
/**
* Abstract Rond
*/
static abstract class Rond implements Item{
public Rond(){
}
public abstract void affiche();
public void bingo2(){
System.out.println("Bingoooooooo Rond");
}
}
/**
* CarreA
*/
static class CarreA extends Carre{
public CarreA(){
}
public void affiche(){
System.out.println("Je suis un Carre;");
}
}
/**
* RondA
*/
static class RondA extends Rond{
public RondA(){
}
public void affiche(){
System.out.println("Je suis un Rond;");
}
}
public static void main(String[] args){
ArrayList<Item> itemList = new ArrayList<Item>();
CarreA p = new CarreA();
RondA s = new RondA();
itemList.add(p);
itemList.add(s);
/*
* On veut pouvoir acceder a la methode bingo ou bingo2 a partir de l'ArrayList d'Item
* sans avoir a faire de tests "instanceof" suivi d'un cast :
* if(itemList.get(0) instanceof CarreA)
* ((CarreA)itemList.get(0)).bingo();
* if(itemList.get(0) instanceof RondA)
* ((RondA)itemList.get(0)).bingo2();
*/
}
} |
Partager