定义一个实现可比较的接口
我试图定义一个接口,以便所有实现类都必须与它们自己及其子类进行比较。
例如,考虑:
public interface Rating implements Comparable<Rating> {}
这意味着实现类必须与所有评级相比较:
public class A implements Rating {
public int compareTo(Rating r) {return 0;}
}
我想放宽该要求,以便我可以定义一个类,例如:
public class A implements Rating {
public int compareTo(A a) {return 0;}
}
I'm trying to define an interface such that all implementing classes must be Comparable to themselves and their subclasses.
For example, consider:
public interface Rating implements Comparable<Rating> {}
This means that implementing classes must be Comparable to all Ratings:
public class A implements Rating {
public int compareTo(Rating r) {return 0;}
}
I'd like to loosen that requirement so that I can define a class like:
public class A implements Rating {
public int compareTo(A a) {return 0;}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我希望您尝试得到这样的东西:
或者也许(虽然我不确定您能做到这一点):
请参阅 Oracle 的此页面 了解有关有界通配符的更多信息。
I expect you're trying to get at something like this:
or maybe (although I'm not sure you can do this):
See this page at Oracle for more information on bounded wildcards.
我想你可能希望你的“compareTo()”返回除“0”之外的东西;)
您可以使用“instanceof”来查看任意对象是否是某个特定类(或子类)
I不确定是否超载“compareTo()”必然是个好主意。您甚至可能希望将输入参数设置为“Object o”。
恕我直言..pSM
I imagine you probably want your "compareTo()" to return something besides "0" ;)
You can use "instanceof" to see if an arbitrary object is some particular class (or a subclass)
I'm not sure overloading "compareTo()" is necessarily a good idea. You might even want to make the input argument "Object o".
IMHO .. pSM