Java 基于CSV文件中的变量创建对象

Java 基于CSV文件中的变量创建对象,java,oop,Java,Oop,前言:这是一项作业,所以我有用户名和密码 CSV文件中的纯文本密码 以下是我得到的: 用户名、密码、员工类型作为我的数据文件 后者是员工的E,经理的M或承包商的C。其中每一个都由它们自己的类和方法表示 我实现了一个功能,可以验证用户输入的用户名/密码。我刚才一直在研究如何基于上一个staff类型值创建对象 起初,我天真的解决方案是做如下事情: if (staffType.equals("E")) { Employee user = new Employee(); else if (staff

前言:这是一项作业,所以我有用户名和密码 CSV文件中的纯文本密码

以下是我得到的:
用户名、密码、员工类型
作为我的数据文件

后者是
员工的
E
经理的
M
承包商的
C
。其中每一个都由它们自己的类和方法表示

我实现了一个功能,可以验证用户输入的用户名/密码。我刚才一直在研究如何基于上一个
staff类型
值创建对象

起初,我天真的解决方案是做如下事情:

if (staffType.equals("E")) {
  Employee user = new Employee();
else if (staffType.equals("C")) {
  Contractor user = new Contractor();
else if (staffType.equals("M")) {
  Manager user = new Manager();
}
然而,我随后尝试将其包装在一个方法中,但我仍然坚持使用什么作为返回类型。我已经从Java和OO中断了一年,所以不幸的是,多态性、继承、泛型方法等概念对我来说都是模糊的

显然,鉴于这是一项任务,我不想要一个完整的实现,只是一个正确方向的提示


谢谢大家

您必须创建一个层次结构,如。例如,经理和承包商都是雇员,因此他们必须扩展Employee类。 大概是这样的:

class Employee

class Manager extends Employee

class Contractor extends Employee

这样,您可以将返回值指定为Employee,并返回Employee或其子类型之一。

您必须创建一个层次结构,如。例如,经理和承包商都是雇员,因此他们必须扩展Employee类。 大概是这样的:

class Employee

class Manager extends Employee

class Contractor extends Employee

这样,您可以将返回值指定为Employee,并返回Employee或其子类型之一。

您必须创建一个层次结构,如。例如,经理和承包商都是雇员,因此他们必须扩展Employee类。 大概是这样的:

class Employee

class Manager extends Employee

class Contractor extends Employee

这样,您可以将返回值指定为Employee,并返回Employee或其子类型之一。

您必须创建一个层次结构,如。例如,经理和承包商都是雇员,因此他们必须扩展Employee类。 大概是这样的:

class Employee

class Manager extends Employee

class Contractor extends Employee

通过这种方式,您可以将返回值指定为Employee,并返回Employee或其子类型之一。

如果在这些类中发现
是一种关系(例如Manager是Employee),则必须使用继承来实现它们:

Class Employee
Class Manager extends Employee
此解决方案中的返回类型为
Employee

public Employee create(String staffType) {
    Employee user = null;
    if (staffType.equals("E")) {
        user = new Employee();
    else if (staffType.equals("M")) {
        user = new Manager();
    }    
    ....
    return user;
}
否则,您可以使用接口作为返回类型:

Class Employee implements Createable
Class Manager  implements Createable
Employee
Manager
的所有常用方法都必须在
Createable
中定义

interface Createable {
    void method1()
    ....
}
然后:

public Employee create(String staffType) {
    Creatable user = null;
    if (staffType.equals("E")) {
        user = new Employee();
    else if (staffType.equals("M")) {
        user = new Manager();
    }    
    ....
    return user;
}

如果在这些类中发现一个
是一个
关系(例如经理是雇员),则必须使用继承来实现它们:

Class Employee
Class Manager extends Employee
此解决方案中的返回类型为
Employee

public Employee create(String staffType) {
    Employee user = null;
    if (staffType.equals("E")) {
        user = new Employee();
    else if (staffType.equals("M")) {
        user = new Manager();
    }    
    ....
    return user;
}
否则,您可以使用接口作为返回类型:

Class Employee implements Createable
Class Manager  implements Createable
Employee
Manager
的所有常用方法都必须在
Createable
中定义

interface Createable {
    void method1()
    ....
}
然后:

public Employee create(String staffType) {
    Creatable user = null;
    if (staffType.equals("E")) {
        user = new Employee();
    else if (staffType.equals("M")) {
        user = new Manager();
    }    
    ....
    return user;
}

如果在这些类中发现一个
是一个
关系(例如经理是雇员),则必须使用继承来实现它们:

Class Employee
Class Manager extends Employee
此解决方案中的返回类型为
Employee

public Employee create(String staffType) {
    Employee user = null;
    if (staffType.equals("E")) {
        user = new Employee();
    else if (staffType.equals("M")) {
        user = new Manager();
    }    
    ....
    return user;
}
否则,您可以使用接口作为返回类型:

Class Employee implements Createable
Class Manager  implements Createable
Employee
Manager
的所有常用方法都必须在
Createable
中定义

interface Createable {
    void method1()
    ....
}
然后:

public Employee create(String staffType) {
    Creatable user = null;
    if (staffType.equals("E")) {
        user = new Employee();
    else if (staffType.equals("M")) {
        user = new Manager();
    }    
    ....
    return user;
}

如果在这些类中发现一个
是一个
关系(例如经理是雇员),则必须使用继承来实现它们:

Class Employee
Class Manager extends Employee
此解决方案中的返回类型为
Employee

public Employee create(String staffType) {
    Employee user = null;
    if (staffType.equals("E")) {
        user = new Employee();
    else if (staffType.equals("M")) {
        user = new Manager();
    }    
    ....
    return user;
}
否则,您可以使用接口作为返回类型:

Class Employee implements Createable
Class Manager  implements Createable
Employee
Manager
的所有常用方法都必须在
Createable
中定义

interface Createable {
    void method1()
    ....
}
然后:

public Employee create(String staffType) {
    Creatable user = null;
    if (staffType.equals("E")) {
        user = new Employee();
    else if (staffType.equals("M")) {
        user = new Manager();
    }    
    ....
    return user;
}