【发布时间】:2018-11-29 19:59:10
【问题描述】:
我正在尝试实现 KeyListener 接口。在实现中,我想根据键代码(keyPressed)用我的“播放器”对象和一堆列表来做一些事情,这些列表是在另一个类中创建的。到目前为止,我一直将它们作为参数传递,但现在我无法更改“keyPressed”参数列表。我唯一的想法是创建一个代表播放器和列表的静态字段并通过 ClassName.staticField 访问它们,但这可能不是最好的主意。我怎样才能以更正确的方式访问它们?
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
//doing some stuff with code
if(code == KeyEvent.VK_SHIFT)
player.manipulateItem(/*few lists to pass here*/); //how to access the "player" object and lists?
} //they are created in another class as variables
【问题讨论】:
标签: java parameters interface keylistener