<tfoot id='rgCqX'></tfoot>
        <bdo id='rgCqX'></bdo><ul id='rgCqX'></ul>
      <legend id='rgCqX'><style id='rgCqX'><dir id='rgCqX'><q id='rgCqX'></q></dir></style></legend>
    1. <i id='rgCqX'><tr id='rgCqX'><dt id='rgCqX'><q id='rgCqX'><span id='rgCqX'><b id='rgCqX'><form id='rgCqX'><ins id='rgCqX'></ins><ul id='rgCqX'></ul><sub id='rgCqX'></sub></form><legend id='rgCqX'></legend><bdo id='rgCqX'><pre id='rgCqX'><center id='rgCqX'></center></pre></bdo></b><th id='rgCqX'></th></span></q></dt></tr></i><div id='rgCqX'><tfoot id='rgCqX'></tfoot><dl id='rgCqX'><fieldset id='rgCqX'></fieldset></dl></div>

    2. <small id='rgCqX'></small><noframes id='rgCqX'>

      1. java.lang.Number 没有实现“+";或任何其他运营商?

        java.lang.Number doesn#39;t implement quot;+quot; or any other operators?(java.lang.Number 没有实现“+;或任何其他运营商?)
          <tbody id='FBfbe'></tbody>
        <tfoot id='FBfbe'></tfoot>

            • <bdo id='FBfbe'></bdo><ul id='FBfbe'></ul>
              <i id='FBfbe'><tr id='FBfbe'><dt id='FBfbe'><q id='FBfbe'><span id='FBfbe'><b id='FBfbe'><form id='FBfbe'><ins id='FBfbe'></ins><ul id='FBfbe'></ul><sub id='FBfbe'></sub></form><legend id='FBfbe'></legend><bdo id='FBfbe'><pre id='FBfbe'><center id='FBfbe'></center></pre></bdo></b><th id='FBfbe'></th></span></q></dt></tr></i><div id='FBfbe'><tfoot id='FBfbe'></tfoot><dl id='FBfbe'><fieldset id='FBfbe'></fieldset></dl></div>

                  <small id='FBfbe'></small><noframes id='FBfbe'>

                1. <legend id='FBfbe'><style id='FBfbe'><dir id='FBfbe'><q id='FBfbe'></q></dir></style></legend>
                  本文介绍了java.lang.Number 没有实现“+";或任何其他运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在创建一个类,它应该能够与任何类型的数字(float、int 等)的数组一起使用,所以这是我拥有的一种方法:

                  I'm creating a class which is supposed to be able to be used with an array of any type of number (float, int, etc), so here is one method I have:

                  // T extends Number
                  public synchronized T[] average() {
                      Number[] ret = new Number[queue[0].length];
                      for (int i = 0; i < ret.length; ++i) {
                          for (int j = 0; j < size; ++j) {
                              ret[i] += queue[j][i]; // WTF ERROR?!
                          }
                          ret[i] /= size; // WTF ERROR?!
                      }
                      return (T[])ret;
                  }
                  

                  除了这不会编译,因为数字"没有实现+="或/="运算符.更糟糕的是,Java 的 Number 类甚至没有实现最基本的运算符,如+"或-"!如果java不让我编译它,我怎么能创建一个返回数字数组平均值的方法,因为它认为不能添加数字?!?!

                  Except this won't compile because "Number" doesn't implement the "+=" or "/=" operators. Event worse, java's Number class doesn't implement even the most basic operators like "+" or "-"! How can I make a method which returns the average of an array of Numbers if java won't let me compile it because it thinks that numbers can't be added?!?!

                  推荐答案

                  你误解了 Java 中数字的工作方式.Number 类是用于表示基本类型(intfloat 等)作为对象,但它确实与通常的算术运算符一起使用.

                  You're misunderstanding the way numbers work in Java. The Number class is the superclass of numeric wrapper classes (Integer, Float, etc.) useful for representing primitive types (int, float, etc.) as objects, but it does not work with the usual arithmetic operators.

                  如果您打算使用算术运算符,请使用原始类型.如果您需要构建适用于所有数字数据类型的通用"方法,您别无选择,只能构建同一方法的多个重载版本,每个数据类型一个,例如:

                  If you intend to use the arithmetic operators, then use primitive types. If you need to build a "generic" method that works for all numeric data types, you have no choice but to build several overloaded versions of the same method, one for each data type, for example:

                  public  float[] average(float[][]  queue) {...}
                  public double[] average(double[][] queue) {...}
                  

                  还要注意,像这样的代码似乎适用于包装器类型:

                  Also be aware that code like this appears to work for wrapper types:

                  Integer i = 0;
                  i += 1;
                  System.out.println(i);
                  

                  ...但在底层,Java 自动 对 Integer 进行装箱和拆箱,因为 += 运算符仅适用于原始类型.它之所以有效,是因为我们明确指出该数字是一个 Integer,但它不适用于 Number,因为 Java 需要准确地知道 什么 用于执行装箱/拆箱的数字类型.

                  ... But under the hood, Java is automatically boxing and unboxing the Integer, since the += operator only works for primitive types. It works because we're explicitly indicating that the number is an Integer, but it won't work for a Number, since Java needs to know exactly what type of number it's dealing with for performing the boxing/unboxing.

                  这篇关于java.lang.Number 没有实现“+";或任何其他运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                2. <legend id='tyfD6'><style id='tyfD6'><dir id='tyfD6'><q id='tyfD6'></q></dir></style></legend>

                3. <tfoot id='tyfD6'></tfoot>

                    <bdo id='tyfD6'></bdo><ul id='tyfD6'></ul>

                      • <i id='tyfD6'><tr id='tyfD6'><dt id='tyfD6'><q id='tyfD6'><span id='tyfD6'><b id='tyfD6'><form id='tyfD6'><ins id='tyfD6'></ins><ul id='tyfD6'></ul><sub id='tyfD6'></sub></form><legend id='tyfD6'></legend><bdo id='tyfD6'><pre id='tyfD6'><center id='tyfD6'></center></pre></bdo></b><th id='tyfD6'></th></span></q></dt></tr></i><div id='tyfD6'><tfoot id='tyfD6'></tfoot><dl id='tyfD6'><fieldset id='tyfD6'></fieldset></dl></div>

                              <tbody id='tyfD6'></tbody>

                            <small id='tyfD6'></small><noframes id='tyfD6'>