java - Comparable and compareto -
i have problem. have class extends class , implements comparable. when tried compile program had error: flight not abstract , not override abstract method compareto. anyway, code of flight this:
class flight extends keyflight implements comparable<flight>{   public keyflight kf;  public boolean departure;   public void flight(){  keyflight kf=new keyflight();  boolean departure=false;  }   public int compareto(keyflight other){   if (kf.time==other.time){   if (kf.key<other.key){    return kf.key;   } else {    return other.key;   }   } else {   if (kf.time <other.time){    return kf.key;   } else {    return other.key;   }    } } } thank in advance!
you should use
implements comparable<keyflight> instead of
implements comparable<flight> as want compare 2 keyflights rather flights itself. type of parameter defines in compareto, should match type specified in implements clause going compare with.
another issue code is, in constructor re-defining keyflight, should be
public void flight(){     kf=new keyflight(); else nullpointerexception in future. same applies departure boolean.
a side note, default in java boolean initialised false, don't have explicitly in constructor.
Comments
Post a Comment