我需要一种方法来随机化通过和持有的结果 类

I need a way to randomize outcomes passing and holding classes

我正在尝试添加 5 个 class,它们都扩展了通用 class 并以不同的方式实现了 init() 方法。 我需要的是一种存储那些 class 的方法,同时将 Class 的许多机会传递给 "happen"

为此我创建了一个 Class 持有人:

public class ClassHolder {

    private Class<? extends GeneralOutcome> holdClass;
    private int chances;

    public ClassHolder(Class<? extends GeneralOutcome> holdClass, int chances) {
        super();
        this.holdClass = holdClass;
        this.chances = chances;
    }

    public Class<? extends GeneralOutcome> getHoldClass() {
        return holdClass;
    }
    public void setHoldClass(Class<? extends GeneralOutcome> holdClass) {
        this.holdClass = holdClass;
    }
    public int getChances() {
        return chances;
    }
    public void setChances(int chances) {
        this.chances = chances;
    }
}

还有一个 GeneralOutcome class,将添加到列表中的那些将扩展:

public class GeneralOutcome {

    public void init(String text, int times) {

    }
}

以及我将它们添加到列表的方式:

public class Randomizer {

    private static List<ClassHolder> myList = new ArrayList<ClassHolder>();

    private static ClassHolder outcome01 = new ClassHolder(Outcome01.class, 10);
    private static ClassHolder outcome02 = new ClassHolder(Outcome02.class, 10);
    private static ClassHolder outcome03 = new ClassHolder(Outcome03.class, 10);
    private static ClassHolder outcome04 = new ClassHolder(Outcome04.class, 10);
    private static ClassHolder outcome05 = new ClassHolder(Outcome05.class, 10);


    public static void main(String[] args) {

        for(int i = 0; i < outcome01.getChances(); i++) {
            myList.add(outcome01);
        }
        for(int i = 0; i < outcome02.getChances(); i++) {
            myList.add(outcome02);
        }
        for(int i = 0; i < outcome03.getChances(); i++) {
            myList.add(outcome03);
        }
        for(int i = 0; i < outcome04.getChances(); i++) {
            myList.add(outcome04);
        }
        for(int i = 0; i < outcome05.getChances(); i++) {
            myList.add(outcome05);
        }

        System.out.println(myList.size());

        int rand = (int) (Math.random() * myList.size());

        System.out.println(rand);

        ClassHolder theHoldClass = myList.get(rand);

        System.out.println(theHoldClass.getHoldClass());

        Class<? extends GeneralOutcome> theOutcome = theHoldClass.getHoldClass();

        theOutcome.init();

    }
}

问题是我无法(不知道真的)转换回 GeneralOutcome 以访问 .init() 方法。 我得到 The method init() is undefined for the type Class<capture#3-of ? extends GeneralOutcome>

我知道这不是最好的方法。所以我对两者都持开放态度,解决这个问题以及实现类似目标的更好方法。

由于某些原因,您在此处尝试执行的操作无效。

首先,您的 init 方法不是静态的。所以那个电话

Class<? extends GeneralOutcome> theOutcome = theHoldClass.getHoldClass();
theOutcome.init();

直接导致编译时错误。

但是,整个设计看起来奇怪。首先持有 Class 个对象有什么意义?

你为什么不创建一个接口

public interface OutcomeFunctionality {
   public void foo(String text, int times);
}

稍后实例化任何 class 实现该接口的对象 对象 ?以便您最终可以处理此类对象的列表(以及这些概率)?

[ 我故意使用名称 foo :仅奇怪的名称 "init" 就让人很不清楚您的代码打算做什么!从这个意义上说,您应该重新考虑您的设计,并找到更好的方法名称来表达这些方法将要做什么! ]

长话短说:using/holding Class 对象不会在您的示例代码中给您带来任何好处 - 它只会增加 复杂性。所以我的建议是:开始在那里工作并摆脱它 "detour"。您可能还想阅读有关 Open/Closed principle 的内容——它可以为您提供一些指导,说明如何使用抽象 classes / subclassing 来拆分 "behavior" 在基础和派生之间 classes.