简体   繁体   English

java generics - Comparable 类型中的方法 compareTo(capture#1-of?)<capture#1-of ?> 不适用于 arguments</capture#1-of>

[英]java generics - The method compareTo(capture#1-of ?) in the type Comparable<capture#1-of ?> is not applicable for the arguments

So I want to do this:所以我想这样做:

public interface IFieldObject {
    public Comparable get();
}

public interface IFieldCondition {
    public boolean apply(IFieldObject field, Comparable compare);
}

public class EqualTo implements IFieldCondition {
    public boolean apply(IFieldObject field, Comparable compare) {
        return (field.get().compareTo(compare) == 0);       
    }    
}

but Eclipse gives me warnings:但是 Eclipse 给了我警告:

Type safety: The method compareTo(Object) belongs to the raw type Comparable.类型安全:方法 compareTo(Object) 属于原始类型 Comparable。 References to generic type Comparable should be parameterized对泛型 Comparable 的引用应该被参数化

So I turned this into:所以我把它变成了:

public interface IFieldObject {
    public Comparable<?> get();
}

public interface IFieldCondition {
    public boolean apply(IFieldObject field, Comparable<?> compare);
}

public class EqualTo implements IFieldCondition {
    public boolean apply(IFieldObject field, Comparable<?> compare) {
        return (field.get().compareTo(compare) == 0);       
    }
}

which does not compile because of:由于以下原因无法编译:

The method compareTo(capture#1-of?) in the type Comparable is not applicable for the arguments (Comparable) Comparable 类型中的 compareTo(capture#1-of?) 方法不适用于 arguments (Comparable)

What's the right way to do that?这样做的正确方法是什么? (without warnings following idiomatic Java >= 1.6) (在惯用的 Java >= 1.6 之后没有警告)

Currently you've no guarantee that the type returned by field.get() is really comparable with the type specified by the method.目前,您无法保证field.get()返回的类型与方法指定的类型真正可比。 Ideally, make the whole thing generic, eg:理想情况下,使整个事情通用,例如:

public interface IFieldObject<T extends Comparable<T>> {
    public T get();
}

public interface IFieldCondition<T> {
    public boolean apply(IFieldObject<T> field, Comparable<T> compare);
}

public class EqualTo<T> implements IFieldCondition<T> {
    public boolean apply(IFieldObject<T> field, Comparable<T> compare) {
        return (field.get().compareTo(compare) == 0);       
    }
}

You could no doubt make this more general using extra captures, but that's the starting point.毫无疑问,您可以使用额外的捕获使其更通用,但这是起点。

How about this?这个怎么样?

public interface IFieldObject {
    public<T> Comparable<T> get();
}

public interface IFieldCondition {
    public boolean apply(IFieldObject field, Comparable<?> compare);
}

public class EqualTo implements IFieldCondition {
    public boolean apply(IFieldObject field, Comparable<?> compare) {
        return (field.get().compareTo(compare) == 0);       
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Generics:compareTo和“capture#1-of?” - Java Generics: compareTo and “capture#1-of ?” Java泛型编译错误-方法method(Class <capture#1-of ? extends Interface> ) <type> 不适用于参数 - Java generics compilation error - The method method(Class<capture#1-of ? extends Interface>) in the type <type> is not applicable for the arguments Java泛型:Comparable类型的compareTo(capture#2-of?扩展E)方法不适用于自变量(Comparable) - Java Generics :The method compareTo(capture#2-of ? extends E) in the type Comparable is not applicable for the arguments (Comparable) 类型Map <String中的方法,捕获#1-of? extends Object>不适用 - Method in the type Map<String,capture#1-of ? extends Object> is not applicable Java泛型编译错误“类 - Java generics compile error “Class<capture#1-of ? …” Java泛型-类型T中的方法validate(T,T)不适用于自变量(可比较,可比较 <capture#11-of ?> ) - java generics - The method evaluate(T,T) in the type T is not applicable for the arguments (Comparable, Comparable<capture#11-of ?>) 方法映射((<no type> cfg) -&gt; {}) 对于 List 类型是未定义的<capture#1-of ? extends Config> - The method map((<no type> cfg) -> {}) is undefined for the type List<capture#1-of ? extends Config> 类型不匹配:无法从Class转换 <capture#1-of ?> 上课 <?> [] - Type mismatch: cannot convert from Class<capture#1-of ?> to Class<?>[] 泛型:无法从 <capture#1-of ? extends Object,D> 至 <S,D> - Generics: cannot convert from <capture#1-of ? extends Object,D> to <S,D> Java泛型:compareTo和“ capture#-of?” - Java Generics: compareTo and “capture#-of ?”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM