有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java对象第二名员工不在

我写了一些代码,我需要一些帮助 这个节目是关于公司的

这是一个主要问题:

Employee e1=new Employee (43208931,"Zainab","Directer","14/5/1990",15000);
    Employee e2=new Employee (43208932,"Ahmed","Maneger","14/5/1991",18000);
    Employee e3=new Employee (43208932,"Talal","Engeniar","14/5/1989",1000);
    Employee e4=new Employee (43208931,"khaled","software Engeniar","14/5/1978",6000);
    Employee e5=new Employee (43208931,"Mohammed","Director","14/5/1978",9000);
    Employee e6=new Employee (43208931,"Jalel","software Designer","14/5/1978",8000);


     Department d1;
     Department d2;
     Department d3;

    d1 = new Department (e1 , e3 , "Jeddah Branch");
    d2 = new Department (e2 , e4 ,"Hail Branch");
    d3 = new Department (e5 , e6 ,"Musqet Branch"); 

     Project p1=new Project (d3,"Bank System");
     Project p2 = new Project (d2,"Employee System");
     Project p3=new Project (d1,"Student System");
     System.out.println(p1.toString());
     System.out.println(p2.toString());
     System.out.println(p3.toString());

这是班级雇员:`公共班级雇员'{ 私人长ID; 私有字符串名; 私人字符串位置; 私人字符串出生日期; 私人双薪

Employee(){

}
public Employee(long ID, String Name, String position, String DateOfBirth, double Salary) {
    this.ID = ID;
    this.Name = Name;
    this.position = position;
    this.DateOfBirth = DateOfBirth;
    this.Salary = Salary;
}

public long getID() {
    return ID;
}

public String getName() {
    return Name;
}

public String getPosition() {
    return position;
}

public String getDateOfBirth() {
    return DateOfBirth;
}

public double getSalary() {
    return Salary;
}

@Override
public String toString() {
    return "Employee{" + "ID=" + ID + ", Name=" + Name + ", position=" + position + ", DateOfBirth=" + DateOfBirth + ", Salary=" + Salary + '}';
}


`

这是班级部门:`公共班级部门扩展员工{ 员工; 私有字符串DName

Department(){

}



public Department(Employee Employee,Employee E, String DName) {
    this.Employee = E;
    this.Employee = Employee;
    this.DName = DName;

}

public Department(Employee Employee, Employee E,String DName, long ID, String Name, String position, String DateOfBirth, double Salary) {
    super(ID, Name, position, DateOfBirth, Salary);
    this.Employee = Employee;
    this.DName = DName;

}



 public Employee getEmployee(){
     return Employee;}

 public String getDName(){
     return DName;
 }

@Override
public String toString() {
    return "\n Department:{" + DName + " }" +Employee +  '}';
}

}`

最后是班级项目:`公共班级项目扩展部门{

Department Department ;
String projecttName;
Project(){

}

public Project(Department Department, String DepartmentName) {
    this.Department = Department;
    this.projecttName = DepartmentName;
}

public Project(Department Department, String DepartmentName, String DName) {
    this.Department = Department;
    this.projecttName = DepartmentName;
}

public Project(Department Department, String DepartmentName, commpany.Employee Employee, String DName, long ID, String Name, String position, String DateOfBirth, double Salary) {
    super(Employee,Employee , DName, ID, Name, position, DateOfBirth, Salary);
    this.Department = Department;
    this.projecttName = DepartmentName;
}

public Department getDepartment() {
    return Department;
}

public String getDepartmentName() {
    return projecttName;
}

@Override
public String toString() {
    return "\n Project :" +projecttName+" {" + Department + '}';
}

} ` 我的问题是:同一部门的第二名员工没有出现在报纸上>>; 有什么问题吗


共 (2) 个答案

  1. # 1 楼答案

    您将两名员工分配给同一个变量,这意味着一名员工被丢弃

    this.Employee = E;
    this.Employee = Employee;
    

    如果你想让两个员工都是同一个人,你必须把他们都保存起来,并在toString()中打印出来

    提示:如果你想知道如何做到这一点,我建议你使用IDE来生成这段代码。你不必自己写

  2. # 2 楼答案

    Department类中,您只有一个Employee。要有多个Employee,应该使用List

    private List<Employee> employees = new ArrayList<Employee>();
    
    public Department(Employee Employee, Employee E, String DName) {
        this.employees.add(E);
        this.employees.add(Employee);
        this.DName = DName;
    }
    
    @Override
    public String toString() {
        return "\n Department:{" + DName + " }" + employees + '}';
    }