Java |
Java doesn't have operator overloading.
You have to work around.
public class Score implements Comparable
{
int value;
public Score (int score) {
value = score;
}
public int compareTo (Object o) {
return value - ((Score)o).value;
}
public boolean equals(Object o) {
return value==((Score)o).value;
}
public static void main(String[] args) {
Score a = new Score(5);
Score b = new Score(5);
Object c = a;
System.out.println(a.compareTo(b));//0
System.out.println(a==b); //false
System.out.println(a==c); //true
System.out.println(a.equals(b)); //true
System.out.println(a>c); //illegal
}
}
|
|
C# |
public class Score : IComparable
{
int value;
public Score (int score) {
value = score;
}
public static bool operator == (Score x, Score y) {
return x.value == y.value;
}
public static bool operator != (Score x, Score y) {
return x.value != y.value;
}
public int CompareTo (object o) {
return value - ((Score)o).value;
}
}
Score a = new Score (5);
Score b = new Score (5);
Object c = a;
System.Console.WriteLine (((IComparable)c).CompareTo (a)); //0
System.Console.WriteLine ((object)a == (object)b; // false
System.Console.WriteLine (a == b); // true
(!= and ==, > and <, >= and <=), must both be defined.
|
|