You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch6_definingclasses.ptx
+14-9Lines changed: 14 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -782,11 +782,11 @@ public void test(Number a, Number b) {
782
782
783
783
<p>
784
784
The <c>Comparable</c> interface says that any object that claims to be <c>Comparable</c> must implement the <c>compareTo</c> method.
785
-
Here is an excerpt from <urlhref="https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T)"visual="https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T)">the official documentation</url> for the <c>compareTo</c> method as specified by the <c>Comparable</c> interface.
785
+
Here is an excerpt from <urlhref="https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T)"visual="https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T)">the official documentation</url> for the <c>compareTo</c> method as specified by the <c>Comparable</c> interface. <xrefref="java-comparable"text="type-global"/> shows the excerpt.
786
786
</p>
787
787
788
-
789
-
<programxml:id="java-comparable"language="text">
788
+
<listingxml:id="java-comparable">
789
+
<programlanguage="text">
790
790
<code>
791
791
int compareTo(T o)
792
792
Compares this object with the specified object for order. Returns a
@@ -798,27 +798,29 @@ iff y.compareTo(x) throws an exception.)
798
798
...
799
799
</code>
800
800
</program>
801
+
</listing>
801
802
802
803
<p>
803
-
To make our <c>Fraction</c> class <c>Comparable</c> we must modify the class declaration line as follows:
804
+
To make our <c>Fraction</c> class <c>Comparable</c> we must modify the class declaration line as shown in <xrefref="java-make-comparable"text="type-global"/>.
public class Fraction extends Number implements Comparable<Fraction> { // fraction class is a child of Number and implements the Comparable interface
810
811
...
811
812
}
812
813
</code>
813
814
</program>
815
+
</listing>
814
816
815
817
<p>
816
818
The specification <c>Comparable<Fraction></c> makes it clear that <c>Fraction</c> is only comparable with another <c>Fraction</c>.
817
-
The <c>compareTo</c> method could be implemented as follows:
819
+
The <c>compareTo</c> method could be implemented as shown in <xrefref="java-compare-to"text="type-global"/>.
818
820
</p>
819
821
820
-
821
-
<programxml:id="java-comparable2"language="java">
822
+
<listingxml:id="java-compare-to">
823
+
<programlanguage="java">
822
824
<code>
823
825
public int compareTo(Fraction other) { // compare this fraction with another fraction
0 commit comments