Java 我可以用对象列表作为轴心方式表示POJO吗
关于我的POJO实现Java 我可以用对象列表作为轴心方式表示POJO吗,java,Java,关于我的POJO实现 Class Employee { Integer empId; String empName; List<Allowance> allowances; List<Deduction> deductions; //getter and setter public Employee(Integer empId, String empName, List<Allowance> allowances, List&
Class Employee {
Integer empId;
String empName;
List<Allowance> allowances;
List<Deduction> deductions;
//getter and setter
public Employee(Integer empId, String empName,
List<Allowance> allowances, List<Deduction> deductions) {
this.empId = empId;
this.empName = empName;
this.allowances = allowances;
this.deductions = deductions;
}
}
Class Allowance {
Integer allwId;
Employee emp;
String allwName; // Basic Pay, DA, HRA etc..
float amount;
//getter and setter
}
Class Deduction {
Integer dedId;
Employee emp;
String dedName; // FBS, Loan, Tax etc..
float amount;
//getter and setter
}
例如,我增加了2名员工。
列表中所有WS1的金额为基本工资,DA、HRA和deds1的金额为FBS、税收。
列表中所有WS2包含基本工资金额,DA和deds2包含FBS、贷款、税收金额。
给这两名员工分配如下的任务:
Employee emp1=new Employee(1,"Mickel", allws1, deds1);
Employee emp2=new Employee(2,"John", allws2, deds2);
List<Employee> employees = new ArrayList<Employee>();
employees.add(emp1); employees.add(emp2);
我能用这样的方式代表员工名单吗
|empName |基本工资| DA | HRA | FBS |贷款|税|
|米克尔| 1400 | 120 | 50 | 20 | 0 | 27|
|约翰| 1200 | 120 | 0 | 20 | 105 | 23|
您已经在数据对象容差和扣减范围内收集了数据。你需要的是对它的不同看法。因此,您可以将它们放在JTable中,或者简单地以所需的格式将其打印到控制台 如果我正确理解了您的问题,您希望以特定的方式输出员工列表的元素 为此,可以重写Employee类中的方法 此方法返回对象的字符串表示形式 比如说,
@Override
public String toString() {
String bp = ""; // get the BP
String da = ""; // get the DA
// etc...
return String.format("|%s|%s|%s|%s|%s|%s|%s|", this.empId, bp, da, ...); // this line won't compile
}
之后,您可以实现另一种方法,该方法将输出带有标题的员工列表。如果现有问题没有揭示您需要的信息,那么您可以对相应的答案发表评论。否则,将答案标记为已接受。