我有這個作業要求,並且我已經爲它寫了一個解決方案。但不確定我是否有正確的答案。代碼工作和編譯。Java代碼有效,但不確定是否滿足要求(對象和類)
,這是我的java文件(2文件)
首先是主文件
public class Employee
{
String name;
int idNumber;
String department;
String position;
public Employee(String emName, int idNum, String depart, String pos)
{
name = emName;
idNumber = idNum;
department= depart;
position = pos;
}
public Employee(String emName, int idNum)
{
name = emName;
idNumber = idNum;
department= "";
position = "";
}
public Employee()
{
name = "";
idNumber = 0;
department= "";
position = "";
}
//set name
public void setName(String nameSet)
{
name = nameSet;
}
//get name
public String getName()
{
String nameGet = name;
return nameGet;
}
//set idnum
public void setID(int idSet)
{
idNumber = idSet;
}
//get idnum
public int getId()
{
int idGet = idNumber;
return idGet;
}
//
//set department
public void setDep(String depSet)
{
department = depSet;
}
//get department
public String getDep()
{
String depGet = department;
return depGet;
}
//
//set position
public void setPos(String posSet)
{
position = posSet;
}
//get position
public String getPos()
{
String posGet = position;
return posGet;
}
}
其次增加了數據和打印數據
public class Employee_test
{
public static void main(String[] args)
{
Employee employee1 = new Employee("Susan Meyers", 47899, "Accounting", "Vice President");
Employee employee2 = new Employee("Mark Jones", 39119, "IT", "Programmer"); //use second with Mark
//Employee employee2 = new Employee("Mark Jones", 39119);
Employee employee3 = new Employee("Joy Rogers", 81774, "Manufacturing", "Engineer"); //use third with Joy
//Employee employee3 = new Employee();
System.out.println("Name ID Number Department Position");
System.out.println("");
System.out.println(employee1.getName() + " " + employee1.getId() + " " + employee1.getDep() + " " + employee1.getPos());
System.out.println(employee2.getName() + " " + employee2.getId() + " " + employee2.getDep() + " " + employee2.getPos());
System.out.println(employee3.getName() + " " + employee3.getId() + " " + employee3.getDep() + " " + employee3.getPos());
System.out.println("");
}
}
這裏的當我運行employee_test文件時輸出。
我是什麼的困惑。
問題說
Make sure you use all three constructors. Use the first constructor with Susan, the second with Mark and the third one with Joy.
如何,如果這個問題想二號員工使用一個構造函數,只有(姓名,身份證號碼)和第三一副事()的輸出來實現。 在此先感謝。
通過在調用構造函數後使用setName,setID等函數。它們的存在是有原因的,那就是有可能的分配 –
的目的,基本上他們是想你使用'setter'方法與第二和第三Employee對象 –
@JacobH - 所以我應該使用兩個構造函數。然後使用setName,setID,setDep和setPos來實現結果? –