本文介绍了将来自不同类的对象添加到 Arraylist 中的问题 - java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,我有 3 个不同的类创建对象 CommissionEmployee、SalriedEmployee 和 HourlyEmployee.我需要将这些添加到主类的数组列表中,但不确定我哪里出错了.

I'm working on a project where i have 3 different classes creating objects CommissionEmployee, SalariedEmployee and HourlyEmployee. I need to add these to an arraylist in the main class, but not sure where i'm going wrong.

公开课公司{

public String companyName;
public SalariedEmployee owner;
public ArrayList<SalariedEmployee> salariedEmployee;
public ArrayList<HourlyEmployee> hourlyEmployee;
public ArrayList<CommissionEmployee> commissionEmployee;


public Company (String companyName, SalariedEmployee owner){
    this.companyName = companyName;
    this.owner = owner;
}
public void addSalariedEmployee (SalariedEmployee SalariedEmployee){
    salariedEmployee.add(SalariedEmployee); **
}

public void addHourlyEmployee (HourlyEmployee HourlyEmployee){
    //HourlyEmployee = new HourlyEmployee (name, position, ratePerHour);
    hourlyEmployee.add(HourlyEmployee);
}
    public void addCommissionEmployee (CommissionEmployee CommissionEmployee){
    //CommissionEmployee = new CommissionEmployee (,, ratePerItem);
    commissionEmployee.add(CommissionEmployee);
}

** = 这是我的编辑告诉我我出错的地方.干杯,任何帮助将不胜感激

** = this is the line where my editor is telling me i'm going wrong.Cheers, any help will be appreciated

推荐答案

因为在该行中有两个同名的变量(即 salariedEmployee).这是适合您的修复程序:

Because you have two variables with the same name in that line (i.e. salariedEmployee). Here is the fix for you:

public void addSalariedEmployee (SalariedEmployee aSalariedEmployee){
    salariedEmployee.add(aSalariedEmployee);
}

这篇关于将来自不同类的对象添加到 Arraylist 中的问题 - java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 03:06