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
|
<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,k100058.html;107,k100060.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="k100058.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100060.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="interfacecomparable"></a>
<h2>9.2 Das Interface <a name="ixa100553">Comparable</a> </h2>
<hr>
<ul>
<li><a href="k100059.html#interfacecomparable">9.2 Das Interface Comparable</a>
</ul>
<hr>
<p>
Interfaces werden verwendet, um Eigenschaften auszudrücken, die
auf Klassen aus unterschiedlichen Klassenhierarchien zutreffen können.
Das erkennt man auch daran, dass ihre Namen oft (substantivierte)
Eigenschaftswörter sind. Ein bekanntes Beispiel, das mit der
Version 1.2 in der Java-Klassenbibliothek eingeführt wurde, ist
das Interface <a name="ixa100554"><a href="index_c.html#ixb100446"><font color=#000080><tt>Comparable</tt></font></a></a>
des Pakets <a href="index_j.html#ixb100188"><font color=#000080><tt>java.lang</tt></font></a>:
<font color="#000077">
<pre>
public interface Comparable
{
public int compareTo(Object o);
}
</pre>
</font>
<p>
Dieses Interface kann von Klassen implementiert werden, deren Objekte
paarweise <i>vergleichbar</i> sind. Die Methode <a name="ixa100555"><a href="index_c.html#ixb100447"><font color=#000080><tt>compareTo</tt></font></a></a>
liefert genau dann einen Wert kleiner 0, wenn das Objekt »kleiner«;
größer 0, wenn es »größer«, und gleich
0, wenn es »gleich« dem als Argument übergebenen Objekt
<font color="#000077"><tt>o</tt></font> ist. In der Klassenbibliothek
gibt es eine ganze Reihe von Klassen, die <a href="index_c.html#ixb100446"><font color=#000080><tt>Comparable</tt></font></a>
implementieren, beispielsweise <font color="#000077"><tt>String</tt></font>
und <font color="#000077"><tt>Character</tt></font> oder die numerischen
Wrapper-Klassen (letztere werden in <a href="k100066.html#wrapperklassen">Abschnitt 10.2</a>
erläutert).
<p>
Mit Hilfe von <a href="index_c.html#ixb100446"><font color=#000080><tt>Comparable</tt></font></a>
kann die Reihenfolge der Objekte einer Klasse ermittelt werden. Aus
dem paarweisen Vergleich läßt sich eine (nicht notwendigerweise
eindeutige) implizite Ordnung der Elemente ableiten, denn für
alle aufeinanderfolgenden Objekte <font color="#000077"><tt>a</tt></font>
und <font color="#000077"><tt>b</tt></font> muss <font color="#000077"><tt>a.compareTo(b)
<= 0</tt></font> gelten. Damit ist es möglich, Methoden zu
schreiben, die das kleinste oder größte Element einer Menge
von Objekten ermitteln oder diese sortieren:
<a name="listingid009007"></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">/* Listing0907.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing0907
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> Object getSmallest(Comparable[] objects)
<font color="#555555">006 </font> {
<font color="#555555">007 </font> Object smallest = objects[0];
<font color="#555555">008 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 1; i < objects.length; ++i) {
<font color="#555555">009 </font> <font color="#0000AA">if</font> (objects[i].compareTo(smallest) < 0) {
<font color="#555555">010 </font> smallest = objects[i];
<font color="#555555">011 </font> }
<font color="#555555">012 </font> }
<font color="#555555">013 </font> <font color="#0000AA">return</font> smallest;
<font color="#555555">014 </font> }
<font color="#555555">015 </font>
<font color="#555555">016 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> bubbleSort(Comparable[] objects)
<font color="#555555">017 </font> {
<font color="#555555">018 </font> <font color="#006699">boolean</font> sorted;
<font color="#555555">019 </font> <font color="#0000AA">do</font> {
<font color="#555555">020 </font> sorted = <font color="#006699">true</font>;
<font color="#555555">021 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i < objects.length - 1; ++i) {
<font color="#555555">022 </font> <font color="#0000AA">if</font> (objects[i].compareTo(objects[i + 1]) > 0) {
<font color="#555555">023 </font> Comparable tmp = objects[i];
<font color="#555555">024 </font> objects[i] = objects[i + 1];
<font color="#555555">025 </font> objects[i + 1] = tmp;
<font color="#555555">026 </font> sorted = <font color="#006699">false</font>;
<font color="#555555">027 </font> }
<font color="#555555">028 </font> }
<font color="#555555">029 </font> } <font color="#0000AA">while</font> (!sorted);
<font color="#555555">030 </font> }
<font color="#555555">031 </font>
<font color="#555555">032 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">033 </font> {
<font color="#555555">034 </font> <font color="#00AA00">//Erzeugen eines String-Arrays</font>
<font color="#555555">035 </font> Comparable[] objects = <font color="#0000AA">new</font> Comparable[4];
<font color="#555555">036 </font> objects[0] = <font color="#0000FF">"STRINGS"</font>;
<font color="#555555">037 </font> objects[1] = <font color="#0000FF">"SIND"</font>;
<font color="#555555">038 </font> objects[2] = <font color="#0000FF">"PAARWEISE"</font>;
<font color="#555555">039 </font> objects[3] = <font color="#0000FF">"VERGLEICHBAR"</font>;
<font color="#555555">040 </font> <font color="#00AA00">//Ausgeben des kleinsten Elements</font>
<font color="#555555">041 </font> System.out.println((String)getSmallest(objects));
<font color="#555555">042 </font> System.out.println(<font color="#0000FF">"--"</font>);
<font color="#555555">043 </font> <font color="#00AA00">//Sortieren und Ausgaben</font>
<font color="#555555">044 </font> bubbleSort(objects);
<font color="#555555">045 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i < objects.length; ++i) {
<font color="#555555">046 </font> System.out.println((String)objects[i]);
<font color="#555555">047 </font> }
<font color="#555555">048 </font> }
<font color="#555555">049 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing0907.java"><font color="#000055" size=-1>Listing0907.java</font></a></td>
</tr>
</table>
<i>
Listing 9.7: Das Interface Comparable</i></p>
<p>
Die Implementierung der Methode <font color="#000077"><tt>getSmallest</tt></font>
ist leicht zu verstehen. Das kleinste Element wird gefunden, indem
jedes Element des Arrays mit dem bis dato kleinsten Element verglichen
wird. Ist es kleiner, wird das bisherige kleinste Element ersetzt,
andernfalls bleibt es unverändert. Die Implementierung von <font color="#000077"><tt>bubbleSort</tt></font>
folgt ebenfalls einem einfachen Schema. In jedem Arraydurchlauf werden
zwei aufeinanderfolgende Elemente vertauscht, falls das zweite kleiner
als das erste ist. Es werden so viele Durchläufe vorgenommen,
bis keine Vertauschung mehr stattgefunden hat.
<p>
Bemerkenswert ist hierbei die <a name="ixa100556"><i>Generizität</i></a>
(Typunabhängigkeit) der Lösung. Es spielt keine Rolle, welche
Art von Objekten sortiert wird, solange alle das Interface <a href="index_c.html#ixb100446"><font color=#000080><tt>Comparable</tt></font></a>
implementieren. Bei Strings ist das standardmäßig der Fall;
eigene Klassen könnten ihre eigene Implementierung von <a href="index_c.html#ixb100446"><font color=#000080><tt>Comparable</tt></font></a>
beisteuern. Bei Objekten, die auch <font color="#000077"><tt>Groesse</tt></font>
implementieren, könnte beispielsweise das <i>Volumen</i> (Länge
mal Breite mal Höhe) oder eine beliebige andere Verknüpfung
der Raumlängen verwendet werden.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Auch Objekte unterschiedlicher Klassen können problemlos miteinander
verglichen werden, sofern <a href="index_c.html#ixb100447"><font color=#000080><tt>compareTo</tt></font></a>
dazu in der Lage ist. So ist es leicht vorstellbar, dass sowohl Autos
als auch Fußballplätze und Papierblätter (und eventuell
noch Äpfel und Birnen) auf der Basis ihrer Grundfläche (siehe
<a href="k100058.html#grundflaeche">Listing 9.5</a>) miteinander verglichen
werden, denn diese Maßzahl hat für alle beteiligten Klassen
eine Bedeutung.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#0099CC">
<tr>
<td><font color="#FFFFFF"> Tipp </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#0099CC"><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="k100058.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100060.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>
|