Skip to content
Snippets Groups Projects
Commit c0d70ed1 authored by Daniel Meißner's avatar Daniel Meißner
Browse files

Added lesson 1 exercise 1: "Abstrakte Klasse".

parent 669c9464
No related branches found
No related tags found
No related merge requests found
File added
Aufgabenblatt_1-Aufgabe_1-Abstrakte_Klasse/doc/Klassendiagramm_Hunde.png

9.61 KiB

public class Aufgabe1AbstracteKlassen {
public static void main(String[] args) {
Hund[] hundeRudel = HundeRudel.erstelleRudel(3); // Range 0-5
for( int i = 0; i<hundeRudel.length; i++ ) {
hundeRudel[i].drucke();
}
}
}
\ No newline at end of file
public class Dackel extends Hund {
public Dackel(String name) {
super(name);
}
public String bellen() {
return "wau wau";
}
public String fressen() {
return "schmatz schmatz";
}
}
public abstract class Hund {
final private String name;
public Hund (String name) {
this.name = name;
}
public void drucke() {
System.out.println("Name: " + this.name);
System.out.println("Fressgeraeusch: " + this.fressen());
System.out.println("Bellgeraeusch: " + this.bellen());
System.out.println();
}
public abstract String bellen();
public abstract String fressen();
}
public class HundeRudel {
private static final String[] moeglicheHundeNamen = { "Peter", "Tobias", "Jan", "Thomas", "Jens" };
public static Hund[] erstelleRudel(int anzahl) {
Hund[] rudel = new Hund[anzahl];
for( int i=0; i<anzahl; i++ ) {
if ( i%2 == 1 ) {
rudel[i] = new Dackel(moeglicheHundeNamen[i]);
} else {
rudel[i] = new Schaeferhund(moeglicheHundeNamen[i]);
}
}
return rudel;
}
}
\ No newline at end of file
public class Schaeferhund extends Hund {
public Schaeferhund(String name) {
super(name);
}
public String bellen() {
return "wuff wuff";
}
public String fressen() {
return "schlurp schlurp";
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment