TD6

Collections & Générique

Générique

public class Jedi<T> {

    private T corps;

    private SabreLaser sabreLaser;

    public Jedi(T corps){
        this.corps = corps;
    }

    public SabreLaser getSabreLaser(){
       return sabreLaser;
    }

}



Chat chat = new Chat();
Chien chien = new Chien();

Jedi<Chat> miaouWalker = new Jedi<>(chat);

Jedi<Chien> woufWalker = new Jedi<Chien>(chien);

Collection

public class PersonneDansLeBus {

   private int numeroSiege;

}


public class Bus {

    private Collection<PersonneDansLeBus> collection;


    public Bus {
        collection = new ArrayList<>();
    }

    public void monteDansLeBus(PersonneDansLeBus p){
        collection.add(p);
    }
}

Map

public class PersonneDansLeBus {

   protected int numeroSiege;

}


public class Bus {

    private Map<Integer, PersonneDansLeBus> map;


    public Bus {
        map = new HashMap<>();
    }

    public void monteDansLeBus(PersonneDansLeBus p){
        map.put(p.numeroSiege, p);
    }

    public PersonneDansLeBus getPersonne(int numeroSiege){
        return map.get(numeroSiege);
    }
}

Comparator

public class PersonneDansLeBus {

   protected int numeroSiege;

}


public class PersonneDansLeBusComparator implements Comparator<PersonneDansLeBus> {

   public int compare(PersonneDansLeBus p1, PersonneDansLeBus p2){
        return p1 - p2;
   }
}

Interface Homme Machine

Made with Slides.com