Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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:泛型方法和有界类型参数]_Java_Generic Method_Bounded Types - Fatal编程技术网

为什么我的Java代码不能编译?[Java:泛型方法和有界类型参数]

为什么我的Java代码不能编译?[Java:泛型方法和有界类型参数],java,generic-method,bounded-types,Java,Generic Method,Bounded Types,下面是我的Java代码,它无法编译。我想不出失败的原因: interface Comparable<T> { public int compareTo(T o); } class MyClass { public static <T extends Comparable<T>> int method1(T t1, T t2) { return t1.compareTo(t2); } } class Tes

下面是我的Java代码,它无法编译。我想不出失败的原因:

interface Comparable<T>
{
    public int compareTo(T o);
}


class MyClass {
    public static  <T extends Comparable<T>> int method1(T t1, T t2)
    {
        return t1.compareTo(t2);
    }
}

class TestApp1 {
    public static void main(String[] args) {
        Integer p1 =new Integer(8);
        Integer p2 =new Integer(9);

        int result = MyClass.method1(p1,p2);

        System.out.println("result = " + result);
    }
}
接口
{
公共国际比较(TO);
}
类MyClass{
公共静态int方法1(t1,t2)
{
返回t1.比较(t2);
}
}
类TestApp1{
公共静态void main(字符串[]args){
整数p1=新整数(8);
整数p2=新整数(9);
int result=MyClass.method1(p1,p2);
System.out.println(“结果=”+结果);
}
}
它不编译,错误为:

TestApp1.java:19: error: method method1 in class MyClass cannot be applied to given types;
        int result = MyClass.method1(p1,p2);
                            ^   required: T,T   found: Integer,Integer   reason: inferred type does not conform to upper bound(s)
    inferred: Integer
    upper bound(s): Comparable<Integer>   where T is a type-variable:
    T extends Comparable<T> declared in method <T>method1(T,T) 1 error
TestApp1.java:19:error:MyClass类中的method1方法无法应用于给定类型;
int result=MyClass.method1(p1,p2);
^必需:T,T找到:整数,整数原因:推断类型不符合上限
推断:整数
上限:可比较,其中T为类型变量:
T扩展了方法method1(T,T)1中声明的可比错误

之所以发生这种情况,是因为您的
method1
方法使用了自定义
Comparable
接口,其中Integer使用
java.lang.Comparable
,所以
method1
将引发异常

仅使用以下代码:

class MyClass {
    public static  <T extends Comparable<T>> int method1(T t1, T t2)
    {
        return t1.compareTo(t2);
    }
}

class TestApp1 {
    public static void main(String[] args) {
        Integer p1 =new Integer(8);
        Integer p2 =new Integer(9);

        int result = MyClass.method1(p1,p2);

        System.out.println("result = " + result);
    }
}
class-MyClass{
公共静态int方法1(t1,t2)
{
返回t1.比较(t2);
}
}
类测试PP1{
公共静态void main(字符串[]args){
整数p1=新整数(8);
整数p2=新整数(9);
int result=MyClass.method1(p1,p2);
System.out.println(“结果=”+结果);
}
}

之所以发生这种情况,是因为您的
method1
方法使用了自定义
Comparable
接口,其中Integer使用
java.lang.Comparable
,所以
method1
将引发异常

仅使用以下代码:

class MyClass {
    public static  <T extends Comparable<T>> int method1(T t1, T t2)
    {
        return t1.compareTo(t2);
    }
}

class TestApp1 {
    public static void main(String[] args) {
        Integer p1 =new Integer(8);
        Integer p2 =new Integer(9);

        int result = MyClass.method1(p1,p2);

        System.out.println("result = " + result);
    }
}
class-MyClass{
公共静态int方法1(t1,t2)
{
返回t1.比较(t2);
}
}
类测试PP1{
公共静态void main(字符串[]args){
整数p1=新整数(8);
整数p2=新整数(9);
int result=MyClass.method1(p1,p2);
System.out.println(“结果=”+结果);
}
}

您确实创建了一个自定义的
Comperable
接口,由于
Integer
类没有实现您自己的自定义接口(因此它没有完全满足您使用
t Extendes Comperable创建的条件)
它抛出了这个编译错误。这是我针对这个问题的全部源代码。Integer具有compareTo()方法。即使我删除了“interface Compariable”,编译错误仍然是一样的。我在阅读时遇到了这个问题,您确实创建了一个自定义的
Comperable
接口,而且由于
Integer
类没有实现您自己的自定义接口(因此它没有完全满足您在
t extensed Comperable中创建的条件)
它抛出了这个编译错误。这是我回答这个问题的全部源代码。Integer具有compareTo()方法。即使我删除了“interface Compariable”,编译错误仍然是一样的。我在阅读您的建议时遇到了这个问题,我删除了自己的界面,但错误仍然是一样的。我在阅读@ZanderHsu时遇到了这个问题,现在它不应该出现了,只需使用
java.lang.Comparable
代替
Comparable
,看看你是否仍然会遇到同样的错误。你的答案中有一个拼写错误,是
Comparable
,而不是
Comparable
。不过仍然是一个有效的答案:)@GPI sry用于拼写错误,已修复。是。你是对的。它在添加“import java.lang.Comparable;”后通过。根据您的建议,我删除了自己的接口,但错误仍然是一样的。我在阅读@ZanderHsu时遇到了这个问题,现在它不应该出现了,只需使用
java.lang.Comparable
代替
Comparable
,看看你是否仍然会遇到同样的错误。你的答案中有一个拼写错误,是
Comparable
,而不是
Comparable
。不过仍然是一个有效的答案:)@GPI sry用于拼写错误,已修复。是。你是对的。它在添加“import java.lang.Compariable;”后通过。