”“x2B” Java 类的运算符

发布于 2024-11-05 01:06:33 字数 262 浏览 1 评论 0原文

我有一个这样的类:

private static class Num {
    private int val;

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

是否可以使用“+”运算符添加到该类的对象?

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

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鲜肉鲜肉永远不皱 2024-11-12 01:06:33

不,你不能。 + 仅对数字、字符和 String 进行重载,并且不允许您定义任何其他重载。

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

这是一个例子:

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, you can't. + is overloaded only for numbers, chars and String, and you are not allowed to define any additional overloadings.

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.

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
美人如玉 2024-11-12 01:06:33

不,因为詹姆斯·高斯林这样说:

我没有将运算符重载作为一个相当个人的选择,因为我看到太多人在 C++ 中滥用它。

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

参考: 为什么 Java 不提供运算符重载?

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++.

Source: http://www.gotw.ca/publications/c_family_interview.htm

Reference: Why doesn't Java offer operator overloading?

月下伊人醉 2024-11-12 01:06:33

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

No. Java does not support operator overloading (for user-defined classes).

披肩女神 2024-11-12 01:06:33

java中没有运算符重载。
对象唯一支持的是通过“+”进行字符串连接。如果您有一系列通过“+”连接的对象,并且其中至少一个是字符串,那么结果将内联到字符串创建。示例:

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

String str = "Test" + a + b;

将内联到

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

There is no operators overloading in 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. 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();
有深☉意 2024-11-12 01:06:33

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

No, it is not possible, as Java doesn't support operator overloading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文