英文:
Java how to add items directly to a class rather than having an array object
问题
我在类的一部分中有一个Array,可以向其中添加项目,但我想直接向类本身添加项目。这可能吗?
public class Buttons {
public Array<SimpleButton> buttons = new Array<SimpleButton>();
}
然后我可以像这样添加项目:
buttons.buttons.add(simpButton);
但我想要像这样直接添加项目:
buttons.add(simpButton);
英文:
I have an Array as part of a class and can add items to that, but I would like to add items directly to the Class itself. Is that possible?
public class Buttons {
public Array<SimpleButton> buttons = new Array<SimpleButton>();
}
Then I can add items like this:
buttons.buttons.add(simpButton);
But I want to add items directly like this:
buttons.add(simpButton);
答案1
得分: 3
你可以为你的类添加一个"Add"函数,并通过类对象来使用它
public class Buttons {
public Array<SimpleButton> buttons = new Array<SimpleButton>();
public void add(SimpleButton input){
// 处理无效输入
buttons.add(input);
}
}
这样使用:
Buttons buttonObj = new Buttons();
buttonObj.add(SimpleButtonTmp); //SimpleButtonTmp -> SimpleButton Obj
或者如果你不想走这条路,你可以这样做:
public class Buttons extends Array<SimpleButton>
然后你可以直接访问所有的函数:
public static void main(String[] args) {
Buttons myButton = new Buttons();
myButton.*AnyfunctionInArray();
}
英文:
You can add an "Add" function to your class and utilize it via the class object
public class Buttons {
public Array<SimpleButton> buttons = new Array<SimpleButton>();
public void add(SimpleButton input){
//Handle invalid input
buttons.add(input);
}
}
So to use it you will do something like -
Buttons buttonObj = new Buttons();
buttonObj.add(SimpleButtonTmp); //SimpleButtonTmp -> SimpleButton Obj
Otherwise if you don't wanna go this route you can do the following -
public class Buttons extends Array<SimpleButton>
Then you can access all the functions directly
public static void main(String[] args)
{
Buttons myButton = new Buttons();
myButton.*AnyfunctionInArray();
}
答案2
得分: 1
如果您确定您的类基本上作为 List 进行操作,您可以利用 Guava 中的 ForwardingList。正如名称所示,它将所有调用转发到获得的 delegate() 实例。这样,您就不必实现所有方法,只需覆盖特定的方法。
public class ButtonList extends ForwardingList<SimpleButton> {
private final List<SimpleButton> delegate = new ArrayList<>();
@Override
protected List<SimpleButton> delegate() {
return delegate;
}
}
由于默认情况下会自动转发调用,您可以调用任何常规的 List 方法。
ButtonList list = new ButtonList();
list.add(new SimpleButton());
int size = list.size(); // 1
然而,如果您的类不作为 List 进行操作,我建议自己创建所需的代理方法。这将隐藏实现并使未来的更改变得更容易。当然,这可能需要一些工作,但现代集成开发环境(IDE)支持为您创建代理方法。
参考资料:
英文:
If you are sure that your class basically acts as List, you can utilize the ForwardingList from Guava. As the name suggests, it will forward all calls to the obtained delegate() instance. This way, you don´t have to implement all methods youself and can only override specific methods.
public class ButtonList extends ForwardingList<SimpleButton> {
private final List<SimpleButton> delegate = new ArrayList<>();
@Override
protected List<SimpleButton> delegate() {
return delegate;
}
}
Since calls are forwarded by default, you can call any regular List method.
ButtonList list = new ButtonList();
list.add(new SimpleButton());
int size = list.size(); // 1
However, if your class does not act as a List, i would recommend to create the required delegate methods yourself. This will hide implementation and makes future changes easier. Of course, this can be a bit of work, but modern IDEs have support the create delegate methods for you.
References:
答案3
得分: 0
你可以使用 **ArrayList** 并扩展所有 **ArrayList** 方法,示例:
import java.util.ArrayList;
public class Buttons extends ArrayList {
public ArrayList<String> buttons = new ArrayList<String>();
}
主类:
public class Main {
public static void main(String[] args) {
Buttons buttons = new Buttons();
buttons.add("button 1");
int i = 0;
while (i < buttons.size()) {
System.out.println(buttons.get(i));
i++;
}
}
}
英文:
You could use ArrayList and extend all ArrayList Methods example:
import java.util.ArrayList;
public class Buttons extends ArrayList{
public ArrayList<String> buttons = new ArrayList<String>();
}
Main Class:
public class Main{
public static void main(String[] args) {
Buttons buttons = new Buttons();
buttons.add("button 1");
int i=0;
while(i < buttons.size())
{
System.out.println(buttons.get(i));
i++;
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!

评论