Java - 使用String作为名称创建类的实例

时间:2015-03-21 12:25:12

标签: java object reflection instances

我想创建一些类的实例,就像:

Country ger = new Country();
Country usa = new Country();
...

等等。因为我想为大量对象执行此操作,所以我宁愿使用文本文件中的实例名创建这些实例,其中列出了这些国家/地区标记并遍历此列表。我熟悉Java反射概念,但是我不想为每个对象创建一个单独的类,因为我只想简短地删除这些声明。

有没有办法做到这一点?

提前致谢

2 个答案:

答案 0 :(得分:1)

将名称变量添加到Country并通过构造函数初始化它。或者使用地图。

答案 1 :(得分:1)

首先让我们关注你的国家级。 根本问题在于,您希望创建Country对象,其中每个对象代表一个国家/地区。为此,您需要一个Country类中的构造函数,它将String作为参数,以便在您的Main程序中执行:

Country ger = new Country("Germany");
Country usa = new Country("USA");

但要做到这一点,首先需要确保类Country有一个实例变量,可以存储每个对象的国家/地区。所以Country类需要至少包含以下内容:

public class Country {

    //Each object holds a variable called country, which is the name of the country
    private String country;

    //This constructor takes the name you enter in brackets and sets country to this name
    public Country(String name) {
        country = name;
    }
}

现在我们已准备好制作代表不同国家/地区的国家/地区对象。 由于您要从文本文件(.txt)输入国家/地区,因此您需要制作一些工具来处理相关文件。为简单起见,我将假设文本文件只有国家名称用新行分隔。 您可以扩展您的类Country以包含主要方法以及一些相关导入:

 import java.io.*;
 import java.util.Scanner;     

 public class Country {

    //Each object holds a variable called country, which is the name of the country
    private String country;

    //This constructor takes the name you enter in brackets and sets country to this name
    public Country(String name) {
        country = name;
    }

    public static void main(String[] args) {
        //This represents the file you want to access
        File countryFile = new File("path-of-text-file-goes-here");
        Scanner input = null;  //this tool will allow you to read from the file

        try {
            //we must initialize the file read tool in a try block
            input = new Scanner( new FileInputStream(countryFile) );
        }
        catch (FileNotFoundException e) {
            System.out.println("Error accessing the file. Exiting...");
            System.exit(0);
        }

        //initialize an empty String and we will put all the countries in the file
        //into our program by first putting it in a String separated by spaces
        String inputText = "";
        while (input.hasNext())  //tests to see whether input has a word left
            inputText += input.next() + " "; //if so, add it to the String and add a space

        //Here we are creating an array of Strings, where each indice is one word from inputText
        // the split(" ") method returns an array of characters separated by spaces
        String[] inputTextArray = inputText.split(" ");

        //initialize a Country array to the length of words in inputText
        Country[] countryObject = new Country[inputTextArray.length];

        //for each word in inputTextArray, initialize each indice in countryObject
        //with a new Country("each-indice-of-inputTextArray-will-be-placed-here-one-by-one")
        for (int i = 0; i < countryObject.length; i++)
            countryObject[i] = new Country(inputTextArray[i]);

    }
}

现在,数组countryObject应填充Country对象,其中每个对象都是您在文本文件中键入的国家/地区。 希望这有帮助,如果您不清楚或者我是否误解了这个问题,请告诉我。谢谢!