This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | Next revision Both sides next revision | ||
|
java:interfaces [2016/04/09 06:46] gthanos [Στατικές μέθοδοι] |
java:interfaces [2016/04/09 06:48] gthanos [Χρησιμοποιώντας ένα interface ως τύπο δεδομένων] |
||
|---|---|---|---|
| Line 150: | Line 150: | ||
| Γράφουμε την Rectangle ώστε να υλοποιεί και το νέο μας interface: | Γράφουμε την Rectangle ώστε να υλοποιεί και το νέο μας interface: | ||
| <code java Rectangle.java> | <code java Rectangle.java> | ||
| - | public class Rectangle implements Comparable, MyComparable{ | + | public class Rectangle implements MyComparable{ |
| | | ||
| // the Rectangle class has 3 fields | // the Rectangle class has 3 fields | ||
| Line 189: | Line 189: | ||
| | | ||
| public Point getOrigin() { return origin; } | public Point getOrigin() { return origin; } | ||
| - | + | ||
| - | @Override | + | |
| - | public int compareTo(Object other) { | + | |
| - | Rectangle otherRect = (Rectangle) other; | + | |
| - | return (this.getArea() - otherRect.getArea()); | + | |
| - | } | + | |
| - | + | ||
| @Override | @Override | ||
| public boolean isLarger(MyComparable other) { | public boolean isLarger(MyComparable other) { | ||
| Line 218: | Line 212: | ||
| public class TwosComparator { | public class TwosComparator { | ||
| | | ||
| - | public MyComparable findLargest(MyComparable object1, MyComparable object2) { | + | public MyComparable findLarger(MyComparable object1, MyComparable object2) { |
| if (object1.isLarger(object2)) | if (object1.isLarger(object2)) | ||
| return object1; | return object1; | ||
| Line 225: | Line 219: | ||
| } | } | ||
| | | ||
| - | public MyComparable findSmallest(MyComparable object1, MyComparable object2) { | + | public MyComparable findSmaller(MyComparable object1, MyComparable object2) { |
| if (!(object1.isLarger(object2))) | if (!(object1.isLarger(object2))) | ||
| return object1; | return object1; | ||
| Line 246: | Line 240: | ||
| System.out.println("rec2 is " + rec2); | System.out.println("rec2 is " + rec2); | ||
| - | System.out.println( comp.findLargest(rec1, rec2) + " is larger!"); | + | System.out.println( comp.findLarger(rec1, rec2) + " is larger!"); |
| - | System.out.println( comp.findSmallest(rec1, rec2) + " is smaller!"); | + | System.out.println( comp.findSmaller(rec1, rec2) + " is smaller!"); |
| } | } | ||
| } | } | ||