Advertisement
dxvmxnd

Untitled

Feb 25th, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import java.lang.reflect.Method;
  2.  
  3. public class FunctionFactory {
  4. public static void main(String[] args) {
  5. String input = "sin cos tg ctg sinh cosh";
  6. String[] functions = input.split(" ");
  7.  
  8. for (String function : functions) {
  9. try {
  10. // Формируем имя класса
  11. String className = capitalize(function) + "Function";
  12. // Получаем класс по имени
  13. Class<?> clazz = Class.forName(className);
  14. // Создаем экземпляр класса
  15. Object instance = clazz.getDeclaredConstructor().newInstance();
  16.  
  17. // Вызываем метод evaluate
  18. Method evaluateMethod = clazz.getMethod("evaluate");
  19. evaluateMethod.invoke(instance);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }
  25.  
  26. private static String capitalize(String str) {
  27. return str.substring(0, 1).toUpperCase() + str.substring(1);
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement