我正在尝试为用户创建一个菜单,以查看地图中的所有项目并进行选择。不幸的是,输出不太正确,因为它调用pairs.getValue()
调用的默认tostring函数,我希望它调用一个自定义函数getName()
。如果我尝试使用pairs.getValue().getName()
,我得到一个语法错误,因为它期望这是一个对象,并且对象没有getName函数。我试着像(Item)pairs.getValue().getName()
一样投射它,但那也不起作用,所以我不确定如何调用getName函数以获得正确的输出。自从我使用地图以来,我也没有让用户做出选择的有效方法。如果他们输入“1”,我实际上无法在开关中使用1来获取首先输出的实际项目,因为地图不使用索引...我想不出一种允许用户使用的好方法只需输入一个数字,并能够从地图中抓取实际对象,因此对于这种新方法的建议将非常棒......如果是C++,我会抓取map.iterator.begin()
和+
,int
来获取位置。 ..但我不认为你可以用Java来做到这一点。任何想法表示赞赏,谢谢!如何在Java map.entry值上调用用户定义的函数?
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;
}
}
另请参见[“什么是原始类型,为什么我们不应该使用它?”](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-我们使用它) – Radiodef
我仍然得到这个陈述'Iterator
这取决于'character'是如何声明的以及真正存储在里面的是什么类型。请添加此代码,我们将轻松消除所有编译器警告。 – Jk1