Vererbung und POLYMOrPHIE

Softwaretechnologie I / Übung II

JAVA II

slides.com/phdd/st1e2/live

Lernziele

  • einfache Methoden selbst zu programmieren
  • einfache Klassen und Objektnetze in UML-Klassendiagrammen darzustellen
  • einfache Vererbung am Beispiel zu verstehen
  • die Merkmalssuche in Vererbungshierarchien nachzuvollziehen
    (dynamic dispatch)
  • zu bestimmen, welche Methoden in einer Vererbungshierarchie überschrieben werden müssen
  • Vererbungshierarchien in UML zu visualisieren
  • eine Vererbungshierarchie in Java zu programmieren

Vererbung?

class Student extends Person {

    int semester;

    int getSemester() {
        return semester;
    }

}
class Person {

    String name;

    String getName() {
        return name;
    }

}

Aufgabe 1

Book search(String title) {














}

Buch Suchen

Book search(String title) {
    Book found = null;

    for (int i = 0; i < number; i++)
        if (title.equals(myBooks[i].toString()))
            found = myBooks[i];

    if (found == null)
        System.out.println("The book with the title "
                + title + " does not exist in the library!");
    else
        System.out.println("The book with the title "
                + title + " exists in the library!");

    return found;
}

Aufgabe 1

Leihstatus

public class Book {

    // Zustandsdefinition


    Book(String title) {
        // Initialzustand

    }

    // ausleihen






    // zurückbringen





    // Zustandsabfrage




}
public class Book {

    // Zustandsdefinition
    private String state;

    Book(String title) {
        // Initialzustand
        state = "available";
    }

    // ausleihen
    Book loan() {
        state = "lent";
        return this;
    }    


    // zurückbringen
    Book bringBack() {
        state = "available";
        return this;
    }

    // Zustandsabfrage
    String getState() {
        return state;
    }

}

Aufgabe 1

Leihe

Book loan(String title) {









    
}
Book loan(String title) {
    Book book = search(title);

    if (book == null)
        return null;

    if ("available".equals(book.getState())) 
        return book.loan();

    System.out.println("Book " + book + " not available");
    return null;
}

Aufgabe 2

Modell der Domäne

Aufgabe 2

implementierung - Einwohner

class Inhabitant {

    private int income;

    void setIncome(int income) {
        this.income = income;
    }

    int taxableIncome() {
        return income;
    }

    int tax() {
        int tax = taxableIncome() / 10;

        if (tax < 1) {
            tax = 1;
        }

        return tax;
    }

}
class Inhabitant {

    private int income;

    void setIncome(int income) {
        this.income = income;
    }

    int taxableIncome() {
        return income;
    }

    int tax() {
        int tax = taxableIncome() / 10;

        if (tax < 1) {
            tax = 1;
        }

        return tax;
    }

}

Aufgabe 2

implementierung - König

class King                      {

    @Override
    int tax() {
        return 0;
    }

}
class King extends Inhabitant {

    @Override
    int tax() {
        return 0;
    }

}

Aufgabe 2

implementierung - Adel

class Noble                        {

    @Override
    int tax() {
        int tax = super.tax();

        if (tax < 20) {
            tax = 20;
        }

        return tax;
    }

}
class Noble extends Inhabitant {

    @Override
    int tax() {
        int tax = super.tax();

        if (tax < 20) {
            tax = 20;
        }

        return tax;
    }

}

Aufgabe 2

implementierung - Bauer

class Peasant                        {

    @Override
    int tax() {
        int tax = super.tax();

        if (tax < 20) {
            tax = 20;
        }

        return tax;
    }

}
class Peasant extends Inhabitant {



}

Aufgabe 2

implementierung - Leibeigener

class Serf                        {

    @Override
    int tax() {
        int tax = super.tax();

        if (tax < 20) {
            tax = 20;
        }

        return tax;
    }

}
class Serf extends Peasant {

    @Override
    int taxableIncome() {
        int taxableIncome = 
            super.taxableIncome() - 12;

        if (taxableIncome < 0)
            taxableIncome = 0;

        return taxableIncome;
    }

}

Aufgabe 2

das Königreich

public class Kingdom {

    public static void main(String[] args) {
        taxBillFor(new King(),    20);
        taxBillFor(new Noble(),   20);
        taxBillFor(new Peasant(), 20);
        taxBillFor(new Serf(),    20);
    }

    static void taxBillFor(Inhabitant inhabitant, int income) {
        inhabitant.setIncome(income);

        System.out.println(inhabitant.getClass().getName() 
                + ": income " + income 
                + ", taxable income " + inhabitant.taxableIncome() 
                + ", tax " + inhabitant.tax());
    }

}

Kontakt

pehei.de

slides.com/phdd

twitter.com/_phdd

ST1E2: Java II - Vererbung und Polymorphie

By Peter Heisig

ST1E2: Java II - Vererbung und Polymorphie

  • 1,137