Problems adding objects from a different class int

2019-09-21 08:27发布

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 class Company {

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

4条回答
疯言疯语
2楼-- · 2019-09-21 08:55

you are using class name as parameter name. it should be different case or any other name instead of class name. but it is better to have camel case name.

just example:

public void addSalariedEmployee (SalariedEmployee salariedEmployee)
查看更多
劫难
3楼-- · 2019-09-21 09:04

You've attempted to name the parameter the same as your class name. A class name is not acceptable as a parameter name. Name the parameter something different. Even something of different case would be good, e.g.:

public void addSalariedEmployee (SalariedEmployee salariedEmployee){
查看更多
戒情不戒烟
4楼-- · 2019-09-21 09:07

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); 
}
查看更多
【Aperson】
5楼-- · 2019-09-21 09:10

Your method parameter is SINGLE OBJECT. You are trying to put a LIST not an object. You have to change your method parameter

When youll changed, here's an example to deal with the iterator objects.

 public static void main(String[] args) {
    Curso curso=new Curso();
    Evento evento=new Evento();
    Publicacion publicacion=new Publicacion();

    List objectList = new ArrayList();

    objectList.add(curso);
    objectList.add(evento);
    objectList.add(publicacion);

    for(Object o:objectList){
        if(o instanceof Curso){
            //do some thing
        }
        if(o instanceof Evento){
            //do some thing
        }
        if(o instanceof Publicacion){
            //do some thing
        }
    }
}

Remember that JAVA works with objects and everyting its a object. Regards

查看更多
登录 后发表回答