java - 调用名称在字符串中的类
问题描述
我正在尝试调用一个类,其名称的一部分包含在通过读取控制台输入获得的字符串中。
static void menu() {
System.out.println("Choose a menu:");
System.out.print("\n");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String nome = null;
try {
nome = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print("\n");
System.out.println("Opening: " + nome);
System.out.print("\n");
String funzione = nome + ".funzione();";
try {
Runtime.getRuntime().exec(funzione);
} catch (IOException e) {
System.out.println(funzione);
e.printStackTrace();
}
基本上,如果我写“打印”,我想执行print.function();
稍后在程序中调用的。该代码有效,但它也输出错误java.io.IOException: Cannot run program "print.function();": error=2, No such file or directory
,但print.function();
无论如何都会执行。
需要明确的是,我是故意遵循这种方法而不是 switch case。
我认为我以错误的方式执行该过程,有人可以在哪里突出显示我吗?非常感谢。
解决方案
推荐阅读
- javascript - 如何从字符串化数组中删除引号
- c++ - 错误:从基类父亲派生类儿子时未定义基类
- python-3.x - 如何使用 python 在 xlsxwriter 中添加标注
- visual-studio-code - 使 Visual Studio Code 始终显示错误,即使在关闭有错误的文件后也是如此?
- tcl - 使用 tcl glob 进行通配符搜索
- javascript - 按子值寻址 Firebase 对象,然后修改该对象?
- algorithm - 如果我切割不同长度的杆,我如何获得 2^(n-1) 的结果总数?其中 n 是杆的长度
- laravel - Laravel Voyager,如何使用 eloquent 模型翻译表格
- java - RestAssured_Class Not Found_com/github/fge/jsonschema/main/JsonSchemaFactory
- javascript - 如何解决这个问题 Uncaught TypeError: Cannot read property 'addEventListener' of null