Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java接口扩展了_Java_Generics_Inheritance_Interface - Fatal编程技术网

Java接口扩展了

Java接口扩展了,java,generics,inheritance,interface,Java,Generics,Inheritance,Interface,我希望有一个由TA参数化的接口A,并且希望实现它的每个类也实现可比较的(带有T及其子类型)。当compariable时,编写接口A extensed compariable似乎很自然,如果使用compariable,则不需要在compare函数中指定子类型的可能性,本质上,可以将对象X的任何子类型传递给声明类X参数的方法。有关详细信息,请参阅下面的代码 public interface Test<T> extends Comparable<T> { } class T

我希望有一个由
T
A
参数化的接口
A
,并且希望实现它的每个类也实现
可比较的
(带有
T
及其子类型)。当
compariable时,编写
接口A extensed compariable似乎很自然,如果使用compariable,则不需要在compare函数中指定子类型的可能性,本质上,可以将对象X的任何子类型传递给声明类X参数的方法。有关详细信息,请参阅下面的代码

public interface Test<T> extends Comparable<T> {

}

class TestImpl implements Test<Number> {
    @Override
    public int compareTo(final Number other) {
        return other.intValue() - 128;
    }
}

class TestMain {
    public static void main(final String[] args) {
        TestImpl testImpl = new TestImpl();
        testImpl.compareTo(Integer.MIN_VALUE);
    }
}
公共接口测试{
}
类TestImpl实现测试{
@凌驾
公共整数比较(最终数字其他){
返回other.intValue()-128;
}
}
类TestMain{
公共静态void main(最终字符串[]args){
TestImpl TestImpl=新的TestImpl();
testImpl.compareTo(整数最小值);
}
}

我还想让您参考Java泛型FAQ(),它将为您提供有关
Compariable
的更多想法。这是一个很棒的答案!我是从北京来的。解决了我的问题。
interface A<T extends Comparable<T>> extends Comparable<A<T>> {
    // ...
}
public interface Test<T> extends Comparable<T> {

}

class TestImpl implements Test<Number> {
    @Override
    public int compareTo(final Number other) {
        return other.intValue() - 128;
    }
}

class TestMain {
    public static void main(final String[] args) {
        TestImpl testImpl = new TestImpl();
        testImpl.compareTo(Integer.MIN_VALUE);
    }
}