• <tfoot id='yNQAs'></tfoot>

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

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

        <bdo id='yNQAs'></bdo><ul id='yNQAs'></ul>
    1. <legend id='yNQAs'><style id='yNQAs'><dir id='yNQAs'><q id='yNQAs'></q></dir></style></legend>

        “+"Java 类的运算符

        quot;+quot; operator for Java-classes(“+Java 类的运算符)

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

                  本文介绍了“+"Java 类的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有这样的课:

                  private static class Num {
                      private int val;
                  
                      public Num(int val) {
                          this.val = val;
                      }
                  }
                  

                  是否可以使用+"-操作符添加到类的对象?

                  Is it possible to add to objects of the class by using the "+"-operator?

                  Num a = new Num(18);
                  Num b = new Num(26);
                  Num c = a + b;
                  

                  推荐答案

                  不,你不能.+ 仅对数字、字符和 String 进行重载,并且不允许定义任何额外的重载.

                  No, you can't. + is overloaded only for numbers, chars and String, and you are not allowed to define any additional overloadings.

                  有一种特殊情况,当您可以连接任何对象的字符串表示时 - 如果前两个操作数中有 String 对象,则调用 toString()在所有其他对象上.

                  There is one special case, when you can concatenate any objects' string representation - if there is a String object in the first two operands, toString() is called on all other objects.

                  这是一个插图:

                  int i = 0;
                  String s = "s";
                  Object o = new Object();
                  Foo foo = new Foo();
                  
                  int r = i + i; // allowed
                  char c = 'c' + 'c'; // allowed
                  String s2 = s + s; // allowed
                  Object o2 = o + o; // NOT allowed
                  Foo foo = foo + foo; // NOT allowed
                  String s3 = s + o; // allowed, invokes o.toString() and uses StringBuilder
                  String s4 = s + o + foo; // allowed
                  String s5 = o + foo; // NOT allowed - there's no string operand
                  

                  这篇关于“+"Java 类的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 表达式)

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

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

                          <tfoot id='YxlQx'></tfoot>