如果我想使用正在初始化类的动态数据列表工作者可以添加扩展Worker类的子类并尝试用下面的测试类填充数据吗?每当我尝试调用某个函数时其中一个子类我得到一个错误,我需要调用这些函数,所以我怎么能正确地做到这一点?
public class Worker extends Person {
private int id;
Worker() {
}
Worker(int i) {
id = i;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String toString() {
return super.toString() + "ID: " + id + " ";
}
}
public class HourlyWorker extends Worker implements Annual {
private double rate;
private double AnnualPayment;
private double percentageIncrease;
HourlyWorker() {
}
HourlyWorker(double r) {
rate = r;
}
public double getAnnualPay(double Annualpayment) {
return Annualpayment = rate * 52 * 40;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getAnnualPayment() {
return AnnualPayment;
}
public void setAnnualPayment(double AnnualPayment) {
this.AnnualPayment = AnnualPayment;
}
public double getpercentageIncrease() {
return percentageIncrease;
}
public void setpercentageIncrease(double percentageIncrease) {
this.percentageIncrease = percentageIncrease;
}
public void increasePayment(double r) {
increasePayR(r);
}
public double increasePayR(double r) {
return rate = (AnnualPayment + getAnnualPay(r) * percentageIncrease) / 2080;
}
public String toString() {
return "Your rate : " + rate + " ";
}
}
public class SalariedWorker extends Worker implements Annual {
private double salary;
private double AnnualPayment;
private double percentageIncrease;
SalariedWorker() {
}
SalariedWorker(double s) {
salary = s;
}
public double getAnnualPay(double Annualpayment) {
return Annualpayment = salary * 12;
}
public void increasePayment(double r) {
increasePayS(r);
}
public double increasePayS(double r) {
return salary = (AnnualPayment + getAnnualPay(r) * percentageIncrease) / 12;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getAnnualPayment() {
return AnnualPayment;
}
public void setAnnualPayment(double AnnualPayment) {
this.AnnualPayment = AnnualPayment;
}
public double getpercentageIncrease() {
return percentageIncrease;
}
public void setpercentageIncrease(double percentageIncrease) {
this.percentageIncrease = percentageIncrease;
}
public String toString() {
return " ";
}
}
public class Test {
public static void main(String[] args) {
Scanner prompt = new Scanner(System.in);
ArrayList<Worker> Worker1 = new ArrayList<Worker>();
Worker1.add(new SalariedWorker());// is it alright to create a subclass object here?
Worker1.add(new SalariedWorker(1000.0));// index 1
Worker1.add(new HourlyWorker());// index 2
Worker1.add(new HourlyWorker(100.0));// index 3
System.out.println("Enter your monthly salary: ");
double salary = prompt.nextDouble();
Worker1.get(0).getSalary(salary);//gets me an error
System.out.println("Enter your hourly rate: ");
double HourlyRate = prompt.nextDouble();
System.out.println("Enter the Percentage Increase for a Salaried worker: ");
double PercentIncreaseS = prompt.nextDouble();
Worker1.get(0).getpercentageIncrease(PercentIncreaseS);//gets me an error
System.out.println("Your Increased payment is: ");
System.out.println("Enter the Percentage Increase for an Hourly worker: ");
double PercentIncreaseH = prompt.nextDouble();
}
}
答案 0 :(得分:1)
您收到错误是因为20
类没有Worker
方法。
您需要将列表中的对象强制转换为适当的子类类型。
例如:
getSalary()
答案 1 :(得分:0)
以下代码片段的问题是:
ArrayList<Worker> Worker1 = new ArrayList<Worker>();
Worker1.get(0).getSalary(salary);
get会返回没有abstraction
方法的getSalary(salary)
。您所要做的就是使用相应的实现类转换每个实现类并在其上调用方法。
例如:
SalariedWorker sw = (SalariedWorker)Worker1.get(0);