Java:添加两个对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23178398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: add two objects
提问by Pavel
I was making a project in Greenfootand I made an imaginary number class. In my project I found a need to add (or subtract, or whatever) two imaginary objects together, is there a way to add two objects like that? So this is how it would look in a perfect world:
我在Greenfoot做一个项目,我做了一个虚数课。在我的项目中,我发现需要将两个虚构的对象相加(或相减,或其他什么),有没有办法像这样添加两个对象?所以这就是它在完美世界中的样子:
Imaginary i1 = new Imaginary(1.7,3.14);
Imaginary i2 = new Imaginary(5.3,9.1);
//the class Imaginary has parameters (double real, double imaginary)
Imaginary i3 = i1+i2;
Is that possible?
那可能吗?
采纳答案by loknath
Java has no operator overloading.
Java没有运算符重载。
For example, BigDecimal
would be a lot more popular if you could write a + b
instead of a.add(b)
.
例如,BigDecimal
如果您可以编写a + b
而不是a.add(b)
.
Way 1.
方式一。
Imaginary i3 = i1.add(i2);
Method:
方法:
public static Imaginary add(Imaginary i2)
{
return new Imaginary(real + i2.real, imaginary + i2.imaginary);
}
Way 2.
方式二。
Imaginary i3 = add(i1, i2)
Method:
方法:
public static Imaginary add(Imaginary i1, Imaginary i2)
{
return new Imaginary(i1.real + i2.real, i1.imaginary + i2.imaginary);
}
Operator overloading would have definitely made design more complex than without it, and it might have led to more complex compiler or slows the JVM.
运算符重载肯定会使设计比没有它更复杂,并且它可能导致更复杂的编译器或减慢 JVM。
回答by CoderCroc
Try Like this.
试试这样。
Imaginary i3 =new Imaginary(i1.real+i2.real,i1.imaginary+i2.imaginary);
If you want to add Object
you can create method for addition.
如果要添加Object
,可以创建添加方法。
public static Imaginary add(Imaginary i1,Imaginary i2)
{
return new Imaginary(i1.real+i2.real,i1.imaginary+i2.imaginary);
}
And create Object from this
并由此创建对象
Imaginary i3 =add(i1,i2);
回答by ApproachingDarknessFish
What you are describing is called "Operator overloading" and it cannot be accomplished in Java (at least by programmers such as you and me; the developers have free reign to do this and did so with the String class). Instead, you can create an add
method and call that:
您所描述的称为“运算符重载”,它无法在 Java 中完成(至少对于像您和我这样的程序员而言;开发人员可以自由地执行此操作并使用 String 类这样做)。相反,您可以创建一个add
方法并调用它:
Imaginary i3 = i1.add(i2);
回答by Ved
It is possible in C++, but not in Java, define a function addImaginary(Imaginary, Imaginary)
which will add two Imaginarys and store them in the object will called the method.
在 C++ 中是可能的,但在 Java 中不能,定义一个函数addImaginary(Imaginary, Imaginary)
来添加两个虚数并将它们存储在将调用方法的对象中。
It will look like:
它看起来像:
i3.addImaginary(i1, i2)
But it is up to you how you define the function, it can also be done as:
但这取决于您如何定义函数,也可以这样做:
i3=i1.add(i2);
回答by Christian
Java doesn't support user-defined operator overloading.But you can make a method in your Imaginary
class:
Java 不支持用户定义的运算符重载。但是你可以在你的Imaginary
类中创建一个方法:
public static Imaginary add(Imaginary other) {
return new Imaginary(real + other.real, imaginary + other.imaginary);
}
so, you can call it like:
所以,你可以这样称呼它:
Imaginary i3 = i1.add(i2);