gpt4 book ai didi

java - 如何在Java map.entry值上调用用户定义的函数?

转载 作者:行者123 更新时间:2023-12-02 06:20:28 27 4
gpt4 key购买 nike

我正在尝试创建一个菜单,供用户查看 map 中的所有项目并进行选择。不幸的是,输出不太正确,因为它正在调用 pairs.getValue() 调用的默认 tostring 函数,而我希望它调用自定义函数 getName() >。如果我尝试使用pairs.getValue().getName(),我会收到语法错误,因为它期望这是一个对象,而对象没有 getName 函数。我尝试像 (Item)pairs.getValue().getName() 一样进行转换,但这也不起作用,所以我不太确定如何调用 getName 函数以获得正确的输出。由于我使用的是 map ,所以我也没有有效的方法让用户做出选择。如果他们输入“1”,我实际上无法在开关中使用 1 来获取首先输出的实际项目,因为 map 不使用索引......我想不出一个好方法来允许用户只需输入一个数字并能够从 map 中获取实际对象,因此建议一种新方法会很棒...如果是 C++,我会获取 map.iterator.begin() + int 来获取位置...但我认为你不能在 Java 中做到这一点。任何想法表示赞赏,谢谢!

Iterator it = character.get("Items").entrySet().iterator();
int i = 1;
while(it.hasNext())
{
Map.Entry pairs = (Map.Entry)it.next();
System.out.println("[" + i + "] " + pairs.getKey() + " = " + pairs.getValue());
i++;
}
System.out.print("\nYour Choice: ");
i = reader.readInt(1,character.get("Items").size());
switch(i)
{
//foo
}

如果您想查看任何其他代码,我会添加它,请告诉我。谢谢!

人物

/**
* Stats
* Items
* BodyParts
* Attributes
* Abilities
* Skills
* Effects
* Quests
* Weapons
* Armors
*
*
*
*
**/
import java.util.HashMap;
import java.io.Serializable;
public class Character extends GameObject
{
protected HashMap<String, HashMap> character;
protected HashMap<String, Weapon> weapons;
protected HashMap<String, Armor> armors;
protected HashMap<String, Attribute> attributes;
protected HashMap<String, Skill> skills;
protected HashMap<String, Effect> effects;
protected HashMap<String, Stat> stats;
protected HashMap<String, Ability> abilities;
protected HashMap<String, Quest> quests;
protected HashMap<String, Item> items;
protected HashMap<String, Response> responses;
protected HashMap<String, Implant> implants;

public Character()
{
name = "Greg";
description = "Just a regular guy living in a binary world";
buildCharacter();
}
public Character(String newName, String newDescription)
{
name = newName;
description = newDescription;
buildCharacter();
}
public Character(String newName, String newDescription, HashMap<String, Weapon> newWeapons, HashMap<String, Armor> newArmors, HashMap<String, Response> newResponses, HashMap<String, Attribute> newAttributes, HashMap<String, Skill> newSkills, HashMap<String, Effect> newEffects, HashMap<String, Stat> newStats, HashMap<String, Ability> newAbilities, HashMap<String, Quest> newQuests, HashMap<String, Item> newItems, HashMap<String, Implant> newImplants, int level, int coins, int health, int mana, int energy, int power, int accuracy, int toughness, int dexterity, int speed, int stealth, int concentration, int crit, int vision, int hearing, int smell, int temperature, int hunger, int thirst, String type)
{
character = new HashMap<String, HashMap>();
name = newName;
description = newDescription;
weapons = newWeapons;
armors = newArmors;
responses = newResponses;
attributes = newAttributes;
abilities = newAbilities;
quests = newQuests;
items = newItems;
implants = newImplants;
character.put("Weapons",weapons);
character.put("Armors",armors);
character.put("Attributes",attributes);
character.put("Skills",skills);
character.put("Effects",effects);
character.put("Stats",stats);
character.put("Abilities",abilities);
character.put("Quests",quests);
character.put("Items",items);
character.put("Responses",responses);
character.put("Implants",implants);
skills.put("Level", new Skill("Level","Overall Level",level));
items.put("Coins",new Item("Coins","Your Money", coins));
attributes.put("Health",new Attribute("Health","Your hit points",1,health));
attributes.put("Mana",new Attribute("Mana","Magic casting resource",1,mana));
attributes.put("Energy",new Attribute("Energy","Physical ability resource",1,energy));
attributes.put("Power",new Attribute("Power","Damage with attacks",1,power));
attributes.put("Accuracy",new Attribute("Accuracy","Chance to hit",1,accuracy));
attributes.put("Toughness",new Attribute("Toughness","Health regeneration / Physical damage reduction",1,toughness));
attributes.put("Dexterity",new Attribute("Dexterity","Energy regeneration / Dodge chance",1,dexterity));
attributes.put("Speed",new Attribute("Speed","Global cooldowns",1,speed));
attributes.put("Stealth",new Attribute("Stealth","Ability to remain hidden",1,stealth));
attributes.put("Concentration",new Attribute("Concentration","Mana regeneration / magical resistance",1,concentration));
attributes.put("Crit",new Attribute("Crit","Chance to hit criticals",1,crit));
attributes.put("Vision",new Attribute("Vision","View Distance",1,vision));
attributes.put("Hearing",new Attribute("Hearing","Ear range",1,hearing));
attributes.put("Smell",new Attribute("Smell","Smell Distance",1,smell));
attributes.put("Temperature",new Attribute("Temperature","Current Temperature",1,temperature));
attributes.put("Hunger",new Attribute("Hunger","Hunger Levels",1,hunger));
attributes.put("Thirst",new Attribute("Thirst","Amount of thirstiness",1,thirst));
stats.put("Type",new Stat("Type",type));
}
public void buildCharacter()
{
character = new HashMap<String, HashMap>();
responses = new HashMap<String, Response>();
weapons = new HashMap<String, Weapon>();
armors = new HashMap<String, Armor>();
attributes = new HashMap<String, Attribute>();
skills = new HashMap<String, Skill>();
effects = new HashMap<String, Effect>();
stats = new HashMap<String, Stat>();
abilities = new HashMap<String, Ability>();
quests = new HashMap<String, Quest>();
items = new HashMap<String, Item>();
implants = new HashMap<String, Implant>();
character.put("Weapons",weapons);
character.put("Armors",armors);
character.put("Attributes",attributes);
character.put("Skills",skills);
character.put("Effects",effects);
character.put("Stats",stats);
character.put("Abilities",abilities);
character.put("Quests",quests);
character.put("Items",items);
character.put("Responses",responses);
character.put("Implants",implants);
skills.put("Level", new Skill("Level","Overall Level",1));
items.put("Coins",new Item("Coins","Your Money"));
attributes.put("Health",new Attribute("Health","Your hit points",1,10));
attributes.put("Mana",new Attribute("Mana","Magic casting resource",1,10));
attributes.put("Energy",new Attribute("Energy","Physical ability resource",1,10));
attributes.put("Power",new Attribute("Power","Damage with attacks",1,1));
attributes.put("Accuracy",new Attribute("Accuracy","Chance to hit",1,1));
attributes.put("Toughness",new Attribute("Toughness","Health regeneration / Physical damage reduction",1,1));
attributes.put("Dexterity",new Attribute("Dexterity","Energy regeneration / Dodge chance",1,1));
attributes.put("Speed",new Attribute("Speed","Global cooldowns",1,1));
attributes.put("Stealth",new Attribute("Stealth","Ability to remain hidden",1,1));
attributes.put("Concentration",new Attribute("Concentration","Mana regeneration / magical resistance",1,1));
attributes.put("Crit",new Attribute("Crit","Chance to hit criticals",1,1));
attributes.put("Vision",new Attribute("Vision","View Distance",1,1));
attributes.put("Hearing",new Attribute("Hearing","Ear range",1,1));
attributes.put("Smell",new Attribute("Smell","Smell Distance",1,1));
attributes.put("Temperature",new Attribute("Temperature","Current Temperature",1,1));
attributes.put("Hunger",new Attribute("Hunger","Hunger Levels",1,1));
attributes.put("Thirst",new Attribute("Thirst","Amount of thirstiness",1,1));
stats.put("Type",new Stat("Type","Human"));
//effects
//abilities
//inventory
//dialogue
}
//getResponse
public HashMap get(String key)
{
return character.get(key);
}
public Weapon getWeapon(String key)
{
return weapons.get(key);
}
public void setWeapon(String key, Weapon newWeapon)
{
weapons.put(key,newWeapon);
}
public Armor getArmor(String key)
{
return armors.get(key);
}
public void setArmor(String key, Armor newArmor)
{
armors.put(key,newArmor);
}
public Attribute getAttribute(String key)
{
return attributes.get(key);
}
public Skill getSkill(String key)
{
return skills.get(key);
}
public Stat getStat(String key)
{
return stats.get(key);
}
public Ability getAbility(String key)
{
return abilities.get(key);
}
public Quest getQuest(String key)
{
return quests.get(key);
}
public Item getItem(String key)
{
return items.get(key);
}
public Effect getEffect(String key)
{
return effects.get(key);
}
public Response getResponse(String key)
{
return responses.get(key);
}
}

如果您想见家长......

import java.util.HashMap;
import java.io.Serializable;
public class GameObject
{
protected String name, description;
protected int number;
protected boolean bool;
public GameObject()
{
name = "Typical name of gameobjects";
description = "What would you expect - it's a game object";
bool = false;
number = 1;
}
public String getName()
{
return name;
}
public void setName(String newName)
{
name = newName;
}
public String getDescription()
{
return description;
}
public void setDescription(String newDescription)
{
description = newDescription;
}
public int getNumber()
{
return number;
}
public void setNumber(int newNumber)
{
number = newNumber;
}
public boolean getBool()
{
return bool;
}
public void setBool(boolean newBool)
{
bool = newBool;
}
}

最佳答案

这里正确使用的是类型参数(Java 世界中的泛型)。这是一种告诉编译器您想要在集合中存储什么类型的方法。

当您尝试在单个 map 中存储不同类型的对象时,我们将采用 GameObject 作为集合的基本类型。首先,让我们为主存储声明一个正确的内容类型:

protected HashMap<String, HashMap<String, ? extends GameObject>> character;
...
character = new HashMap<String, HashMap<String, ? extends GameObject>>();

这意味着我们有以下伪代码映射:

字符串 -> map (字符串 -> 扩展游戏对象的东西)

接下来,我们需要更改访问方法以反射(reflect)详细的返回类型:

//getResponse
public HashMap<String, ? extends GameObject> get(String key) {
return character.get(key);
}

并在使用站点上设置适当的类型边界:

    int i = 1;
for (Map.Entry<String, ? extends GameObject> item : character.get("Items").entrySet()) {
System.out.println("[" + i + "] " + item.getKey() + " = " + item.getValue().getName());
i++;
}

对我来说,它编译并运行时没有错误或警告。

关于java - 如何在Java map.entry值上调用用户定义的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21070874/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com