In the current class, you must programmatically determine the name of another class (of the form "ClassNumPath + i" ) and then call its methods.
ClassNumPath0.java class ClassNumPath0.java (as well as ClassNumPath1.java .. ClassNumPath5.java ) exist and their methods are called without errors if you directly access them in code, for example:
ClassNumPath0.initPositions(); or
ClassNumPath4.initPositions(); .. then there are no problems - everything works as it should, drawing on onDraw done and so on ... But I need to determine the class name ClassNumPathХ dynamically and be able to change this class name in the course of the action.
Part of the code over which I fight:
public class ClassNumPath extends SurfaceView implements SurfaceHolder.Callback { ... int currentNum = 5; String currentClassName; Class cClass; ... @Override protected void onDraw(Canvas canvas) { if (needInitialazation) { currentClassName = "com.mytestapp.testapp.ClassNumPath" + currentNum; cClass = Class.forName(currentClassName); cClass.initPositions(); // Ошибка: Cannot resolve method 'initPositions()' ClassNumPath5.initPositions(); // А это работает правильно Log.d("INF", "currentClassName = " + currentClassName); Log.d("INF", "cClass = " + cClass); // Эти логи выдают правильное имя класса com.mytestapp.testapp.ClassNumPath5 } ... cClass.checkState(); // Ошибка: Cannot resolve method 'checkState()' ClassNumPath5.checkState(); // А это работает правильно ... } So the question is:
How can I access the methods of a class whose name was dynamically set?
Or, please, tell me how to solve the problem differently. I do not want to build a large structure with Case-type operators with the choice of the desired class name depending on the currentNum value, since the ClassNumPathХ classes can be more than 100 pieces.
ClassNumPath1-ClassNumPathN. Then, for example, put one object from each class into an array, then callmyClasses[currentNum].initPositions(). It is possible to approach this decision more accurately, but "at least so." - Regent