[英]How to use Vector and Comparable Interface
I did 我做了
public class Employee implements Comparable <Object> {
public int compareTo (Object other ){
i did the implementation well (it works fine) and calles it from other class: 我做了很好的实现(它工作正常),并从其他类中调用它:
import java.util.Vector;
public class Firm {
private Vector <Employee> employees = new Vector<Employee>();
and using in the same class (Firm) Comperator Vector in function : 并在函数中使用同一类(固定)Comperator Vector:
public void SortVector(Vector <Comparable> vector) {
and calls it with: 并调用:
SortEmployeesBy(this.employees); ////Error is here
and in this line the compiler said: 在这一行中,编译器说:
The method SortVector(Vector) in the type Firm is not applicable for the arguments (Vector)" 公司类型中的方法SortVector(Vector)不适用于参数(Vector)“
Note: i required to use only vector and need to use SortVector several times so that i need to use the comparator because i have many vector and other classes like Employee, customer, Artist... tha each has own implementation for Comparable 注意:我只需要使用vector,并且需要多次使用SortVector,所以我需要使用比较器,因为我有很多vector以及其他类,例如Employee,customer,Artist等。每个类都有自己的Comparable实现
why is that, and how to fix it? 为什么会这样,以及如何解决?
Try any one 尝试任何一个
public void SortEmployeesBy(Vector<? extends Comparable<Object>> vector) {..}
public void SortEmployeesBy(Vector<Employee> vector) {...}
instead of using 而不是使用
public void SortEmployeesBy(Vector<Comparable> vector) {...}
--EDIT-- - 编辑 -
You can try with Firm
class making it parametrized as shown below to make it more generic . 您可以尝试使用Firm
类使其参数化,如下所示, 以使其更通用 。
class Firm<T extends Comparable<Object>> {
private Vector<T> employees = new Vector<T>();
public void SortEmployeesBy(Vector<T> vector) {...}
}
Firm<Employee> f = new Firm<Employee>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.