我有一个名为EmployeeFileProcessor的公共抽象类,这也是我要实例化的类。我如何轻松地修复此代码,以便实例化此代码?

public abstract class EmployeeFileProcessor
{
    public static void main(String[] args) throws Exception
    {
        EmployeeFileProcessor process = new EmployeeFileProcessor();
        //Non important code here
        process.writeFullTimeEmployee(user_input1, FtCollection[0]);
        //Defined in another class (method writeFullTimeEmployee)
        process.readFulltimeEmployee(user_input1);
        //Defined in another class (method readFullTimeEmployee)
    }
}


我将如何使用“流程”?

最佳答案

您不能直接创建抽象类的实例。您必须首先定义一个扩展抽象类的类,然后创建该类的实例。

例如

public ConcreteEmployeeFileProcessor extends EmployeeFileProcessor {

}


然后,您可以创建ConcreteEmployeeFileProcessor的实例,但使用抽象类型对其进行引用。

EmployeeFileProcessor instance = new ConcreteEmployeeFileProcessor();

08-04 18:32