html5-img
1 / 10

„ Höhere objektorientierte Konzepte“ Zusammenfassung des Kapitels 8

„ Höhere objektorientierte Konzepte“ Zusammenfassung des Kapitels 8 Küchlin, Weber, Einführung in die Informatik, 3.Auflage. Marc-Oliver Pahl, 25.1.2004. Id. private static int counter private int myId. public Id() public int getId(). Label. private String name. public String getName().

yon
Télécharger la présentation

„ Höhere objektorientierte Konzepte“ Zusammenfassung des Kapitels 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. „ Höhere objektorientierte Konzepte“ Zusammenfassung des Kapitels 8 Küchlin, Weber, Einführung in die Informatik, 3.Auflage Marc-Oliver Pahl, 25.1.2004 W. Küchlin, A. Weber: Einführung in die Informatik – objektorientiert mit Java -1- Springer-Verlag, ISBN 3-540-20958-1

  2. Id private static int counter private int myId public Id() public int getId() Label private String name public String getName() Vererbung publicclass Id { privatestaticint counter = 1; privateint myId; public Id(){ myId = counter++; } publicint getId(){ return myId; } } publicclass Label extends Id { private String name; public Label() { // super(); // -> Id(); implizit! name = "unknown"; } public Label(String val) { name = val; } public String getName(){ return name; } } W. Küchlin, A. Weber: Einführung in die Informatik – objektorientiert mit Java -2- Springer-Verlag, ISBN 3-540-20958-1

  3. Id private static int counter private int myId public Id() public int getId() Label private String name public String getName() Vererbung Die Methoden der Klasse Id, von der geerbt wird, sind in Label auch automatisch vorhan-den! publicclass Main { publicstaticvoid main(String[] args) { Label myLabel = new Label("as2004"); System.out.println( “Das Gerät“ + myLabel.getName() + “hat die Id“+ myLabel.getId()); } } W. Küchlin, A. Weber: Einführung in die Informatik – objektorientiert mit Java -3- Springer-Verlag, ISBN 3-540-20958-1

  4. Id private static int counter private int myId public Id() public int getId() Label private String name public String getName() Vererbung/ Sichtbarkeit publicclass Id { ... privateint myId; ... } geht NICHT, da das Feld private ist! publicclass Label extends Id { ... public Label(String val) { name = val; super.myId = 0; //hier:  myId = 0; } ... } W. Küchlin, A. Weber: Einführung in die Informatik – objektorientiert mit Java -4- Springer-Verlag, ISBN 3-540-20958-1

  5. Id private static int counter protected int myId public Id() public int getId() Label private String name public String getName() Vererbung/ Sichtbarkeit publicclass Id { ... privateint myId; ... } geht, da das Feld protected ist! publicclass Label extends Id { ... public Label(String val) { super(); name = val; super.myId = 0; //hier:  myId = 0; } ... } W. Küchlin, A. Weber: Einführung in die Informatik – objektorientiert mit Java -5- Springer-Verlag, ISBN 3-540-20958-1

  6. Id private static int counter protected int myId public Id() public int getId() Label private String name private int myId public String getName() Vererbung/ Sichtbarkeit publicclass Id { ... privateint myId; ... } publicclass Label extends Id { ... public Label(String val) { super(); name = val; super.myId = 0; // NICHT = myId = 0; myId = 23; } ... } W. Küchlin, A. Weber: Einführung in die Informatik – objektorientiert mit Java -6- Springer-Verlag, ISBN 3-540-20958-1

  7. virtuelle Funktionen/ Überschreiben publicclass Parent { publicvoid method(){ System.out.println("Methode des Parent."); } } publicclass Child extends Parent { publicvoid method(){ System.out.println("Methode des Child."); } } publicclass Main { publicstaticvoid main(String[] args) { Parent eins = new Parent(); Parent zwei = new Child(); eins.method(); zwei.method(); } } W. Küchlin, A. Weber: Einführung in die Informatik – objektorientiert mit Java -7- Springer-Verlag, ISBN 3-540-20958-1

  8. virtuelle Funktionen/ Überschreiben/ final publicclass Parent { publicfinal void method(){ System.out.println("Methode des Parent."); } } publicclass Child extends Parent { publicvoid method(){ System.out.println("Methode des Child."); } } publicclass Main { publicstaticvoid main(String[] args) { Parent eins = new Parent(); Parent zwei = new Child(); eins.method(); zwei.method(); } } W. Küchlin, A. Weber: Einführung in die Informatik – objektorientiert mit Java -8- Springer-Verlag, ISBN 3-540-20958-1

  9. ADT/ Interfaces/ Selektoren Kundenklasse publicclass Main { publicstaticvoid main(String[] args){ ZahlenSpeicher z1 = new ZahlenSpeicher1(); ZahlenSpeicher z2 = new ZahlenSpeicher2(); z1.setValue(7); z2.setValue(8); System.out.println("z1+z2="+(z1.getValue()+z2.getValue())); }} Schnittstelle/ Interface publicinterface ZahlenSpeicher { publicint getValue(); publicvoid setValue(int val);} interne Repräsentation/ Implementierung publicclassZahlenSpeicher1implements ZahlenSpeicher { privateint theValue = 0; publicint getValue(){ return theValue; } publicvoid setValue(int val){ theValue = val; } } publicclassZahlenSpeicher2implements ZahlenSpeicher { private String theValue; publicint getValue(){ return Integer.parseInt(theValue); } publicvoid setValue(int val){ theValue = Integer.toString(val); } } W. Küchlin, A. Weber: Einführung in die Informatik – objektorientiert mit Java -9- Springer-Verlag, ISBN 3-540-20958-1

  10. generisches Programmieren • Stichwort: Muster • in C++ • in Java template<class T> class Node{ private: T data; Node* next; public: T getData(); ... } publicstaticvoid inc(ZahlenSpeicher z){ z.setValue(z.getValue()+1); } Funktioniert „generisch“ für alle beliebigen Implementierungen des Interface ZahlenSpeicher W. Küchlin, A. Weber: Einführung in die Informatik – objektorientiert mit Java -10- Springer-Verlag, ISBN 3-540-20958-1

More Related