由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 泛型问题
相关主题
这几个函数可以用Generic之类的东西合并么?Comparator Accessor method for SortedSet
帮忙看看???这里应该填什么Core Java2 Notes (4)
comparable interface in generic containerJava的method不都是virtual的么?private就不同了?
@Override annotation.Re: How to use abstract class?
TreeMap, TreeSet原来用起来这么爽问题征解
Skyline[合集] Java interview question, Thanks.
问两个语法问题Re: 初级问题
error: generic array creationRe: print problem, GUI guru please come in
相关话题的讨论汇总
话题: comparable话题: implements话题: compareto话题: override话题: extends
进入Java版参与讨论
1 (共1页)
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
4
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

【在 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:

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
6
Just use two variable to reference the same instance of B:
B b = new B()...;
A a = b;
Then use b to invoke B specific methods and a to compare.

【在 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. :(

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
9
well, what you said is alright, which is showed in the compile error.
but I think what LZ needs is just for A to use Comparable, and for B to
use Comparable. it happens to be not possible if B extends A. so
Comparator is a way to get around.

【在 m******t 的大作中提到】

:
: 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

1 (共1页)
进入Java版参与讨论
相关主题
Re: print problem, GUI guru please come inTreeMap, TreeSet原来用起来这么爽
Re: 如何在两个窗口之间通信?Skyline
请问有没有generic的array问两个语法问题
Is it possible to get Class object for T from a generic class? (下列空档,是否可填)error: generic array creation
这几个函数可以用Generic之类的东西合并么?Comparator Accessor method for SortedSet
帮忙看看???这里应该填什么Core Java2 Notes (4)
comparable interface in generic containerJava的method不都是virtual的么?private就不同了?
@Override annotation.Re: How to use abstract class?
相关话题的讨论汇总
话题: comparable话题: implements话题: compareto话题: override话题: extends