public static void main (String[] args)
{
SalariedEmployee se = new SalariedEmployee();
HourlyEmployee he = new HourlyEmployee();
se.setName("Simos");
se.setAfm("111440000");
se.setSalary(4500);
he.setName("Chatzis");
he.setAfm("011155555");
he.setHoursWorked(200);
he.setHourlyPayment(25);
ArrayList list = new ArrayList();
list.add(se);
list.add(he);
}
所以我有两个不同类型的对象,我想将它们添加到列表中。我怎样才能使它在编译器方面安全。由于对象是从不同的类创建的,所以在制作列表时我不能使用泛型。或者我可以在完成后更改列表的类型。我的意思是我可以拥有这个
ArrayList<SalariedEmployee> list = new ArrayList<SalariedEmployee>();
添加SalariedEmployee的“se”对象,然后将泛型更改为HourlyEmployee,然后添加HourlyEmployee的“he”对象?
答案 0 :(得分:5)
看起来您可以创建公共接口Employee
并使两个类都实现它。所以你将能够使用泛型:
List<Employee> list = new ArrayList<Employee>()
;
答案 1 :(得分:2)
创建界面:
Employee.java
public interface Employee {
public void setName(String name);
public String getName();
public void setAfm(String afm);
public String getAfm();
}
然后创建实现它的两个类,添加它们的独特方法:
HourlyEmployee.java
public class HourlyEmployee implements Employee {
private String name;
private String afm;
private int hoursWorked;
private int hourlyPayment;
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public void setAfm(String afm) {
this.afm = afm;
}
@Override
public String getAfm() {
return this.afm;
}
public int getHoursWorked() {
return this.hoursWorked;
}
public void setHoursWorked(int hoursWorked) {
this.hoursWorked = hoursWorked;
}
public int getHourlyPayment() {
return this.hourlyPayment;
}
public void setHourlyPayment(int hourlyPayment) {
this.hourlyPayment = hourlyPayment;
}
}
SalariedEmployee.java
public class SalariedEmployee implements Employee {
private String name;
private String afm;
private int salary;
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public void setAfm(String afm) {
this.afm = afm;
}
@Override
public String getAfm() {
return this.afm;
}
public int getSalary() {
return this.salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
然后测试:
import java.util.ArrayList;
public class TestMain {
public static void main(String[] args){
SalariedEmployee se = new SalariedEmployee();
HourlyEmployee he = new HourlyEmployee();
se.setName("Simos");
se.setAfm("111440000");
se.setSalary(4500);
he.setName("Chatzis");
he.setAfm("011155555");
he.setHoursWorked(200);
he.setHourlyPayment(25);
ArrayList<Employee> list = new ArrayList<Employee>();
list.add(se);
list.add(he);
}
}
答案 2 :(得分:1)
创建一个这两个可以继承的Employee类,并在ArrayList
中使用它public abstract class Employee
{
public string setName(string name) { ... }
public string setAfm(string afm) { ... }
public int setSalary(float afm) { ... }
}
public class HourlyEmployee extends Employee
{
}
public class SalariedEmployee extends Employee
{
}
ArrayList<Employee> employees;
这也意味着您不必为每个员工类重写您的setter方法
答案 3 :(得分:0)
为什么不做一个超级班&#34;员工&#34;并使两个对象成为员工的子女:
class Employee { }
class SalariedEmployee extends Employee { }
class HourlyEmployee extends Employee { }
Employee salariedEmp = new SalariedEmployee();
Employee hourlyEmp = new HourlyEmployee();
然后创建一个Employee类型的数组列表:
ArrayList<Employee> list = new <Employee>ArrayList();
答案 4 :(得分:-3)
如下所示初始化添加到列表。
> List<Object> list = new <Object>ArrayList();
要稍后从列表中读取,您可以使用instanceof
并转换为代码中的已知类型。