| 
 abstract 
public 
class Zahl { 
  abstract 
  public 
  Zahl plus(Zahl z2); 
} 
 | 
    
| 
 public 
class GanzeZahl extends Zahl { 
  int i; 
  public 
  GanzeZahl(int i) { 
    this.i = i; 
  } 
  // 1.Parameter ist ganze Zahl 
  // die Verzweigung ueber die Art des 2.Paramters 
  // wird ueber instanceof realisiert 
  public 
  Zahl plus(Zahl z2) { 
      if (z2 instanceof GanzeZahl) 
          return 
              new GanzeZahl(i + ((GanzeZahl)z2).i); 
      if (z2 instanceof RationaleZahl) { 
          r2 = (RationaleZahl)z2; 
          return 
              new RationaleZahl(i * r2.n + r2.z, r2.n); 
      } 
      if (z2 instanceof ReelleZahl) 
          return 
              new ReelleZahl((double)i + ((ReelleZahl)z2).r); 
  } 
} 
 | 
    
| 
         
 Für rationale und reeele Zahlen analoge Methoden für plus.
 
       | 
    |
| 
         
 Mehrwegverzweigungen über instanceof widerspricht dem OO-Ansatz.
 
       | 
    
| Letzte Änderung: 13.04.2012 | © Prof. Dr. Uwe Schmidt |