如何从JDA中的方法访问变量
How to access variable from method in JDA
我正在制作一个 Discord 机器人,但遇到了困难。我在同一个文件中有 2 个方法。
方法一:
@Override
public void onSelectMenuInteraction(SelectMenuInteractionEvent event) {
int a = Integer.parseInt(event.getValues().get(0));
}
方法二:
@Override
public void playlistLoaded(AudioPlaylist playlist) {
final AudioTrack Track = playlist.getTracks().get(a);
}
问题:如何从方法 1 访问变量 a?
尝试将其设置为字段
public class SomeClass extends ListenerAdapter {
private int track;
@Override
public void onSelectMenuInteraction(SelectMenuInteractionEvent event) {
track = Integer.parseInt(event.getValues().get(0));
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
final AudioTrack Track = playlist.getTracks().get(track);
}
或者使用堆栈
public class SomeClass extends ListenerAdapter {
private final Stack<Integer> tracks = new Stack<Integer>();
@Override
public void onSelectMenuInteraction(SelectMenuInteractionEvent event) {
int track = Integer.parseInt(event.getValues().get(0));
tracks.push(track);
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
final AudioTrack Track = playlist.getTracks().get(tracks.pop());
}
我正在制作一个 Discord 机器人,但遇到了困难。我在同一个文件中有 2 个方法。
方法一:
@Override
public void onSelectMenuInteraction(SelectMenuInteractionEvent event) {
int a = Integer.parseInt(event.getValues().get(0));
}
方法二:
@Override
public void playlistLoaded(AudioPlaylist playlist) {
final AudioTrack Track = playlist.getTracks().get(a);
}
问题:如何从方法 1 访问变量 a?
尝试将其设置为字段
public class SomeClass extends ListenerAdapter {
private int track;
@Override
public void onSelectMenuInteraction(SelectMenuInteractionEvent event) {
track = Integer.parseInt(event.getValues().get(0));
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
final AudioTrack Track = playlist.getTracks().get(track);
}
或者使用堆栈
public class SomeClass extends ListenerAdapter {
private final Stack<Integer> tracks = new Stack<Integer>();
@Override
public void onSelectMenuInteraction(SelectMenuInteractionEvent event) {
int track = Integer.parseInt(event.getValues().get(0));
tracks.push(track);
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
final AudioTrack Track = playlist.getTracks().get(tracks.pop());
}