简体   繁体   English

Java解决方案中的动态类创建

[英]dynamic class creation in java solution

I know that this will not work, which sucks IMO, but I am wondering what is my best solution. 我知道这行不通,这给IMO带来了麻烦,但是我想知道什么是我最好的解决方案。 After some searching I have found out about ORM and hibernate. 经过一些搜索,我发现了有关ORM和休眠的信息。 After finding out about them I have found many people who say its more hassle than it is worth and promotes laziness. 在找到他们之后,我发现许多人说它比它值得的麻烦更多,并且促进了懒惰。 Please can anyone point me in a good direction? 请谁能指出我的正确方向?

//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"));
}

I know this has been asked many times, but I am just finding so many contradictory opinions. 我知道这个问题已经被问过很多次,但我只是发现了许多矛盾的意见。 :( :(

I would suggest using a Map to store your instance for weapons that you fetch from your database. 我建议您使用地图来存储您从数据库中获取的武器的实例。 The key would be the name and the value would be an instance of your Weapons class. 密钥将是名称,值将是Weapons类的实例。

Something like: 就像是:

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);
}

Using an ORM depends on your project size. 使用ORM取决于您的项目规模。 Personally, and by looking at your understanding of Java, I would not recommend that you jump into hibernate and such for now. 就个人而言,通过查看您对Java的理解,我建议您暂时进入休眠状态。 I would suggest that you play directly with SQL first so you have a better understanding of persistence. 我建议您首先直接使用SQL,以便您更好地了解持久性。

As for the code you posted, here is what you could do : 至于您发布的代码,这是您可以做的:

//Setup the weapon classes
logger.info("Initializing the weapon classes.");
stat = conn.prepareStatement("SELECT *" + 
                            " FROM " + DB_NAME + ".weapons");

HashMap<String, Weapon> weapons = new HashMap<String, Weapon>();

rs = stat.executeQuery();
while (rs.next()) {
    Weapon weapon = new Weapon()
        .setId(rs.getint("weapon_id"))
        .setName(rs.getString("name"))
        .setDescription(rs.getString("description"))
        .setFilename(rs.getString("filename"))
        .setRarity(rs.getint("rarity"))
        .setSellPrice(rs.getint("sell_price"))
        .setQuantityMax(rs.getint("quantity_max"))
        .setSlot(rs.getint("slot"))
        .setLvlRequirement(rs.getint("level_requirement"))
        .setStrRequirement(rs.getint("strength_requirement"))
        .setDexRequirement(rs.getint("dexterity_requirement"))
        .setIntRequirement(rs.getint("intelligence_requirement"))
        .setEnchantedInc(rs.getint("enchanted_increase"))
        .setEnchantedCost(rs.getint("enchanted_cost"))
        .setHitMin(rs.getint("hit_min"))
        .setHitMax(rs.getint("hit_max"))
        .setSpeed(rs.getint("speed"))
        .setElementalType(rs.getint("elemental_type"))
        .setElementalHitMin(rs.getint("elemental_hit_min"))
        .setElementalHitMax(rs.getint("elemental_hit_max"));

    weapons.put(rs.getString("name"), weapon);
}

Avoid having a constructor (or even a method!) with so many arguments. 避免拥有太多参数的构造函数(甚至是方法!)。 Unless you are exporting your class to a third party or exposing it to some user plugins and don't want anyone to change the internal values, don't worry about setters! 除非您将类导出到第三方或将其暴露给某些用户插件,并且不希望任何人更改内部值,否则不要担心设置器! Then each set* methods return this so they can be chained. 然后,每个set*方法都返回this以便可以将它们链接起来。 For example : 例如 :

public Weapon setHitMax(int max) {
    this.hitMax = max;
    return this;
}

And you can, then access your weapons using the Map . 然后,您可以使用Map来访问武器。 Like, for example : 例如,例如:

weapons.get("sword").getHitMax();

What exactly do you mean by "dynamic class creation"? “动态类创建”到底是什么意思? There's two things I can understand from your code: 我可以从您的代码中了解两点:

a) you're a reaallly bad Java programmer :) or a)您是一个非常糟糕的Java程序员:)或

b) you're trying to have your variable named differently for each row in the database, (which doesn't make any sense) b)您试图为数据库中的每一行以不同的方式命名变量 (这没有任何意义)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM