A**o 发帖数: 1550 | | s***e 发帖数: 122 | 2 why don't you just use the A's compareTo() method? it seems to be your
intention to compare them as A, no matter whether subtype they are.
or if you do care their type, you might override the compareTo() method like:
if (!(a instanceof B)) throws new SomeException();
return super.compareTo(a);
but i do suspect it's not what you want it to do.
【在 A**o 的大作中提到】 : if A implements Comparable : and B extends A : then B can't implements Comparable : so, what should i do to make it work? : currently, i have to override in B using: : compareTo(A a) then do type checking and all the same shit. :(
| A**o 发帖数: 1550 | 3 It was what i want. And I'm doing exactly that.
type checking, casting or throw exception, compare instance of B...
isn't that back to the days before generic?
what's the point of generic, then?
like:
【在 s***e 的大作中提到】 : why don't you just use the A's compareTo() method? it seems to be your : intention to compare them as A, no matter whether subtype they are. : or if you do care their type, you might override the compareTo() method like: : if (!(a instanceof B)) throws new SomeException(); : return super.compareTo(a); : but i do suspect it's not what you want it to do.
| s***e 发帖数: 122 | | s***e 发帖数: 122 | 5 or you can consider Comparator anyway:
class A {
class MyComparator implements Comparator {
@Override
public int compare(A o1, A o2) {
// ...
return 0;
}
}
}
【在 A**o 的大作中提到】 : It was what i want. And I'm doing exactly that. : type checking, casting or throw exception, compare instance of B... : isn't that back to the days before generic? : what's the point of generic, then? : : like:
| F****n 发帖数: 3271 | | F****n 发帖数: 3271 | 7 If A is supposed to be extended, it should be declared like
public class A implements Comparable {
public int compareTo(T t) {
...
}
}
Since it is very likely A will be compared with sub-classes of A.
【在 A**o 的大作中提到】 : It was what i want. And I'm doing exactly that. : type checking, casting or throw exception, compare instance of B... : isn't that back to the days before generic? : what's the point of generic, then? : : like:
| m******t 发帖数: 2416 | 8
B can override compareTo but still fulfill the contract,
as long as B.compareTo(A) works. That's actually the
essence of the problem.
The T in Comparable doesn't really have to be A. All
A implements Comparable
says is that A is comparable to T, and _only_ comparable to T.
So if A already implements Comaprable, so would B from the
inheritance, and it would violate the contract for B to
(re)implement Comparable, because that would mean that B is
no longer comparable to A.
【在 s***e 的大作中提到】 : generic is making methods more specific for a specific type. I think your : problem is not about generic. the problem comes out from the fact that B : extends A, no matter you use generic or not. : If B is not a subclass of A, then both A implements Comparable and B : implements Comparable make sense. : But if you make B a subclass of A, then B must fulfill the contract that A : provides, which is the method: int compareTo(A). I think you do want the : compareTo() method to be at the level of A, and not
|
| s***e 发帖数: 122 | |
|