• [^] # Re: Typage

    Posté par . En réponse au message débutant java : opérations de base sur les listes. Évalué à 0.

    Salut,

    Allez, comme c'était pas long...

    Zou. Une version sans null dans la ClasseA, sans le get() inutile, un peu de type checking de plus...

    public class ClassA {
     private class ClassB<I extends Integer, S extends String> extends ArrayList<Object> {
     public void myAdd(I i,S s) throws Exception {
     if(i==null && s==null) 
     throw new Exception("Je veux un paramètre !");
     if(i!=null && s!=null) 
     throw new Exception("Mais juste un seul !");
     Object o =null;
     if(i!=null)
     o=i;
     else
     o=s;
     super.add(o);
     }
     public void addInt(I i) throws Exception {
     myAdd(i, null);
     }
     public void addString(S s) throws Exception {
     myAdd(null, s);
     }
     }
     public static void main(String[] args) throws Exception {
     ClassA a = new ClassA();
     a.doThings();
     }
     public void doThings() throws Exception {
     ClassB<Integer, String> maListeDeBidules = new ClassB<>(); 
     maListeDeBidules.addInt(1); // ok
     maListeDeBidules.addString("deux"); // ok
     System.out.println("mon bidule 1: "+maListeDeBidules.get(0));
     System.out.println("mon bidule 2: "+maListeDeBidules.get(1));
     maListeDeBidules.myAdd(3, "quatre"); // pas ok
     } 
    }

    Matricule 23415