Java: Casting to a type parameter

前端 未结 2 1744
暖寄归人
暖寄归人 2021-01-29 12:27

I have the following two classes:

public class GenericNumberOperation {
    public GenericNumberOperation() {} 
    public  T getSomeValu         


        
2条回答
  •  [愿得一人]
    2021-01-29 13:25

    As @Sotirios Delimanolis wrote, you cannot even run that code.

    Try this one:

    @SuppressWarnings("unchecked")
    public  T getSomeValue(boolean tf) {
        T number;
        if (tf) {
            number = (T) new Double(1.0);
        } else {
            number = (T) new Integer(11);
        }
        return number;
    }
    

提交回复
热议问题