未填充以下子类对象的抽象对象数组

时间:2017-05-26 07:22:15

标签: java arrays abstract aggregation

我有两种方法可以从文件和抽象对象数组中读取进程对象。这两个方法从文件数据创建作为抽象类的子类的对象。

当我尝试填充此数组时,它会填充空值,我不确定为什么。如果我首先使用默认构造函数初始化新的子类对象,它将显示默认对象,但不显示从其他两个方法创建的对象

private static NonMetal processNonMetal (String line, int numEle)
{
    NonMetal nonMetalElement = new NonMetal();
    String[] lineArray = line.split("[^a-zA-Z0-9.]");
    nonMetalElement.setSymbol(lineArray[0+ 5 * numEle]);
    nonMetalElement.setName(lineArray[1 + 5 * numEle]);
    nonMetalElement.setAtomNum(Integer.parseInt(lineArray[2 + 5 * numEle]));
    nonMetalElement.setMass(Double.parseDouble(lineArray[3 + 5 * numEle]));
    char state = lineArray[4 + 5 * numEle].charAt(0);
    nonMetalElement.setState(state);
    return nonMetalElement;
}

private static Metal processMetal (String line, int numEle)
{
    Metal metalElement = new Metal();
    String[] lineArray = line.split("[a-zA-Z0-9.]");
    metalElement.setSymbol(lineArray[0 + 5 * numEle]);
    metalElement.setName(lineArray[1 + 5 * numEle]);
    metalElement.setAtomNum(Integer.parseInt(lineArray[2 + 5 * numEle]));
    metalElement.setMass(Double.parseDouble(lineArray[3 + 5 * numEle]));
    metalElement.setConduct(Double.parseDouble(lineArray[4 + 5 * numEle]));
    return metalElement;
}

填充数组的代码

public static void readValues(String fileName)
{
    if (!constructed)
    {
        FileInputStream fileStrm = null;
        InputStreamReader rdr;
        BufferedReader bufRdr;
        String line;
        int lineNum;
        try
        {
            fileStrm = new FileInputStream(fileName);
            rdr = new InputStreamReader(fileStrm);
            bufRdr = new BufferedReader(rdr);
            lineNum = 0;
            line = bufRdr.readLine();
            while(line != null)
            {

                count = numEle(line);
                for (int i = 0; i < count; i++)
                {
                    try
                    {
                        lineNum++;
                        if (isMetal(line, i))
                        {
                            elements[lineNum - 1] = new Metal(processMetal(line, i));
                        }
                        else
                        {
                            elements[lineNum - 1] = new NonMetal(processNonMetal(line, i));
                        }

                    }
                    catch(NumberFormatException e)
                    {
                    }

                }
                line = bufRdr.readLine();

            }
            fileStrm.close();
        }
        catch(IOException e)
        {
            if(fileStrm != null)
            {
                try
                {
                    fileStrm.close();

                    constructed = true;
                }
                catch (IOException ex2)
                {
                }
            }
            System.out.print("error in file processing: " + e.getMessage());
        }
        for (int i = 0; i < elements.length; i++)
        {
            System.out.println(elements[i]);
        }
    }
}

0 个答案:

没有答案