У меня есть перечисление, которое будет содержать мои алгоритмы. Я не могу создать экземпляр этих классов, потому что мне нужен контекст приложения, который доступен только после запуска приложения. Я хочу загрузить класс во время выполнения, когда я выбираю, вызывая getAlgorithm (Context cnx).
Как мне легко создать экземпляр класса во время выполнения, учитывая его .class (и мой конструктор принимает аргументы)? Все мои классы являются подклассами Алгоритма.
public enum AlgorithmTypes {
ALL_FROM_9_AND_LAST_FROM_10_ID(AlgorithmFactory.AlgorithmAllFrom9AndLastFrom10Impl.class),
ALL_FROM_9_AND_LAST_FROM_10_CURRENCY_ID(AlgorithmFactory.AlgorithmAllFrom9AndLastFrom10Impl.class),
DIVIDE_BY_9_LESS_THAN_100(AlgorithmFactory.AlgorithmAllFrom9AndLastFrom10Impl.class),
TABLES_BEYOND_5_BY_5(AlgorithmFactory.AlgorithmAllFrom9AndLastFrom10Impl.class);
private Class<? extends Algorithm> algorithm;
AlgorithmTypes(Class<? extends Algorithm> c) {
algorithm = c;
}
public Algorithm getAlgorithm(Context cnx) {
return //needs to return the current algoriths constructor which takes the Context Algorithm(Context cnx);
}
}