Alain Ravet
Posts: 19
Nickname: aravet
Registered: Feb, 2002
|
|
Re: Comparable Class (overiding)
|
Posted: Mar 5, 2002 1:41 PM
|
|
You must first have a rule to compare 2 objects of the same class.
Example : Pixel (1,2) < Pixel (2,3) < Pixel (1,5) because 1+2 < 2+3 < 1+5 => new Pixel (2,3).compareTo (new Pixel(1,5)) < 0 ; and 0 < new Pixel (2,3).compareTo (new Pixel(1,2)) ;
public class Pixel implements Comparable
{
public int compareTo( Object o )
{
final Pixel pixel = ( (Pixel ) o );
final int rad1 = pixel.x + pixel.y;
final int radThis = this.x + this.y;
return radThis - rad1 ;
}
public Pixel( int i_x , int i_y )
{
x = i_x;
y = i_y;
}
public int x ;
public int y ;
...
}
|
|