使用数组字符串元素动态调用方法?

时间:2016-01-05 19:04:32

标签: java arrays methods

我试图遍历一个字符串数组,这样对于每个值,它会动态调用以它命名的setter方法,例如one.set"holdingArray[i]"(a);。有没有办法实现这种行为? 这是一个代码示例来说明我的问题。第one.setholdingArray[i](a);行正在编译,必须更改。

class Troops {
    private int barbarian;
    private int archer;
    private int goblin;
    private int giant;

    private String[] holdingArray =  {
            "Barbarian",
            "Archer",
            "Goblin",
            "Giant",
    };

    int getBarbarian() {
        return barbarian;
    }
    int getArcher() {
        return archer;
    }
    int getGoblin() {
        return goblin;
    }
    int getGiant() {
        return giant;
    }

    void setBarbarian(int barb) {
        barbarian = barb * 150;
    }
    void setArcher(int a) {
        archer = a * 300;
    }
    void setGoblin(int g) {
        goblin = g * 80;
    }
    void setGiant(int gi) {
        giant = gi * 2250;
    }

    class HelloWorld {
        public static void main(String args[]) {
            Scanner in = new Scanner(System.in);
            Troops one = new Troops();
            int a;

            for(int i = 0; i < holdingArray.length; i++) {
                System.out.println("How many " + holdingArray[i] + " do you have??");
                a = in.nextInt();
                // TODO this line must be changed with the answer
                one.setholdingArray[i](a);
                System.out.println();
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

您似乎正在尝试实施地图 - https://docs.oracle.com/javase/7/docs/api/java/util/Map.html

你的钥匙是部队类型(倒钩,弓箭手等),他们的价值就是他们的数量。看一眼就是

std::sort(v)

如果您想按照自己的方式进行操作,那么您可以根据i值调用不同的setter ...就像i == 0然后setBarbarian,i == 1 setArcher。使用地图会更有效率。

答案 1 :(得分:0)

我认为这就是你想要的,

public static void main(String args[]) {

    Scanner in = new Scanner(System.in);
    Troops one = new Troops();
    int a;
    Method method = null;
    for (int i = 0; i < one.holdingArray.length; i++) {

        System.out.println("How many " + one.holdingArray[i] + " do you have??");
        a = in.nextInt();

        try {
            method = one.getClass().getDeclaredMethod("set" + one.holdingArray[i], Integer.class);
            method.invoke(one, a);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    System.out.printf("");

}

static class Troops {

    private int barbarian;
    private int archer;
    private int goblin;
    private int giant;

    public String[] holdingArray = {
            "Barbarian",
            "Archer",
            "Goblin",
            "Giant",
    };


    //Getters.
    int getBarbarian() {
        return barbarian;
    }

    int getArcher() {
        return archer;
    }

    int getGoblin() {
        return goblin;
    }

    int getGiant() {
        return giant;
    }


    //Setters.

    void setBarbarian(Integer barb) {
        barbarian = barb * 150;
    }

    void setArcher(Integer a) {
        archer = a * 300;
    }

    void setGoblin(Integer g) {
        goblin = g * 80;
    }

    void setGiant(Integer gi) {
        giant = gi * 2250;
    }
}

}