1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100057.html;106,k100060.html;107,k100062.html;108,k100064.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100057.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100060.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100062.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100064.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">Kapitel 9 - OOP III: Interfaces
</table>
<hr>
<!-- Section -->
<a name="weitereanwinterfaces"></a>
<h2>9.4 Weitere Anwendungen von Interfaces </h2>
<hr>
<ul>
<li><a href="k100061.html#weitereanwinterfaces">9.4 Weitere Anwendungen von Interfaces</a>
<ul>
<li><a href="k100061.html#konstininterfaces">9.4.1 Konstanten in Interfaces</a>
<ul>
<li><a href="k100061.html#sectlevel4id009004001001">static import</a>
</ul>
<li><a href="k100061.html#sectlevel3id009004002">9.4.2 Implementierung von Flags</a>
<li><a href="k100061.html#fzeigerinterfaces">9.4.3 Nachbildung von Funktionszeigern </a>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="konstininterfaces"></a>
<h3>9.4.1 <a name="ixa100557">Konstanten</a> in Interfaces </h3>
<p>
Neben abstrakten Methoden können Interfaces auch Konstanten,
also Membervariablen mit den Attributen <a href="index_s.html#ixb100422"><font color=#000080><tt>static</tt></font></a>
und <a href="index_f.html#ixb100401"><font color=#000080><tt>final</tt></font></a>,
enthalten. Wenn eine Klasse ein Interface implementiert, erbt es auch
alle seine Konstanten. Es ist auch erlaubt, dass ein Interface ausschließlich
Konstanten enthält.
<p>
Dieses Feature kann zum Beispiel nützlich sein, wenn ein Programm
sehr viele Konstanten definiert. Anstatt sie in ihren eigenen Klassen
zu belassen und bei jedem Gebrauch durch den qualifizierten Ausdruck
<font color="#000077"><tt>Klasse.Name</tt></font> aufzurufen, könnte
ein Interface definiert werden, das alle Konstantendefinitionen vereinigt.
Wenn nun jede Klasse, in der diese Konstanten benötigt werden,
dieses Interface implementiert, stehen alle darin definierten Konstanten
direkt zur Verfügung und können ohne qualifizierten Klassennamen
aufgerufen werden.
<p>
Das folgende Listing zeigt die Anwendung dieses Prinzips am Beispiel
eines Interface, das eine Reihe von Debug-Konstanten zur Verfügung
stellt. Sie werden beispielsweise zur bedingten Übersetzung oder
zur Steuerung von Debug-Ausgaben verwendet:
<a name="listingid009011"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing0911.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">interface</font> Debug
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#0000AA">final</font> <font color="#006699">boolean</font> FUNCTIONALITY1 = <font color="#006699">false</font>;
<font color="#555555">006 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#0000AA">final</font> <font color="#006699">boolean</font> FUNCTIONALITY2 = <font color="#006699">true</font>;
<font color="#555555">007 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#0000AA">final</font> <font color="#006699">boolean</font> FUNCTIONALITY3 = <font color="#006699">false</font>;
<font color="#555555">008 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#0000AA">final</font> <font color="#006699">boolean</font> FUNCTIONALITY4 = <font color="#006699">false</font>;
<font color="#555555">009 </font>}
<font color="#555555">010 </font>
<font color="#555555">011 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing0911
<font color="#555555">012 </font><font color="#0000AA">implements</font> Debug
<font color="#555555">013 </font>{
<font color="#555555">014 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">015 </font> {
<font color="#555555">016 </font> <font color="#00AA00">//...</font>
<font color="#555555">017 </font> <font color="#0000AA">if</font> (FUNCTIONALITY1) {
<font color="#555555">018 </font> System.out.println(<font color="#0000FF">"..."</font>);
<font color="#555555">019 </font> }
<font color="#555555">020 </font> <font color="#00AA00">//...</font>
<font color="#555555">021 </font> <font color="#0000AA">if</font> (FUNCTIONALITY2) {
<font color="#555555">022 </font> System.out.println(<font color="#0000FF">"..."</font>);
<font color="#555555">023 </font> }
<font color="#555555">024 </font> <font color="#00AA00">//...</font>
<font color="#555555">025 </font> }
<font color="#555555">026 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing0911.java"><font color="#000055" size=-1>Listing0911.java</font></a></td>
</tr>
</table>
<i>
Listing 9.11: Konstanten in Interfaces</i></p>
<!-- Section -->
<a name="sectlevel4id009004001001"></a>
<h4><a name="ixa100558">static import</a></h4>
<p>
Durch die <a href="index_i.html#ixb100444"><font color=#000080><tt>implements</tt></font></a>-Anweisung
können die Konstanten direkt, d.h. ohne vorangestellten Interface-Namen
verwendet werden. Diese praktische und von vielen Entwicklern verwendete
Möglichkeit ist allerdings nicht unbedingt im Sinne des Erfinders.
Die Implementierung eines Interface soll ja schließlich die
Zugehörigkeit einer Klasse zu einem bestimmten Typ dokumentieren
- nicht die Schreibfaulheit des Entwicklers.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Seit der J2SE 5.0 gibt es deshalb eine Möglichkeit, statische
Teile aus Klassen und Interfaces unqualifiziert zu benutzen, ohne
<a href="index_i.html#ixb100444"><font color=#000080><tt>implements</tt></font></a>
zu verwenden. Bei diesem, als <i>static import</i> bezeichneten Verfahren
werden die benötigten statischen Bestandteile in einer speziellen
<a href="index_i.html#ixb100169"><font color=#000080><tt>import</tt></font></a>-Anweisung
aufgelistet, bei der nach dem Schlüsselwort <a name="ixa100559"><a href="index_i.html#ixb100450"><font color=#000080><tt>import static</tt></font></a></a>
der qualifizierte Name des statischen Members folgt. Dieser kann dann
im weiteren Verlauf der Klasse unqualifiziert verwendet werden. Sollen
alle statischen Member auf einmal verfügbar gemacht werden, ist
<font color="#000077"><tt>Klasse.*</tt></font> zu schreiben. Dieses
Verfahren funktioniert sowohl mit Interfaces als auch mit Klassen
und kann sowohl Methoden wie auch Membervariablen importieren.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#FF9900">
<tr>
<td><font color="#FFFFFF"> JDK1.1-6.0 </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Das folgende Beispiel zeigt die Anwendung dieses neuen Features am
Beispiel der Klasse <a name="ixa100560"><a href="index_j.html#ixb100451"><font color=#000080><tt>java.lang.Math</tt></font></a></a>.
Die Methoden <a name="ixa100561"><a href="index_s.html#ixb100437"><font color=#000080><tt>sqrt</tt></font></a></a>
und <a name="ixa100562"><a href="index_s.html#ixb100452"><font color=#000080><tt>sin</tt></font></a></a>
sowie die Konstante <a name="ixa100563"><a href="index_p.html#ixb100453"><font color=#000080><tt>PI</tt></font></a></a>
können nach dem statischen Import der Klasse unqualifiziert verwendet
werden:
<a name="listingid009012"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing0912.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> <font color="#0000AA">static</font> java.lang.Math.*;
<font color="#555555">004 </font>
<font color="#555555">005 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing0912
<font color="#555555">006 </font>{
<font color="#555555">007 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">008 </font> {
<font color="#555555">009 </font> System.out.println(sqrt(2));
<font color="#555555">010 </font> System.out.println(sin(PI));
<font color="#555555">011 </font> }
<font color="#555555">012 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing0912.java"><font color="#000055" size=-1>Listing0912.java</font></a></td>
</tr>
</table>
<i>
Listing 9.12: static import</i></p>
<!-- Section -->
<a name="sectlevel3id009004002"></a>
<h3>9.4.2 Implementierung von Flags </h3>
<p>
Einige Interfaces definieren weder Methoden noch Konstanten. Sie stellen
statt dessen eine Art Flag, also einen logischen Schalter dar, der
zur Compile- und Laufzeit abgefragt werden kann. Beispiele aus der
Klassenbibliothek sind die Interfaces <a href="index_s.html#ixb100454"><font color=#000080><tt>java.io.Serializable</tt></font></a>
oder <a href="index_c.html#ixb100455"><font color=#000080><tt>java.lang.Cloneable</tt></font></a>.
Während <a href="index_s.html#ixb100454"><font color=#000080><tt>Serializable</tt></font></a>
in <a href="k100260.html#kapitelserialisierung">Kapitel 41</a> ausführlich
gewürdigt wird, wollen wir uns nachfolgend die Bedeutung des
Interfaces <a href="index_c.html#ixb100455"><font color=#000080><tt>Cloneable</tt></font></a>
ansehen.
<p>
<a href="index_c.html#ixb100455"><font color=#000080><tt>Cloneable</tt></font></a>
ist ein Schalter für die in der Klasse <a href="index_o.html#ixb100224"><font color=#000080><tt>Object</tt></font></a>
implementierte Methode <a name="ixa100564"><a href="index_c.html#ixb100278"><font color=#000080><tt>clone</tt></font></a></a>.
Implementiert eine abgeleitete Klasse dieses Interface nicht, so deutet
<a href="index_c.html#ixb100278"><font color=#000080><tt>clone</tt></font></a>
das als fehlende Fähigkeit (oder Bereitschaft) der Klasse, eine
Objektkopie herzustellen, und löst beim Aufruf eine <a name="ixa100565"><a href="index_c.html#ixb100456"><font color=#000080><tt>CloneNotSupportedException</tt></font></a></a>
aus. Implementiert die abgeleitete Klasse dagegen <a href="index_c.html#ixb100455"><font color=#000080><tt>Cloneable</tt></font></a>,
erzeugt ein Aufruf von <a href="index_c.html#ixb100278"><font color=#000080><tt>clone</tt></font></a>
eine elementweise Kopie des aktuellen Objekts.
<p>
Es ist wichtig zu verstehen, was der Begriff <i>elementweise</i> bedeutet
- insbesondere wenn die Klasse Objekte als Membervariablen enthält.
Beim Aufruf von <a href="index_c.html#ixb100278"><font color=#000080><tt>clone</tt></font></a>
werden nämlich lediglich die <i>Verweise</i> auf diese Membervariablen
kopiert, nicht aber die dahinter stehenden Objekte (bei primitiven
Membervariablen macht das keinen Unterschied, denn sie werden nicht
als Zeiger gespeichert). Diese Vorgehensweise wird auch als <a name="ixa100566"><i>shallow
copy</i></a> bezeichnet (»flache Kopie«).
<p>
Soll eine <a name="ixa100567"><i>deep copy</i></a> (»tiefe Kopie«)
angelegt werden, muss man <a href="index_c.html#ixb100278"><font color=#000080><tt>clone</tt></font></a>
überlagern und selbst dafür sorgen, dass alle vorhandenen
Objekt-Membervariablen kopiert werden. Da jedes Memberobjekt weitere
Objekte enthalten kann, die kopiert werden müssen, ist das Erstellen
einer tiefen Kopie in der Regel ein rekursiver Vorgang.
<p>
Wir wollen uns beispielhaft das tiefe Kopieren der folgenden Klasse
<font color="#000077"><tt>BinTreeNode</tt></font> ansehen. Diese stellt
einen Knoten in einem Binärbaum dar und besitzt einen Namen und
die üblichen Verweise auf den linken und rechten Unterbaum, ebenfalls
vom Typ <font color="#000077"><tt>BinTreeNode</tt></font>. Beim Kopieren
wird das aktuelle Objekt durch Aufruf von <a name="ixa100568"><a href="index_s.html#ixb100459"><font color=#000080><tt>super.clone</tt></font></a></a>
zunächst flach kopiert. Dann wird <a href="index_c.html#ixb100278"><font color=#000080><tt>clone</tt></font></a>
rekursiv aufgerufen, um Kopien des linken bzw. rechten Unterbaums
anzulegen. Enthält ein Unterbaum den Wert <a href="index_n.html#ixb100235"><font color=#000080><tt>null</tt></font></a>,
so terminiert der Kopiervorgang und mit ihm die Rekursion. Auf diese
Weise wird der Knoten mit allen Unterknoten, Unterunterknoten usw.
kopiert und es entsteht eine Kopie, deren Objektvariablen keine gemeinsamen
Objekte mit dem Original mehr besitzen.
<a name="listingid009013"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* BinTreeNode.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">class</font> BinTreeNode
<font color="#555555">004 </font><font color="#0000AA">implements</font> Cloneable
<font color="#555555">005 </font>{
<font color="#555555">006 </font> String name;
<font color="#555555">007 </font> BinTreeNode leftChild;
<font color="#555555">008 </font> BinTreeNode rightChild;
<font color="#555555">009 </font>
<font color="#555555">010 </font> <font color="#0000AA">public</font> BinTreeNode(String name)
<font color="#555555">011 </font> {
<font color="#555555">012 </font> <font color="#006699">this</font>.name = name;
<font color="#555555">013 </font> <font color="#006699">this</font>.leftChild = <font color="#006699">null</font>;
<font color="#555555">014 </font> <font color="#006699">this</font>.rightChild = <font color="#006699">null</font>;
<font color="#555555">015 </font> }
<font color="#555555">016 </font>
<font color="#555555">017 </font> <font color="#0000AA">public</font> Object clone()
<font color="#555555">018 </font> {
<font color="#555555">019 </font> <font color="#0000AA">try</font> {
<font color="#555555">020 </font> BinTreeNode newNode = (BinTreeNode)<font color="#006699">super</font>.clone();
<font color="#555555">021 </font> <font color="#0000AA">if</font> (<font color="#006699">this</font>.leftChild != <font color="#006699">null</font>) {
<font color="#555555">022 </font> newNode.leftChild = (BinTreeNode)<font color="#006699">this</font>.leftChild.clone();
<font color="#555555">023 </font> }
<font color="#555555">024 </font> <font color="#0000AA">if</font> (<font color="#006699">this</font>.rightChild != <font color="#006699">null</font>) {
<font color="#555555">025 </font> newNode.rightChild = (BinTreeNode)<font color="#006699">this</font>.rightChild.clone();
<font color="#555555">026 </font> }
<font color="#555555">027 </font> <font color="#0000AA">return</font> newNode;
<font color="#555555">028 </font> } <font color="#0000AA">catch</font> (CloneNotSupportedException e) {
<font color="#555555">029 </font> <font color="#00AA00">//Kann eigentlich nicht auftreten...</font>
<font color="#555555">030 </font> <font color="#0000AA">throw</font> <font color="#0000AA">new</font> InternalError();
<font color="#555555">031 </font> }
<font color="#555555">032 </font> }
<font color="#555555">033 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/BinTreeNode.java"><font color="#000055" size=-1>BinTreeNode.java</font></a></td>
</tr>
</table>
<i>
Listing 9.13: Implementierung einer tiefen Kopie</i></p>
<!-- Section -->
<a name="fzeigerinterfaces"></a>
<h3>9.4.3 Nachbildung von Funktionszeigern <a name="ixa100569"></a>
</h3>
<p>
Eine wichtige Anwendung von Interfaces besteht darin, die aus C oder
C++ bekannten, aber in Java nicht vorhandenen Funktionszeiger nachzubilden,
die es ermöglichen, eine Funktion als Argument an andere Funktionen
zu übergeben. Nützlich ist das vor allem, wenn die Konfigurationsanforderungen
einer Funktion die durch die Übergabe von Variablen gegebenen
Möglichkeiten übersteigen. Beispiele für ihre Anwendung
sind etwa Funktionsplotter oder Callback-Funktionen bei der Programmierung
grafischer Oberflächen.
<p>
Funktionszeiger können leicht mit Hilfe von Interfaces nachgebildet
werden. Dazu wird zunächst ein Interface definiert, das eine
einzelne Methode <font color="#000077"><tt>f</tt></font> des gewünschten
Typs deklariert. Es kann dann von unterschiedlichen Klassen so implementiert
werden, dass in <font color="#000077"><tt>f</tt></font> jeweils die
gewünschte Berechnung ausgeführt wird. Anstelle der Übergabe
eines Zeigers wird nun einfach ein Objekt dieser Klasse instanziert
und an die zu konfigurierende Methode übergeben. Diese wird vom
Typ des Interfaces deklariert und kann so die Methode <font color="#000077"><tt>f</tt></font>
aufrufen.
<p>
Als Beispiel soll ein Programm geschrieben werden, das in der Lage
ist, eine Wertetabelle für beliebige <a href="index_d.html#ixb100247"><font color=#000080><tt>double</tt></font></a>-Funktionen
auszugeben. Wir definieren dazu zunächst ein Interface <font color="#000077"><tt>DoubleMethod</tt></font>,
das eine Methode <font color="#000077"><tt>compute</tt></font> deklariert,
die zu einem <a href="index_d.html#ixb100247"><font color=#000080><tt>double</tt></font></a>-Argument
ein <a href="index_d.html#ixb100247"><font color=#000080><tt>double</tt></font></a>-Ergebnis
berechnet.
<a name="doublemethod"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* DoubleMethod.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">interface</font> DoubleMethod
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#0000AA">public</font> <font color="#006699">double</font> compute(<font color="#006699">double</font> value);
<font color="#555555">006 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/DoubleMethod.java"><font color="#000055" size=-1>DoubleMethod.java</font></a></td>
</tr>
</table>
<i>
Listing 9.14: Das Interface DoubleMethod</i></p>
<p>
Anschließend implementieren wir das Interface in verschiedenen
Klassen und stellen die Funktionen <i>Exponentation</i>, <i>Quadratwurzel</i>,
<i>Multiplikation mit zwei</i> und <i>Quadrat</i> zur Verfügung.
Anschließend instanzieren wir diese Klassen und übergeben
die Objekte nacheinander an die Methode <font color="#000077"><tt>printTable</tt></font>,
mit der die Wertetabelle erzeugt und ausgegeben wird:
<a name="fzeigerbeispiel"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing0915.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">class</font> MathExp
<font color="#555555">004 </font><font color="#0000AA">implements</font> DoubleMethod
<font color="#555555">005 </font>{
<font color="#555555">006 </font> <font color="#0000AA">public</font> <font color="#006699">double</font> compute(<font color="#006699">double</font> value)
<font color="#555555">007 </font> {
<font color="#555555">008 </font> <font color="#0000AA">return</font> Math.exp(value);
<font color="#555555">009 </font> }
<font color="#555555">010 </font>}
<font color="#555555">011 </font>
<font color="#555555">012 </font><font color="#0000AA">class</font> MathSqrt
<font color="#555555">013 </font><font color="#0000AA">implements</font> DoubleMethod
<font color="#555555">014 </font>{
<font color="#555555">015 </font> <font color="#0000AA">public</font> <font color="#006699">double</font> compute(<font color="#006699">double</font> value)
<font color="#555555">016 </font> {
<font color="#555555">017 </font> <font color="#0000AA">return</font> Math.sqrt(value);
<font color="#555555">018 </font> }
<font color="#555555">019 </font>}
<font color="#555555">020 </font>
<font color="#555555">021 </font><font color="#0000AA">class</font> Times2
<font color="#555555">022 </font><font color="#0000AA">implements</font> DoubleMethod
<font color="#555555">023 </font>{
<font color="#555555">024 </font> <font color="#0000AA">public</font> <font color="#006699">double</font> compute(<font color="#006699">double</font> value)
<font color="#555555">025 </font> {
<font color="#555555">026 </font> <font color="#0000AA">return</font> 2 * value;
<font color="#555555">027 </font> }
<font color="#555555">028 </font>}
<font color="#555555">029 </font>
<font color="#555555">030 </font><font color="#0000AA">class</font> Sqr
<font color="#555555">031 </font><font color="#0000AA">implements</font> DoubleMethod
<font color="#555555">032 </font>{
<font color="#555555">033 </font> <font color="#0000AA">public</font> <font color="#006699">double</font> compute(<font color="#006699">double</font> value)
<font color="#555555">034 </font> {
<font color="#555555">035 </font> <font color="#0000AA">return</font> value * value;
<font color="#555555">036 </font> }
<font color="#555555">037 </font>}
<font color="#555555">038 </font>
<font color="#555555">039 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing0915
<font color="#555555">040 </font>{
<font color="#555555">041 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> printTable(DoubleMethod meth)
<font color="#555555">042 </font> {
<font color="#555555">043 </font> System.out.println(<font color="#0000FF">"Wertetabelle "</font> + meth.toString());
<font color="#555555">044 </font> <font color="#0000AA">for</font> (<font color="#006699">double</font> x = 0.0; x <= 5.0; x += 1) {
<font color="#555555">045 </font> System.out.println(<font color="#0000FF">" "</font> + x + <font color="#0000FF">"->"</font> + meth.compute(x));
<font color="#555555">046 </font> }
<font color="#555555">047 </font> }
<font color="#555555">048 </font>
<font color="#555555">049 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">050 </font> {
<font color="#555555">051 </font> printTable(<font color="#0000AA">new</font> Times2());
<font color="#555555">052 </font> printTable(<font color="#0000AA">new</font> MathExp());
<font color="#555555">053 </font> printTable(<font color="#0000AA">new</font> Sqr());
<font color="#555555">054 </font> printTable(<font color="#0000AA">new</font> MathSqrt());
<font color="#555555">055 </font> }
<font color="#555555">056 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing0915.java"><font color="#000055" size=-1>Listing0915.java</font></a></td>
</tr>
</table>
<i>
Listing 9.15: Funktionszeiger mit Interfaces</i></p>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
In <a href="k100274.html#fzeigerreflection">Abschnitt 43.3.2</a> wird
gezeigt, wie man Funktionszeiger auf ähnliche Weise mit dem Reflection-API
realisieren kann.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100057.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100060.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100062.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100064.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">© 1998, 2007 Guido Krüger & Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>
|