英文:
How to add item to Hashtable at Declaration?
问题
以下是你提供的代码的翻译部分:
所以我正在尝试创建一个基于文本的冒险/地牢探险游戏。我正在尝试使用类似于我在基于文本的宝可梦游戏中用于项目、怪物等存储的系统。这里是我在C#中存储系统的功能代码。
public static Dictionary<string, pokemonTrainer> pokemonTrainers = new Dictionary<string, pokemonTrainer>()
{
{"Robert", new pokemonTrainer{name="Robert",typesOfPokemon={"Plant","Normal"}, levelMultiplier=0.4}},
{"James", new pokemonTrainer{name="James",typesOfPokemon={"Plant","Normal","Water","Fire","Ice","Flying","Electric","Poison","Psychic","Fighting","Dark","Dragon","Fairy","Rock","Ghost","Ground","Bug"},levelMultiplier=0.8}}
};
我想创建一个哈希表,其中包含特定的键值对,就像C#代码中所做的那样,而不仅仅是使用.put()来添加它们,这可能吗?如果可能的话,我应该如何做?如果不行,创建这样一个存储系统的最佳方法是什么?
如果有帮助的话,这是我在Java中声明哈希表的方式,
public static Hashtable<Integer, monster> monster_data_base = new Hashtable<Integer, monster>()
{
};
这是我想要作为键值对值部分的monster类。
class monster
{
private int health;
private int damage;
private String name;
private int level;
monster(int health, int damage, String name, int level)
{
this.health=health;
this.damage=damage;
this.name=name;
this.level=level;
}
String get_name()
{
return name;
}
int get_health()
{
return health;
}
int get_damage()
{
return damage;
}
void change_health(int change_in_durability)
{
health=health+change_in_durability;
}
int get_level()
{
return level;
}
}
英文:
So I am trying to create a text-based adventure/dungeon crawler game. I am trying to use a similar system to what I used in a text-based Pokemon game for the storage of items, monsters, etc. Here is my functional code for the storage system in C#.
public static Dictionary<string, pokemonTrainer> pokemonTrainers = new Dictionary<string, pokemonTrainer>()
{
{"Robert", new pokemonTrainer{name="Robert",typesOfPokemon={"Plant","Normal"}, levelMultiplier=0.4}},
{"James", new pokemonTrainer{name="James",typesOfPokemon={"Plant","Normal","Water","Fire","Ice","Flying","Electric","Poison","Psychic","Fighting","Dark","Dragon","Fairy","Rock","Ghost","Ground","Bug"},levelMultiplier=0.8}}
};
I would like to create my hashtable with certain key-value pairs inside of it like what is being done in the C# code instead of just adding them with .put() is this possible? If so how would I do it? And if not what would be the best way to create a storage system like this?
If it helps at all here's my declaration for the hashtable in java,
public static Hashtable<Integer, monster> monster_data_base = new Hashtable<Integer, monster>()
{
};
and here's the monster class I would like to be the value part of the key-value pairs.
class monster
{
private int health;
private int damage;
private String name;
private int level;
monster(int health, int damage, String name, int level)
{
this.health=health;
this.damage=damage;
this.name=name;
this.level=level;
}
String get_name()
{
return name;
}
int get_health()
{
return health;
}
int get_damage()
{
return damage;
}
void change_health(int change_in_durabilty)
{
health=health+change_in_durabilty;
}
int get_level()
{
return level;
}
}
答案1
得分: 1
创建一个静态方法来初始化你的静态散列表对象。这将在类加载时进行初始化。
public class Main {
public static Hashtable<Integer, String> mytable = new Hashtable<>();
static {
initializeMytable();
}
// 静态初始化器,在这里你可以初始化你的静态对象
static void initializeMytable() {
mytable.put(2, "sam");
}
public static void main(String[] args) {
// 在之后需要的地方使用你的表格
System.out.println(mytable);
}
}
英文:
Create a static method to initialize your static hashtable object. This will be initialized when the class is loaded.
public class Main {
public static Hashtable<Integer,String> mytable = new Hashtable<>();
static {
initializeMytable();
}
// static initializer where you can initiate your static object
static void initializeMytable() {
mytable.put(2,"sam");
}
public static void main(String[] args) {
// use your table where you need it later
System.out.println(mytable);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论