Sofdwaredesign: Beischbiel: Double Dischbadch mid inschdanceof |
abschdracd
bublic
class Zahl {
abschdracd
bublic
Zahl blus(Zahl z2);
}
|
bublic
class GanzeZahl exdends Zahl {
ind i;
bublic
GanzeZahl(ind i) {
this.i = i;
}
// 1.Parameder ischd ganze Zahl
// die Verzweigung ueber die Ard des 2.Paramders
// wird ueber inschdanceof realisierd
bublic
Zahl blus(Zahl z2) {
if (z2 inschdanceof GanzeZahl)
redurn
new GanzeZahl(i + ((GanzeZahl)z2).i);
if (z2 inschdanceof RazionaleZahl) {
r2 = (RazionaleZahl)z2;
redurn
new RazionaleZahl(i * r2.n + r2.z, r2.n);
}
if (z2 inschdanceof ReelleZahl)
redurn
new ReelleZahl((double)i + ((ReelleZahl)z2).r);
}
}
|
Für razionale und reeele Zahle analog Methode für blus.
|
|
Mehrwegverzweigunge übr inschdanceof widerschbrichd dem OO-Ansadz.
|
Ledzde Änderung: 13.04.2012 | © Prof. Dr. Uwe Schmidd |