Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.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 - Fatal编程技术网

java泛型加法

java泛型加法,java,generics,Java,Generics,我正在尝试实现前面提到的add方法 类矩阵 { 专用T添加(T左,T右) { if(整数的左实例) { 返回新的整数(((整数)左).intValue()+((整数)右).intValue()); } } 使用的编译器错误在我返回新整数的行中找到java.lang.Integer Required T。我不确定我缺少什么,因为T扩展了Number,Integer是Number的一个子类。编译器不允许您这样做,因为T可能是其他类,例如Double 从instanceof检查中,您知道T是Inte

我正在尝试实现前面提到的add方法

类矩阵
{
专用T添加(T左,T右)
{
if(整数的左实例)
{
返回新的整数(((整数)左).intValue()+((整数)右).intValue());
}
}

使用
的编译器错误在我返回新整数的行中找到java.lang.Integer Required T
。我不确定我缺少什么,因为T扩展了Number,Integer是Number的一个子类。

编译器不允许您这样做,因为
T
可能是其他类,例如
Double

instanceof
检查中,您知道
T
Integer
,但编译器不知道。

“我不确定缺少什么,因为T扩展了Number,Integer是Number的子类。”

此声明是错误的。通常,如果您有:

 public class B extends A {
 }

 public class C extends A {
 }
这并不意味着B可以转换为C。所以写下如下内容:

 public <T extends A> T method(T arg) {
     return (B)arg;
 }
公共T方法(T参数){
返回(B)arg;
}

bb=(B)方法(C);
调用它显然是错误的。

Java的类型系统根本无法表达这一点。下面是一个解决方法

创建一个接口
Numeric
,该接口提供您感兴趣的数值操作,并为您感兴趣的数据类型编写其实现

interface Numeric<N> {
  public N add(N n1, N n2);
  public N subtract(N n1, N n2);
  // etc.
}

class IntNumeric extends Numeric<Integer> {
  public static final Numeric<Integer> INSTANCE = new IntNumeric();

  private IntNumeric() {
  }

  public Integer add(Integer a, Integer b) {
    return a + b;  
  }

  public Integer subtract(Integer a, Integer b) {
    return a - b;  
  }

  // etc.
}
希望对你有所帮助。

包装仿制药

public class Box<T> {

      public T j,k;
      int l;
      float f;

      @SuppressWarnings("unchecked")
    public void add(T j,T k) {
        this.j = j;
        this.k=k;

        if(j.toString().contains("."))
        {
              this.f=Float.parseFloat(j.toString())+Float.parseFloat(k.toString());


        } else{
        this.l=Integer.parseInt(j.toString())+Integer.parseInt(k.toString());
        }
      }

      public int getInt() {
        return l;
      }

      public float getFloat() {
            return f;
          }

      public static void main(String[] args) {
         Box<Integer> integerBox = new Box<Integer>();
         Box<Float> floatBox = new Box<Float>();

         integerBox.add(new Integer(10),new Integer(20));
         floatBox.add(new Float(2.2),new Float(3.3));

         System.out.printf("Integer Value :%d\n\n", integerBox.getInt());
         System.out.printf("float Value :%f\n", floatBox.getFloat());
      }
    }
公共类框{
公共T j,k;
int l;
浮动f;
@抑制警告(“未选中”)
公共无效添加(Tj,Tk){
这个。j=j;
这个。k=k;
如果(j.toString()包含(“.”)
{
this.f=Float.parseFloat(j.toString())+Float.parseFloat(k.toString());
}否则{
this.l=Integer.parseInt(j.toString())+Integer.parseInt(k.toString());
}
}
公共int getInt(){
返回l;
}
公共浮点getFloat(){
返回f;
}
公共静态void main(字符串[]args){
Box integerBox=新的Box();
Box floatBox=新的Box();
integerBox.add(新整数(10),新整数(20));
浮动框。添加(新浮动(2.2)、新浮动(3.3));
System.out.printf(“整数值:%d\n\n”,integerBox.getInt());
System.out.printf(“浮点值:%f\n”,floatBox.getFloat());
}
}

Cat
扩展了
Animal
Dog
Animal
的一个子类。这并不意味着你可以在需要
Cat
的地方返回
Dog
。@OliCharlesworth我尝试过转换,但似乎没有help@NuclearGhost:那是什么错误?@SLaks对不起,我应该说是警告。Matrix.java:43:警告:[未选中]未选中的强制转换找到:java.lang.Integer必需:T返回(T)(新整数(((整数)左)。intValue()+((整数)右)。intValue());编译器警告您不能在运行时检查该强制转换。没有办法避免该警告;您应该抑制该警告。啊,是的,这是正确的。仅仅因为它们扩展了相同的东西,就不会使它们相同。您不应该在没有任何解释的情况下发布代码,它可以做什么以及如何解决问题。顺便说一下,你也不应该在你的代码中添加
@SuppressWarnings(“unchecked”)
,因为你是故意这样做的。还有一件事:你的代码很容易发生异常。
class Matrix<N> {
  private final Numeric<N> num;
  private final List<List<N>> contents;

  public Matrix(Numeric<N> num) {
    this.num = num;
    this.contents = /* Initialization code */;
  }

  public Matrix<N> add(Matrix<N> that) {
    Matrix<N> out = new Matrix<N>(num);
    for( ... ) {
      for( ... ) {
        out.contents.get(i).set(j,
          num.add(
            this.contents.get(i).get(j),
            that.contents.get(i).get(j),
          )
        );
      }
    }
    return out;
  }
}

// Use site
Matrix<Integer> m = new Matrix<Integer>(IntNumeric.INSTANCE);
public class Box<T> {

      public T j,k;
      int l;
      float f;

      @SuppressWarnings("unchecked")
    public void add(T j,T k) {
        this.j = j;
        this.k=k;

        if(j.toString().contains("."))
        {
              this.f=Float.parseFloat(j.toString())+Float.parseFloat(k.toString());


        } else{
        this.l=Integer.parseInt(j.toString())+Integer.parseInt(k.toString());
        }
      }

      public int getInt() {
        return l;
      }

      public float getFloat() {
            return f;
          }

      public static void main(String[] args) {
         Box<Integer> integerBox = new Box<Integer>();
         Box<Float> floatBox = new Box<Float>();

         integerBox.add(new Integer(10),new Integer(20));
         floatBox.add(new Float(2.2),new Float(3.3));

         System.out.printf("Integer Value :%d\n\n", integerBox.getInt());
         System.out.printf("float Value :%f\n", floatBox.getFloat());
      }
    }