我可以为动态数组列表 super class 类型的对象创建一个 subclass 对象吗?

Can i create a subclass object for a dynamic Array List super class type object?

如果我想使用正在初始化 class Worker 的动态数组列表,我可以添加扩展 Worker class 的子classes 并尝试用数据填充它们就像下面的测试 class?.. 每当我尝试调用子 classes 之一的某个函数时我得到一个错误,我需要调用这些函数所以我怎样才能正确地做到这一点?

    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();
        }
    }

您收到错误是因为 Worker class 没有 getSalary() 方法。

您需要将列表中的对象转换为适当的子class类型。

例如:

SalariedWorker sw = (SalariedWorker) Worker1.get(0);
sw.getSalary(salary);

以下代码片段的问题是:

ArrayList<Worker> Worker1 = new ArrayList<Worker>();
Worker1.get(0).getSalary(salary);

get 将 return 你 abstraction 没有 getSalary(salary) 方法。您所要做的就是使用各自的实现 class 进行转换,并在其上调用方法。 例如:

SalariedWorker sw = (SalariedWorker)Worker1.get(0);