简体   繁体   English

静态的 <T extends Number & Comparable<? super Number> &gt;

[英]static <T extends Number & Comparable<? super Number>>

I have following class with one static method: 我有一个静态方法跟随类:

public class Helper {

    public static <T extends Number & Comparable<? super Number>> Boolean inRange(T value, T minRange, T maxRange) {
        //  equivalent (value >= minRange && value <= maxRange)
        if (value.compareTo(minRange) >= 0 && value.compareTo(maxRange) <= 0)
            return true;
        else
            return false;
    }

}

I try to call this method: 我试着调用这个方法:

Integer value = 2;
Integer min = 3;
Integer max = 8;
Helper.inRange(value, min, max) ;

Netbeans compiler show me this error message: Netbeans编译器显示此错误消息:

method inRange in class Helper cannot be applied to given types; 类Helper中的方法inRange不能应用于给定的类型; required: T,T,T found: java.lang.Integer,java.lang.Integer,java.lang.Integer reason: inferred type does not conform to declared bound(s) inferred: java.lang.Integer bound(s): java.lang.Number,java.lang.Comparable required:T,T,T found:java.lang.Integer,java.lang.Integer,java.lang.Integer reason:推断类型不符合推断的声明边界:java.lang.Integer bound(s) :java.lang.Number,java.lang.Comparable

Any ideas? 有任何想法吗?

thanks. 谢谢。

Try <T extends Number & Comparable<T>> . 尝试<T extends Number & Comparable<T>>

Eg Integer implements Comparable<Integer> , which is not compatible with Comparable<? super Number> 例如, Integer实现了Comparable<Integer> ,它与Comparable<? super Number>不兼容Comparable<? super Number> Comparable<? super Number> (Integer is not a superclass of Number). Comparable<? super Number> (整数不是Number的超类)。 Comparable<? extends Number> Comparable<? extends Number> would not work either because Java would then think the ? Comparable<? extends Number>也不会工作,因为Java会考虑? could be any subclass of Number , and passing a T to compareTo would then not compile because it expects a parameter of ? 可以是Number任何子类,并且将T传递给compareTo则不会编译,因为它期望参数为? , not T . ,不是T

Edit: as newacct said, <T extends Number & Comparable<? super T>> 编辑:正如newacct所说, <T extends Number & Comparable<? super T>> <T extends Number & Comparable<? super T>> will work too (and be slightly more general) since then compareTo will then accept any ? <T extends Number & Comparable<? super T>>也会起作用(稍微更一般),因为那时compareTo会接受? of which T is a subclass, and as usual, an instance of a subclass can be given as a parameter where a superclass is expected. 其中T是子类,并且像往常一样,子类的实例可以作为期望超类的参数给出。

暂无
暂无

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

相关问题 泛型之间的区别 <T extends Number & Comparable<T> &gt;和T扩展可比 <? extends Number> - Difference between generics <T extends Number & Comparable<T>> and T extends Comparable<? extends Number> 问题 <T extends Comparable<? super T> &gt; - Issue with <T extends Comparable<? super T>> 有问题<t extends comparable<? super t> ></t> - Problem with <T extends Comparable<? super T>> 怎么会 <T extends Comparable<T> &gt;作为 <T extends Comparable<? super T> &gt;? - How come <T extends Comparable<T>> working as <T extends Comparable<? super T>>? 通用:扩展了数量和可比性的基本知识 - generic: extends Number & Comparable Basic understanding 如何返回一个通用数组<T extends Comparable<? super T> &gt; 在 Java 中? - How return a generic array of <T extends Comparable<? super T>> in Java? 为什么我的通用实现不起作用? (扩展了可比性 <? super T> &gt;) - Why is my generic implementation not working? (extends Comparable<? super T>>) 为什么T扩展了Comparable <? super T> 包括T? 含义包括可比较 <T> ? - Why does T extends Comparable <? super T> include T? Meaning includes Comparable<T>? 泛型的解释<T extends Comparable <? superT >>在collection.sort /可比代码? - Explanation of generic <T extends Comparable<? super T>> in collection.sort/ comparable code? Java泛型-为什么不允许我做可比 <? extends T> 而不是可比 <? super T> ? - Generics in Java - Why I am not allowed to do Comparable<? extends T> instead of Comparable<? super T>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM