Java如何将字符串作为类构造器对象名,类对象能否多次使用?
问题解答
1. Java 能否将字符串内容作为类构造器的对象名?
Java 是静态强类型编译语言,变量名(你所说的「对象名」)属于编译期确定的符号,无法在运行时动态用字符串作为变量名。
你想通过拼接递增数字区分不同对象的需求,完全可以用Map容器实现:
// 示例:用字符串作为key存储Create_File实例 Map<String, Create_File> instanceMap = new HashMap<>(); int count = 0; // 每次需要新建对象时拼接key String key = "create_file_" + count++; instanceMap.put(key, new Create_File()); // 需要使用时通过字符串key取出对象 Create_File target = instanceMap.get("create_file_0");
补充:你当前的Create_File类所有方法都是静态方法,根本不需要实例化对象,直接用Create_File.create()类名调用即可,不需要动态生成对象名。
2. 类的对象能否多次使用?
可以。只要你持有对象的引用,没有被JVM垃圾回收,就可以在程序运行周期内反复使用该对象调用成员方法。
你当前代码中每次调用select()方法都新建Create_File实例属于不必要的操作,完全可以全局保留一个Create_File实例反复调用。
3. 程序持续运行直到用户主动退出的实现方案
你当前程序执行一次就退出的核心原因是没有加循环逻辑,只需要把菜单选择逻辑套进while(true)死循环即可,只有用户选择退出选项时才终止程序。
另外你现有代码存在两个影响循环运行的问题:
- 每次调用
select()都通过try-with-resources创建并关闭BufferedReader,第二次读取输入时会报流已关闭的错误,需要把流的创建提到循环外 Create_File的info()方法逻辑错误:把读取文件内容的逻辑写到了「文件不存在」的else分支里,且Scanner未关闭
修改后的Selecter类参考代码
import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; public class Selecter { // 全局保留一个Create_File实例即可 private static final Create_File fileOperator = new Create_File(); static void select() throws IOException { // 把BufferedReader提到循环外创建,不要每次都关闭 try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { // 死循环保持程序运行 while(true) { System.out.println("Select Action"); System.out.println("1. Create new text file"); System.out.println("2. Delete an existing file"); System.out.println("3. Find info of a file"); System.out.println("4. Print the contents of a file"); System.out.println("5. EXIT"); System.out.println("Your choice = "); int choice = Integer.parseInt(reader.readLine()); if(choice==1) { fileOperator.create(); } else if(choice == 2){ System.out.println("enter file name to delete"); String name=reader.readLine(); fileOperator.delete(name); } else if(choice == 3) { System.out.println("enter file name to find its info"); String name= reader.readLine(); fileOperator.info(name); } else if(choice ==4) { Buffer contents = new Buffer(); Buffer.main(null); } else if(choice == 5) { System.out.println("程序退出"); System.exit(0); } else { System.out.println("无效选项,请重新输入"); } } } } }
修改后的Create_File类info方法参考
static void info(String name) throws IOException { File obj = new File("D:\\txtfiles\\"+name+".txt"); String data = null; if(obj.exists()) { System.out.println("File properties"); System.out.println("Name of the file = "+obj.getName()); System.out.println("Path of file = "+obj.getAbsolutePath()); System.out.println("Size of file in Kb= "+obj.length()/1024); System.out.println("Readable = "+obj.canRead()); System.out.println("Writable = "+obj.canWrite()); // 读取文件内容的逻辑移到存在的分支里,用try-with-resources自动关Scanner System.out.println("文件内容:"); try(Scanner scan = new Scanner(obj)) { while(scan.hasNextLine()) { data = scan.nextLine(); System.out.println(data); } } } else { System.out.println("the file does not exist"); } }
内容的提问来源于stack exchange,提问作者Tejas Gudi
相关产品推荐
相关产品推荐

