On ne peut pas stocker d'opérateur du langage dans une variable, et donc une variable ne peut pas servir d'opérateur.
Voici la manière la plus directe de faire cela :
	
	if(tab[i][j] == '<' ? a < b : a > b)
 qui peut être écrite plus clairement ainsi :
	
	| 12
 3
 4
 
 | if(
  (tab[i][j] == '<' && a < b) ||
  (tab[i][j] != '<' && a > b)
) | 
 
Voici la manière objet de faire cela :
fichier IntComparator :
	
	| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | public class IntComparator {
 
  public static final IntComparator LOWER_THAN = new IntComparator(true);
  public static final IntComparator GREATER_THAN = new IntComparator(false);
 
  boolean lower;
 
  private IntComparator(boolean lower) {
    this.lower = lower;
  }
 
  public boolean comparison(int a, int b) {
    if(lower) {
       return a < b;
    } else {
       return a > b;
    }
  }
} | 
 Puis pour s'en servir,
	
	| 12
 3
 4
 
 | IntComparator[][] tab;
// initialiser le tableau avec des IntComparator.LOWER_THAN et des IntComparator.GREATER_THAN...
// ...
if(tab[i][j].comparison(a, b)) | 
 ... Mais pour ce coup-là, l'approche objet c'est peut-être se faire ch*er pour pas grand-chose.
						
					
Partager