Le polymorphisme
CHAPITRE 13
284
.
Exemples langages
Voici, écrit en Java, C#, PHP et C++, l’exemple de gestion d’un tableau hétérogène du paragraphe 3, page
277.
Java
En Java, il n’y a absolument rien de particulier à faire pour disposer du polymorphisme.
class Point
{ public Point (int x, int y)
{ abs = x ; ord = y ;
}
public void affiche ()
{ identifie() ;
System.out.println (" Mes coordonnees sont : " + abs + " " + ord) ;
}
public void identifie ()
{ System.out.println ("Je suis un point ") ;
}
private int abs, ord ;
}
class Pointcol extends Point
{ public Pointcol (int x, int y, int c)
{ super (x, y) ;
couleur = c ;
}
public void identifie ()
{ System.out.println ("Je suis un point colore de couleur " + couleur) ;
}
private int couleur ;
}
public class TabHeter
{ public static void main (String args[])
{ Point [] tabPts = new Point [4] ;
tabPts [0] = new Point (0, 2) ;
tabPts [1] = new Pointcol (1, 5, (byte)3) ;
tabPts [2] = new Pointcol (2, 8, (byte)9) ;
tabPts [3] = new Point (1, 2) ;
for (int i=0 ; i< tabPts.length ; i++)
tabPts[i].affiche() ;
}
}
CHAPITRE 13
284
.
Exemples langages
Voici, écrit en Java, C#, PHP et C++, l’exemple de gestion d’un tableau hétérogène du paragraphe 3, page
277.
Java
En Java, il n’y a absolument rien de particulier à faire pour disposer du polymorphisme.
class Point
{ public Point (int x, int y)
{ abs = x ; ord = y ;
}
public void affiche ()
{ identifie() ;
System.out.println (" Mes coordonnees sont : " + abs + " " + ord) ;
}
public void identifie ()
{ System.out.println ("Je suis un point ") ;
}
private int abs, ord ;
}
class Pointcol extends Point
{ public Pointcol (int x, int y, int c)
{ super (x, y) ;
couleur = c ;
}
public void identifie ()
{ System.out.println ("Je suis un point colore de couleur " + couleur) ;
}
private int couleur ;
}
public class TabHeter
{ public static void main (String args[])
{ Point [] tabPts = new Point [4] ;
tabPts [0] = new Point (0, 2) ;
tabPts [1] = new Pointcol (1, 5, (byte)3) ;
tabPts [2] = new Pointcol (2, 8, (byte)9) ;
tabPts [3] = new Point (1, 2) ;
for (int i=0 ; i< tabPts.length ; i++)
tabPts[i].affiche() ;
}
}
