Ani*_*kar 0 java addition operator-keyword
我知道String是java.lang中的最后一个类,所以像string类一样可以使用plus(+)运算符追加其他类.
例如我有一个类:
public class Foo{
public int x;
public int y;
public Foo(int x,int y){
this.x=x;
this.y=y;
}
public Foo append(int x,int y){
return new Foo(this.x+x,this.y+y);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,是否可以添加两个类,如下所示:
Foo a=new Foo(2,3),b=new Foo(3,4);
Foo c=a+b;
System.out.println(c.x+" "+c.y);
Run Code Online (Sandbox Code Playgroud)
得到这样的输出:
5 7
Run Code Online (Sandbox Code Playgroud)
如果是的话,我还需要做什么,如果没有原因?
| 归档时间: |
|
| 查看次数: |
119 次 |
| 最近记录: |