gpt4 book ai didi

java - java解决方案中的动态类创建

转载 作者:行者123 更新时间:2023-12-02 00:08:56 25 4
gpt4 key购买 nike

我知道这行不通,这在我看来很糟糕,但我想知道我最好的解决方案是什么。经过一番搜索,我发现了 ORM 和 hibernate。在了解了他们之后,我发现很多人说这比其值(value)更麻烦,并且提倡懒惰。请问有人能给我指出一个好的方向吗?

//Setup the weapon classes
logger.info("Initializing the weapon classes.");
stat = conn.prepareStatement("SELECT *" +
" FROM " + DB_NAME + ".weapons");
rs = stat.executeQuery();
while (rs.next()) {
Weapon rs.getString("name") = new Weapon(rs.getint("weapon_id"), rs.getString("name"), rs.getString("description"), rs.getString("filename"), rs.getint("rarity"), rs.getint("sell_price"), rs.getint("quantity_max"), rs.getint("slot"), rs.getint("level_requirement"), rs.getint("strength_requirement"), rs.getint("dexterity_requirement"), rs.getint("intelligence_requirement"), rs.getint("enchanted_increase"), rs.getint("enchanted_cost"), rs.getint("hit_min"), rs.getint("hit_max"), rs.getint("speed"), rs.getint("elemental_type"), rs.getint("elemental_hit_min"), rs.getint("elemental_hit_max"));
}

我知道这个问题已经被问过很多次了,但我只是发现了很多相互矛盾的观点。 :(

最佳答案

我建议使用 Map存储从数据库中获取的武器实例。键是名称,值是您的 Weapons 类的实例。

类似于:

while(rs.next()){
Weapon weapon = new Weapon(rs.getint("weapon_id"), rs.getString("name"), rs.getString("description"), rs.getString("filename"), rs.getint("rarity"), rs.getint("sell_price"), rs.getint("quantity_max"), rs.getint("slot"), rs.getint("level_requirement"), rs.getint("strength_requirement"), rs.getint("dexterity_requirement"), rs.getint("intelligence_requirement"), rs.getint("enchanted_increase"), rs.getint("enchanted_cost"), rs.getint("hit_min"), rs.getint("hit_max"), rs.getint("speed"), rs.getint("elemental_type"), rs.getint("elemental_hit_min"), rs.getint("elemental_hit_max"));
//Assuming you have an instance of Map<String, Weapon>
map.add(rs.getString("name"), weapon);
}

关于java - java解决方案中的动态类创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13263910/

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