我写了一些代码,我想要一些帮助 关于公司的这个程序
这是一个主要的:
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());
这是类Employee:`public class Employee { 私人长身份证; 私有字符串名称; 私人字符串位置; private String DateOfBirth; 私人双薪;
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 + '}';
}
`
这是班级部门:`公共班级部门扩展员工{ 员工员工; private String 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 + '}';
}
}`
最后这是Class Project:`public class Project extends Department {
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 + '}';
}
} ` 我的问题是:同一部门的第二名员工不是在印刷品>> 这个问题是什么?
答案 0 :(得分:0)
您将两名员工分配到同一个变量,意味着一个被丢弃
this.Employee = E;
this.Employee = Employee;
如果你想要同样的两个员工,你必须保存它们并在toString()中打印它们。
提示:如果您想知道如何执行此操作,建议您使用IDE生成此代码。你不必自己写。 ;)
答案 1 :(得分:0)
你的问题在这里
public class Department extends Employee {
Employee Employee;
private String DName;
Department(){
}
public Department(Employee Employee,Employee E, String DName) {
this.Employee = E;
this.Employee = Employee;
this.DName = DName;
}
您只有一个实例字段Employee Employee
(请注意,变量名称以小写或下划线开头)。
执行此代码时
this.Employee = E;
this.Employee = Employee;
this.Employee
换言之,您的私有变量只是被重写
解决方法是使用:
两个实例变量Employee e1; Employee e2
然后你的构造函数看起来像
public Department(Employee e1,Employee e2, String DName) {
this.e1 = e1;
this.e2 = e2;
this.DName = DName;
}
此外,如果您想将两名以上的员工添加到特定部门,请使用List<Employee > myList
或array
答案 2 :(得分:0)
在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 + '}';
}