Beruflich Dokumente
Kultur Dokumente
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Version: 17. Jan. 2006 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 2
Java-Programm:
Menge von Komponenten
Interface: C1 C2
C5
C1
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 3 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 4
Komponenten in Java Java-Syntax: Komponentenarten
zwei Arten
(zumindest syntaktisch)
Quelltextdatei ::=
Klasse Interface [Paketfestlegung]
class interface {Import}
{Typdeklaration}
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 5 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 6
import java.util.*
• Inhaltlich-logische Funktion:
class C1 {
... - Trennung Interface - Implementation
}
interface I1 {
... • Technische Funktion:
} - eingeschränkte Mehrfachvererbung
Definition einer Komponente C1/I1 kann voraussetzen:
int totalMinutes =
(60*hour + minute + m) % (24 * 60);
if (totalMinutes < 0)
totalMinutes = totalMinutes + 24 * 60;
Schnittstelle zur hour = totalMinutes / 60;
Interface "Außenwelt" ...
}
... Java-Klasse ist Mischung aus
Implementation Hiervon kann die Interface und Implementation:
}
"Außenwelt" Abstraktionsprinzip nicht umgesetzt
("versteckt") abstrahieren
- Interface - Implementation
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 9 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 10
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 11 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 12
Java–Interface:
eine (fast) "saubere" Abstraktion
Sc
he
du
l eI
interface TimeI { nt.
jav
Interface in Java:
public void addMinutes (int m); a
public void substractMinutes (int m);
public void printTime ();
- Definition public void printTimeInMinutes ();
}
- Implementation
• Nur die Köpfe der Methoden
- Anwendung • Keine Daten + keine Algorithmen
• Was fehlt?
- Vergleich mit abstrakten Klassen Konstruktoren (im Interface nicht erlaubt):
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 13 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 14
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 17 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 18
Interface:
eingeschränkte Mehrfachvererbung
für Klassen verboten – für Interface erlaubt:
Komponente 1 Komponente 2
interface I1 ...
interface I2 ... ok
class K3 implements I1, I2 ...
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 19 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 20
Beispiel: Beispiel:
Hierarchie mit Mehrfachvererbung (1) Hierarchie mit Mehrfachvererbung (2)
class class
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 21 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 22
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 23 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 24
Interface: Interface:
Welche Komponentenart modelliert? modellierbare Komponentenart
Vgl. III.4 Komponentenarten
interface Fahrzeug {
public int kapazitaet();
public double verbrauch(); sinnlose viele
static final int anzahl = 100000; Konstruktionen semantisch
} sehr unterschiedliche
Komponenten a r t e n
Festlegung (gilt auch implizit für Interface):
Methoden: public, abstract, non-static
Variablen: public, static, final imperativ objektorientiert
Funktions- ADT
Konstanten-
Æ ADT mit Konstanten sammlung
sammlung Daten-
sammlung ADT mit
andere Daten- Konstanten
ADT mit
abstraktion Klassenelementen
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 25 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 26
Interface: Java-Interface:
nicht modellierbare Komponentenarten Methoden mit Interface-Parametern
interface TimeI {
public void addMinutes (int m);
public void printTime ();
...
Îsinnlose
Nicht für jede viele pulic boolean before (TimeI t);
Komponentenart
Konstruktionen semantisch } Dieselbe Signatur:
kann ein Interface sehr unterschiedliche Interface-Typ auch in Klasse
angeboten werden Komponenten a r t e n
class Time implements TimeI {
Funktions- ADT
Typ-Transformation: Parameter hat Interface-Typ
Konstanten- allgemeiner Typ (Interface) Æ spezieller Typ
sammlung
h sammlung Daten-
at ADT mit
.M andere sammlung Cast-Operation kann Laufzeitfehler erzeugen:
ng Daten- Konstanten
.la ADT mit
va abstraktion aktueller Parameter einer anderen
ja Klassenelementen
ss ClassCastException
cla rd implementierenden Klasse übergeben
bo a eC
K ey Ti m
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 27 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 28
Interface: Anwendungsfälle
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 29 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 30
extends
Klassen • Algorithmen mit 'Funktionsparametern':
Stack - Druck.java: Druck beliebiger Funktionen
public class Vector Mehrfachvererbung
durch Vielzahl von
extends AbstractList
implements List, Cloneable, Serializable Interfaces im Java-API
• Java-API: List – Set – Collection ...
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 31 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 32
E i n Interface eines ADT – Anwendung:
zwei (mehrere) Implementationen: nur Interface bekannt + Konstruktor
Um Um ke h
rungNU
interface Stack { ke interface Stack { .java
hr
public boolean isempty(); un public boolean isempty();
gN
public void push(char x); U. public void push(char x); Besonderheit von UmkehrungNU.java:
public char top(); j av Erst dynamisch wird die
a public char top();
Vorteil: pulic void pop(); pulic void pop(); implementierende Klasse ausgewählt
} } Æ dynamische Bindung von
Gute Methoden zur Laufzeit
Modifizierbarkeit, class StackN implements Stack { Anwendung:
falls die private char[] stackElements;
private int top;
Stack s; Auswechseln der
Anwendung nur
...
s = new StackN(n); Implementation:
mit den beschränkte Größe oder
} Nur der Konstruktor
Informationen aus
s = new StackU(); ist auszuwechseln
dem Interface Algorithmus korrekt
arbeitet class StackU implements Stack { für beliebige while (!s.isempty()) {
private class Zelle {...} Implementationen: System.out.print(s.top());
private Zelle top; - Begrenzte Stacks Methoden aus
unbeschränkt s.pop();
... - Unbegrenzte Stacks dem Interface
}
}
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 33 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 34
Funktions- ADT
Konstanten-
sammlung
sammlung Daten- Anwendung: Keyboard kb = new Keyboard();
sammlung ADT mit technisch andere
andere Daten- Konstanten
ADT mit Realisierung Æ jn = kb.readChar();
abstraktion Klassenelementen Objekt erzeugt n = kb.readInt();
rd
bo a vgl. Keyboard.readChar()
K ey
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 37 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 38
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 39 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 40
Algorithmen mit 'Funktionsparametern'
Interface: Anwendungsfälle
Problem:
• Algorithmen: arbeiten mit (beliebigen)
• E i n Interface eines ADT mathematischen Funktionen
• Funktion ist Parameter des Algorithmus
- z w e i (mehrere) Implementationen:
- UmkehrungNU.java: beschränkt und unbeschränkt - Pascal: Funktionsparamter (spezielle Parameterart)
function druckeKurve (x0: real; x1: real;
• Interface für Datenabstraktionen: delta: real;
function F(x: real): real ) ...
- KeyboardIApp.java
Aufruf:
druckeKurve(0.5, 1.5, 0.1, sin);
• Import gemeinsamer Konstanten von Klassen druckeKurve(0.5, 1.5, 0.1, cos);
Beispiel: Beispiel:
Algorithmen mit 'Funktionsparametern' (1) Algorithmen mit 'Funktionsparametern' (2)
Dru
ck.j Dr
ava interface Function { uc
k.j
interface Function { double apply (double x); av
double apply (double x); allgemeinstes Schema } a
} einer einstelligen
reellwertigen Funktion Nutzer: arbeitet nur mit allgemeinem Schema (interface)
class SinFunction implements Function {
public double apply (double x) { static void druckeKurve (double x0, double x1,
return Math.sin(x); double delta, Function f) {
} System.out.print( f.apply(x0) ); ...
} }
2 Spezialisierungen: Sinus, Cosinus Anwendung: konkrete Funktionen 'sin', 'cos' Technisch anders
als in Pascal, C, ...:
public static void main (...) {
class CosFunction implements Function { Hier wird Objekt
SinFunction sinus = new SinFunction();
public double apply (double x) { übergeben, das
CosFunction cosinus = new CosFunction();
return Math.cos(x); passende Methode
druckeKurve(0, Math.PI/2, 0.1, sinus);
} umfasst
druckeKurve(0, Math.PI/2, 0.1, cosinus);
} (Wrapper)
}
K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 43 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 44
Druck aller Funktionswerte von x0 bis x1
(Verbesserung von Druck.java)