Advertisement
Guest User

Polimorfismo

a guest
Aug 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. public abstract class Human
  2. {
  3.    ...
  4.    public abstract void goPee();
  5. }
  6.  
  7. /**
  8.  *"Human" è una classe astratta per la quale il metodo "goPee" non può trovare un'applicazione concreta.
  9.  *Non tiene infatti conto delle differenze fisiologiche e comportamentali tra uomo e donna.
  10. /*
  11.  
  12. public class Male extends Human
  13. {
  14.     ...
  15.     @Override
  16.     public void goPee()
  17.     {
  18.         System.out.println("Stand Up");
  19.     }
  20. }
  21.  
  22. public class Female extends Human
  23. {
  24.     ...
  25.     @Override
  26.     public void goPee()
  27.     {
  28.         System.out.println("Sit Down");
  29.     }
  30. }
  31.  
  32. public static void main(String[] args){
  33.     ArrayList<Human> group = new ArrayList<Human>();
  34.     group.add(new Male());
  35.     group.add(new Female());
  36.     // ... add more...
  37.  
  38.     // tell the class to take a pee break
  39.     for (Human person : group) person.goPee();
  40. }
  41. /**L'esecuzione di person.goPee() restituirebbe un risultato del tipo:
  42.  *Stand Up
  43.  *Sit Down
  44.  *...
  45. /*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement