Java 如何将类方法分配给HashMap中的值?
我正在创建一个基于文本的“真实生活游戏”程序。我正在努力寻找一种有效的方法来分配方法,以实现游戏板(具有明确起点/终点的线性游戏板)的每个磁贴所给出的方向。游戏中的每个玩家都由一个“player”类的对象表示,该类对象包含不同游戏统计数据的变量(包括其在棋盘上位置的占位符值),并且每个玩家对象都包含在哈希映射中。我想创建一个循环,循环遍历每个玩家对象,并根据各自的占位符值,引用该棋盘平铺的特定方法 我的主要班级:Java 如何将类方法分配给HashMap中的值?,java,loops,hashmap,Java,Loops,Hashmap,我正在创建一个基于文本的“真实生活游戏”程序。我正在努力寻找一种有效的方法来分配方法,以实现游戏板(具有明确起点/终点的线性游戏板)的每个磁贴所给出的方向。游戏中的每个玩家都由一个“player”类的对象表示,该类对象包含不同游戏统计数据的变量(包括其在棋盘上位置的占位符值),并且每个玩家对象都包含在哈希映射中。我想创建一个循环,循环遍历每个玩家对象,并根据各自的占位符值,引用该棋盘平铺的特定方法 我的主要班级: import java.util.Scanner; import java.uti
import java.util.Scanner;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Coin coin = new Coin();
Die die = new Die();
Scanner choice = new Scanner(System.in);
System.out.println("Choose player amount:");
int n = choice.nextInt();
/*This map contains master list of all players and player values*/
HashMap<Integer, Player> playerList = new HashMap<Integer, Player>();
/*This map contains just the true_false value of life for each player*/
HashMap<Integer, Boolean> lifeCheck = new HashMap<Integer, Boolean>();
/*This section defines number/sex/name of each player*/
for (int y = 1; y <= n; y++) {
System.out.println("New player now flips a coin.");
coin.flip();
if (coin.get() == true) {
System.out.println("You flipped heads! You are a man.");
} else {
System.out.println("You flipped tails! You are a woman.");
}
System.out.println("What is your name?");
String user = choice.next();
playerList.put(y, new Player(user));
/*This adds a check functionality to ensure live players still exist*/
lifeCheck.put(y, playerList.get(y).pulse());
}
/*This invokes a new instance of our game board*/
GameSpace gameBoard = new GameSpace();/*Not yet defined*/
/*This do_while loop checks that a live player exists after each turn*/
do {
/*This is where I think I would be putting the majority of
the game code*/
} while (lifeCheck.containsValue(true));
/*This tests to see if my player naming loop works properly*/
/*
int x = 1;
for (int i = 1; i <= n; i++) {
System.out.println("Player number " + x + " is named " + playerList.get(i).name);
x++;
}
*/
choice.close();
}
}
从这里开始,哪里是最符合逻辑的地方?如果HashMap的值是您要调用的方法的名称,您应该能够使用反射:
HashMap<Integer, String> map = new HashMap<>();
map.put(0, "methodName1");
map.put(1, "methodName2");
String methodName = map.get(player.getPosition());
Method method = Player.class.getDeclaredMethod(methodName);
method.invoke();
HashMap map=newhashmap();
map.put(0,“methodName1”);
map.put(1,“methodName2”);
String methodName=map.get(player.getPosition());
Method=Player.class.getDeclaredMethod(methodName);
方法调用();
如果我理解了这个问题,您会问如何存储与电路板上每个位置相关联的方法,并指定在该位置执行的操作。如果是这样,我建议您将其封装在位置中。最好将其定义为接口:
interface Position {
void operateOnPlayer(Player player);
}
然后,电路板成为从起点到某个位置的地图:
Map<Integer,Position> board;
它在以前版本的Java中也同样适用,您只需要创建Position
的(可能是匿名的)实现
称之为这些是非常简单的:
for (Player player: players)
board.get(player.getPosition()).operateOnPlayer(player);
或者,更好的是,将其添加到Player
类中,这样就不需要公开position字段:
class Player {
public takeTurn() {
board.get(position).operateOnPlayer(this);
}
}
还有其他机制,比如映射到
消费者
,但我真的认为它们不如定义显式接口那么好 请尝试提供一个完整的、最少的示例。大部分代码似乎与您的问题无关,我们唯一需要处理的是您的标题和最后一句话。现在还不清楚您的问题到底是什么,以及为什么要对hashmaps和“占位符值”执行某些操作。我想使用Player类的placeHold整数值直接引用一个HashMap键,该键激活相应游戏磁贴的方法。这是实现我所能看到的目标的最好方法,但我一直未能成功地实现它。如果这些方法都在不同的类中呢
board.put(17, player -> player.moveBack(4));
board.put(16, player -> player.skipATurn());
for (Player player: players)
board.get(player.getPosition()).operateOnPlayer(player);
class Player {
public takeTurn() {
board.get(position).operateOnPlayer(this);
}
}