简体   繁体   English

Java类的“+”运算符

[英]“+” operator for Java-classes

I have a class like this: 我有一个这样的课:

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;

No, you can't. 不,你不能。 + is overloaded only for numbers, chars and String , and you are not allowed to define any additional overloadings. +仅为数字,字符和String重载,并且不允许定义任何其他重载。

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

Here's an illustration: 这是一个例子:

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

No, because James Gosling said so: 不,因为詹姆斯·高斯林这么说:

I left out operator overloading as a fairly personal choice because I had seen too many people abuse it in C++. 我遗漏了操作符重载作为一个相当个人的选择,因为我看到有太多人在C ++中滥用它。

Source: http://www.gotw.ca/publications/c_family_interview.htm 资料来源: http//www.gotw.ca/publications/c_family_interview.htm

Reference: Why doesn't Java offer operator overloading? 参考: 为什么Java不提供运算符重载?

不.Java不支持运算符重载(对于用户定义的类)。

There is no operators overloading in java. java中没有运算符重载。 The only thing which is supported for objects, is string concatenations via "+". 唯一支持对象的是通过“+”进行字符串连接。 If you have a sequences of objects joined via "+" and at least one of them is a String, then the result will be inlined to String creation. 如果您有一系列通过“+”连接的对象,并且其中至少有一个是String,则结果将内联到String创建。 Example: 例:

Integer a = 5;
Object b = new Object();

String str = "Test" + a + b;

will be inlined to 将被内联

String str = new StringBuilder("Test").append(a).append(b).toString();

不,这是不可能的,因为Java不支持运算符重载

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

相关问题 避免在继承的Java类中进行强制转换 - Avoid casting in inherited java-classes 使用Hibernate和Maven从数据库中生成Java类-缺少AnnotationConfiguration吗? - Generating Java-classes out of database with hibernate and maven - AnnotationConfiguration missing? 如何访问默认包中的 java 类? - How to access java-classes in the default-package? 是否有一种经过批准的方法可以根据 Java 类的功能对其进行分类? - Is there an approved way to classificate Java-Classes based on their functionalities? 使用 JSF Facelets 的国际化 (i18n):Java 类中的消息 - Internationalization (i18n) using JSF Facelets: Messages in Java-classes 尝试使用JPype调用Java类(Neo4j)时遇到很多错误(异常) - Get a lot of errors (Exceptions) while trying to call Java-classes (Neo4j) with JPype 我可以只用C / C ++创建Java类吗? - Can I create Java-classes from C/C++ only? java中的instanceof运算符用于比较不同的类 - instanceof operator in java for comparing different classes 为什么 Java 7 菱形运算符不能与匿名类一起使用? - Why can't Java 7 diamond operator be used with anonymous classes? 可序列化的类和 == 运算符 - Serializable classes and the == operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM