Java 在一行上调用多个方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21199419/
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
Call Multiple Methods on one Line?
提问by sparklyllama
If I have a line like this in my program:
如果我的程序中有这样一行:
fireBalls.add(new Fireball(tileMap).setPosition(20, 20)); // set position is a method of the fireball class
How can I call multiple methods like that, on that same line?
如何在同一行上调用多个这样的方法?
I tried this:
我试过这个:
fireBalls.add(new Fireball(tileMap).setPosition(20, 20).setLeft());
But the setLeft()
method can't be put there, as it can't be called on a void type.
I can't have them on separate lines, as I don't know what index it would be in the fireBalls ArrayList.
但是该setLeft()
方法不能放在那里,因为它不能在 void 类型上调用。我不能将它们放在单独的行上,因为我不知道它在 fireBalls ArrayList 中的索引是什么。
采纳答案by Hovercraft Full Of Eels
It looks like you're trying to be too succinct and in the process possibly shooting yourself in the foot. You don't need to know the ArrayList index to set up the Fireball object that you're adding. You just need to have a reference to the object, and you can easily do that by creating a local Fireball variable, setting it up, and then adding it to the ArrayList.
看起来你试图过于简洁,在这个过程中可能会用脚射击自己。您无需知道 ArrayList 索引即可设置要添加的 Fireball 对象。您只需要引用该对象,您就可以通过创建本地 Fireball 变量、设置它,然后将其添加到 ArrayList 来轻松做到这一点。
Why not simply do:
为什么不简单地做:
Fireball fireball = new Fireball(tileMap);
fireball.setPosition(20, 20);
fireball.setLeft();
fireBalls.add(fireball);
回答by Christian
You can modify your Fireball
methods, so they return an instance of that class (in other words return this;
)
您可以修改您的Fireball
方法,以便它们返回该类的实例(换句话说return this;
)
public Fireball setPosition(int x, int y) {
...
return this;
}
With this, the call to
有了这个,调用
new Fireball(tileMap).setPosition(20, 20)
will return the recent created instance, so you can call setLeft()
from that instance.
You could implement this for setLeft()
too.
将返回最近创建的实例,因此您可以setLeft()
从该实例调用。你也可以实现这个setLeft()
。
public Fireball setLeft() {
...
return this;
}
回答by Abimaran Kugathasan
To do that, your Fireball
class should follow builderpattern, which methods return the instance itself. So, you can can chain the method in a line.
为此,您的Fireball
类应遵循构建器模式,哪些方法返回实例本身。因此,您可以将方法链接在一行中。
For example method : java.lang.StringBuilder#append()
回答by nhgrif
While I believe Hovercraft's answer is the best by far, an alternative solution might be to write a better constructor.
虽然我相信 Hovercraft 的答案是迄今为止最好的,但另一种解决方案可能是编写一个更好的构造函数。
A constructor that takes a tileMap
(whatever this is), two ints
for setting position, as well as a boolean
or enum or something appropriate for whatever setLeft()
does could be used here.
一个构造函数,它接受一个tileMap
(无论是什么),两个ints
用于设置位置,以及一个boolean
或枚举或适合任何setLeft()
可以在这里使用的东西。
fireballs.add(new Fireball(tileMap, 20, 20, true));
回答by Hitesh Vyas
you can make mathod chain like this :
你可以像这样制作 mathod 链:
class a{
static a obj= new a();
static a show(){
System.out.println("omg");
return obj;
}
static a show2(){
System.out.println("god");
return obj;
}
void show3(){
System.out.println("Research done");
}
}
}
public class eg4{
公共类eg4{
public static void main (String hv[]){
a obj2= new a();
obj2.show().show2().show3();
}
}
}