Lecture overview -- Keyboard shortcut: 'u'  Previous page: Eksempel: Stakken igen -- Keyboard shortcut: 'p'  Next page: Eksempel: Microbib -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  Annotated slide -- Keyboard shortcut: 't'  Alphabetic index  Help page about these notes  Course home    Design af klassehierarkier - slide 8 : 26

Eksempel: Comparable 

abstract class Comparable {
  
  /** Return whether I am less than other? */
  abstract boolean lessThan(Comparable other);

  /** Return whether I am less than or equal to other? */
  boolean lessThanOrEqual (Comparable other){
    return (this.lessThan(other) || this.equals(other));
  }

  /** Return whether I am greater than other? */
  boolean greaterThan (Comparable other){
    return (!this.lessThanOrEqual(other));
  }

  /** Return whether I am greater than or equal to other? */
  boolean greaterThanOrEqual(Comparable other){
    return (!this.lessThan(other));
  }
}
 

Det giver kun mening at sammenligne objekter som tilhører samme klasse

Dette er svært at udtrykke statisk i Java

 

Go to exerciseSammenlignelige bankkonti