Documente Academic
Documente Profesional
Documente Cultură
10
Clase abstracte. Polimorfism
class AfisarePuncte
{
public static void main (String args[])
{
int N;//numarul de puncte
N=Integer.parseInt(JOptionPane.showInputDialog("N="));
Punct p[]=new Punct[N];//vectorul de obiecte Punct (clasa de
//baza)
int i;
for(i=0;i<N;i++){
String sRaspuns=JOptionPane.showInputDialog("Tip punct
(0,1,2) : ");
int raspuns=Integer.parseInt(sRaspuns);
int x=Integer.parseInt(JOptionPane.showInputDialog("x="));
int y=Integer.parseInt(JOptionPane.showInputDialog("y="));
if(raspuns==0){
//citeste un Punct:
p[i]=new Punct(x,y);}
else if(raspuns==1){
//citeste un PunctColor:
String culoare=JOptionPane.showInputDialog("culoare=");
p[i]=new PunctColor(x,y,culoare);}
else if(raspuns==2){
//citeste un Punct3D:
int z=Integer.parseInt(JOptionPane.showInputDialog("z="));
p[i]=new Punct3D(x,y,z);}
3
}//for
//Afisare vector:
for(i=0;i<N;i++)
p[i].afisare();
}
}
Tema