简体   繁体   English

为什么通过在泛型中将Integer类型参数设置为Integer变量而出错?

[英]Why i am getting error by setting the Integer type parameter to the Integer variable in generics?

I have the following program: 我有以下程序:

class MyGenClass{

    public <T> void setAge(T ageParam){
        Integer age = ageParam;
    }

}
class Program{

    public static void main(String args[]){

        MyGenClass gnClass = new MyGenClass();
        gnClass.<Integer>setAge(80);

    }

}

In fact, i am passing the Integer then why the ageParam is not assigned to age . 实际上,我通过了Integer然后为什么不将ageParam分配给age And when i do: 当我这样做时:

class MyGenClass{

    public <T> void setAge(T ageParam){
        T age = ageParam;
    }

}

Why the generic type variable is not assigned to the Integer type variable age in fact the generic type variable ageParam is Integer . 为什么泛型类型变量没有分配给Integer类型变量age实际上,泛型类型变量ageParamInteger Is this compulsory that the ageParam must be assigned to the variable that is of type T ? 是否必须将ageParam分配给类型T的变量? Whats the scenario behind this? 这背后的情况是什么?

There is not assured that the type T will be compatible with Integer . 无法保证类型T将与Integer兼容。 To make it clear, you have to use the following approach where T would be a subtype of Integer : 为了清楚起见,您必须使用以下方法,其中T将是Integer的子类型:

public <T extends Integer> void setAge(T ageParam){
    age = ageParam;
}

However, I see no point on this. 但是,我对此没有任何意义。 Consider the following approach for the sake of variability: 考虑到可变性,请考虑以下方法:

class MyGenClass {
    Number age;
    public <T extends Number> void setAge(T ageParam){
        age = ageParam;
    }
}

Therefore the following is possible (the explicit type arguments can be inferred, thought): 因此,以下是可能的(可以推断出显式类型参数):

MyGenClass gnClass = new MyGenClass();
gnClass.<Integer>setAge(80);
gnClass.<Long>setAge(80L);
gnClass.<Double>setAge(80.0);
gnClass.<Float>setAge(80.0F);

Look at your MyGenClass in isolation. 孤立地查看您的MyGenClass T could be literally anything. T实际上可以是任何东西。 It is not necessarily an Integer. 它不一定是整数。 I could call it with a String, or a HashMap or an ArrayList, or literally anything else . 我可以用String,HashMap或ArrayList或其他任何名称来调用它。

MyGenClass gnClass = new MyGenClass();
gnClass.setAge("hello");
gnClass.setAge(new HashMap<String, String>());
gnClass.setAge(new ArrayList<String>());

In all of these cases, assignment to an Integer variable is invalid, hence the compiler error. 在所有这些情况下,分配给Integer变量都是无效的,因此会导致编译器错误。

You only happen to be calling it with an Integer in your example. 在您的示例中,您恰好使用整数来调用它。 The compiler cannot assert that it will always be this way. 编译器不能断言它将一直是这种方式。

It looks as if you should not be using generics at all. 似乎您根本不​​应该使用泛型。 Just change the signature to 只需将签名更改为

public void setAge(Integer ageParam)

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

相关问题 为什么我可以将整数文字分配给短类型变量而不是短类型方法参数? - Why can I assign an integer literal to a short type variable but not to a short type method parameter? 为什么我会收到“整数太大”错误? 我在最后加上一个“L” - Why am I getting the error "integer number too large"? I'm including an "L" at the end 为什么我在ArrayList中而不是整数值中得到此结果? - Why am I getting this results in my ArrayList instead of the integer values? 为什么在将字符串转换为整数时会出现NumberFormatException? - Why am I getting NumberFormatException while converting a string to integer? 为什么在将数字转换为整数的前面得到“ 0”? - Why I am getting “0” in front part of transforming digits into integer? 为什么我在使用最大 integer 值时得到荒谬的值? - Why I am getting absurd values on using maximum integer value? 为什么我会收到意外的类型、需要变量的错误? 即使我使用的是变量 - Why am I getting a unexpected type, variable required error? even though I am using a variable 我在Java 8代码列表中将Integer值获取为&#39;Integer&#39; - I am getting Integer value as 'Integer' in the list in my Java 8 code 我得到了意外的整数值 - I am getting an unexpected integer value 为什么会出现类型不匹配错误? - Why am I getting Type mismatch error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM