Commit d3a34cd1 authored by Dominikus Herzberg's avatar Dominikus Herzberg

HTML generiert

parent 8c38b239
......@@ -654,11 +654,14 @@ false</pre>
<span class="local-variable">this</span>.y = y;
}
<span class="directive">public</span> <span class="type">boolean</span> equals(<span class="predefined-type">Object</span> other) {
<span class="keyword">if</span> (other <span class="keyword">instanceof</span> <span class="predefined-type">Point</span>) { <i class="conum" data-value="1"></i><b>(1)</b>
<span class="predefined-type">Point</span> otherPoint = (<span class="predefined-type">Point</span>) other; <i class="conum" data-value="2"></i><b>(2)</b>
<span class="keyword">return</span> otherPoint.x == <span class="local-variable">this</span>.x &amp;&amp; otherPoint.y == <span class="local-variable">this</span>.y;
}
<span class="keyword">return</span> <span class="predefined-constant">false</span>;
<span class="keyword">if</span> (other == <span class="predefined-constant">null</span>) <span class="keyword">return</span> <span class="predefined-constant">false</span>;
<span class="keyword">if</span> (other == <span class="local-variable">this</span>) <span class="keyword">return</span> <span class="predefined-constant">true</span>;
<span class="keyword">if</span> (!(other <span class="keyword">instanceof</span> <span class="predefined-type">Point</span>)) <span class="keyword">return</span> <span class="predefined-constant">false</span>; <i class="conum" data-value="1"></i><b>(1)</b>
<span class="predefined-type">Point</span> otherPoint = (<span class="predefined-type">Point</span>) other; <i class="conum" data-value="2"></i><b>(2)</b>
<span class="keyword">return</span> otherPoint.x == <span class="local-variable">this</span>.x &amp;&amp; otherPoint.y == <span class="local-variable">this</span>.y;
}
<span class="directive">public</span> <span class="type">int</span> hashCode() {
<span class="keyword">return</span> Objects.hash(x, y); <i class="conum" data-value="3"></i><b>(3)</b>
}
}
......@@ -668,11 +671,14 @@ false</pre>
<span class="local-variable">this</span>.tachometer = tachometer;
}
<span class="directive">public</span> <span class="type">boolean</span> equals(<span class="predefined-type">Object</span> other) {
<span class="keyword">if</span> (other <span class="keyword">instanceof</span> Car) { <i class="conum" data-value="1"></i><b>(1)</b>
Car otherCar = (Car) other; <i class="conum" data-value="2"></i><b>(2)</b>
<span class="keyword">return</span> otherCar.tachometer == <span class="local-variable">this</span>.tachometer;
}
<span class="keyword">return</span> <span class="predefined-constant">false</span>;
<span class="keyword">if</span> (other == <span class="predefined-constant">null</span>) <span class="keyword">return</span> <span class="predefined-constant">false</span>;
<span class="keyword">if</span> (other == <span class="local-variable">this</span>) <span class="keyword">return</span> <span class="predefined-constant">true</span>;
<span class="keyword">if</span> (!(other <span class="keyword">instanceof</span> Car)) <span class="keyword">return</span> <span class="predefined-constant">false</span>; <i class="conum" data-value="1"></i><b>(1)</b>
Car otherCar = (Car) other; <i class="conum" data-value="2"></i><b>(2)</b>
<span class="keyword">return</span> otherCar.tachometer == <span class="local-variable">this</span>.tachometer;
}
<span class="directive">public</span> <span class="type">int</span> hashCode() {
<span class="keyword">return</span> Objects.hash(tachometer); <i class="conum" data-value="3"></i><b>(3)</b>
}
}
......@@ -688,22 +694,29 @@ false</pre>
<span class="local-variable">this</span>.denominator = denominator;
}
<span class="directive">public</span> <span class="type">boolean</span> equals(<span class="predefined-type">Object</span> other) {
<span class="keyword">if</span> (other <span class="keyword">instanceof</span> Fraction) { <i class="conum" data-value="1"></i><b>(1)</b>
Fraction otherFrac = (Fraction) other; <i class="conum" data-value="2"></i><b>(2)</b>
<span class="keyword">return</span> otherFrac.numerator == <span class="local-variable">this</span>.numerator
&amp;&amp; otherFrac.denominator == <span class="local-variable">this</span>.denominator;
}
<span class="keyword">return</span> <span class="predefined-constant">false</span>;
<span class="keyword">if</span> (other == <span class="predefined-constant">null</span>) <span class="keyword">return</span> <span class="predefined-constant">false</span>;
<span class="keyword">if</span> (other == <span class="local-variable">this</span>) <span class="keyword">return</span> <span class="predefined-constant">true</span>;
<span class="keyword">if</span> (!(other <span class="keyword">instanceof</span> Fraction)) <span class="keyword">return</span> <span class="predefined-constant">false</span>; <i class="conum" data-value="1"></i><b>(1)</b>
Fraction otherFrac = (Fraction) other; <i class="conum" data-value="2"></i><b>(2)</b>
<span class="keyword">return</span> otherFrac.numerator == <span class="local-variable">this</span>.numerator
&amp;&amp; otherFrac.denominator == <span class="local-variable">this</span>.denominator;
}
<span class="directive">public</span> <span class="type">int</span> hashCode() {
<span class="keyword">return</span> Objects.hash(numerator, denominator); <i class="conum" data-value="3"></i><b>(3)</b>
}
}
<span class="keyword">assert</span> !<span class="keyword">new</span> Fraction(<span class="integer">1</span>,<span class="integer">2</span>).equals(<span class="keyword">new</span> Car(<span class="float">123.0</span>));
<span class="keyword">assert</span> <span class="keyword">new</span> Fraction(<span class="integer">1</span>,<span class="integer">2</span>).equals(<span class="keyword">new</span> Fraction(<span class="integer">1</span>,<span class="integer">2</span>));
<span class="keyword">assert</span> <span class="keyword">new</span> Fraction(<span class="integer">1</span>,<span class="integer">2</span>).hashCode() == <span class="keyword">new</span> Fraction(<span class="integer">1</span>,<span class="integer">2</span>).hashCode();
<span class="keyword">assert</span> <span class="keyword">new</span> Fraction(<span class="integer">2</span>).equals(<span class="keyword">new</span> Fraction(<span class="integer">2</span>,<span class="integer">1</span>));
<span class="keyword">assert</span> <span class="keyword">new</span> Fraction(<span class="integer">2</span>).hashCode() == <span class="keyword">new</span> Fraction(<span class="integer">2</span>, <span class="integer">1</span>).hashCode();
<span class="keyword">assert</span> !<span class="keyword">new</span> Car(<span class="integer">12</span>).equals(<span class="keyword">new</span> Car(<span class="float">12.1</span>));
<span class="keyword">assert</span> <span class="keyword">new</span> Car(<span class="integer">12</span>).equals(<span class="keyword">new</span> Car(<span class="float">12.0</span>));
<span class="keyword">assert</span> <span class="keyword">new</span> Car(<span class="integer">2</span>).hashCode() == <span class="keyword">new</span> Car(<span class="integer">2</span>).hashCode();
<span class="keyword">assert</span> !<span class="keyword">new</span> <span class="predefined-type">Point</span>(<span class="integer">3</span>,<span class="integer">4</span>).equals(<span class="keyword">new</span> <span class="predefined-type">Point</span>(<span class="integer">4</span>,<span class="integer">3</span>));
<span class="keyword">assert</span> <span class="keyword">new</span> <span class="predefined-type">Point</span>(<span class="integer">3</span>,<span class="integer">4</span>).equals(<span class="keyword">new</span> <span class="predefined-type">Point</span>(<span class="integer">3</span>,<span class="integer">4</span>));</code></pre>
<span class="keyword">assert</span> <span class="keyword">new</span> <span class="predefined-type">Point</span>(<span class="integer">3</span>,<span class="integer">4</span>).equals(<span class="keyword">new</span> <span class="predefined-type">Point</span>(<span class="integer">3</span>,<span class="integer">4</span>));
<span class="keyword">assert</span> <span class="keyword">new</span> <span class="predefined-type">Point</span>(<span class="integer">3</span>,<span class="integer">4</span>).hashCode() == <span class="keyword">new</span> <span class="predefined-type">Point</span>(<span class="integer">3</span>,<span class="integer">4</span>).hashCode();</code></pre>
</div>
</div>
<div class="colist arabic">
......@@ -716,6 +729,10 @@ false</pre>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Nachdem wir überprüft haben, dass das Objekt den richtigen Typ hat können wir es zu diesem Typ <em>casten</em>, um auf die relevanten Felder zugreifen zu können.</td>
</tr>
<tr>
<td><i class="conum" data-value="3"></i><b>3</b></td>
<td>Eine Grundregel von Java lautet "Wer <code>equals</code> sagt, muss auch <code>hashCode</code> sagen.". Überschreibt man die Funktionalität von <code>equals</code>, muss man gewährleisten, dass für zwei Objekte für die <code>x.equals(y)</code> den Wert <code>true</code> ergibt auch <code>x.hashCode() == y.hashCode()</code> gelten muss. Eine einfache Lösung dafür ist die Hilfsmethode <code>hash()</code> der Klasse <code>java.util.Objects</code>. An diese Methode müssen alle Objekte übergeben werden, die bei dem Vergleich relevant sind.</td>
</tr>
</table>
</div>
</details>
......@@ -1010,7 +1027,7 @@ class SVGCircle extends SVGElement {
</div>
<div id="footer">
<div id="footer-text">
Last updated 2017-11-14 22:23:01 Mitteleuropõische Zeit
Last updated 2017-11-21 09:04:25 Mitteleuropõische Zeit
</div>
</div>
</body>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment