我被要求爲代碼寫評論,但我不明白setEmployeeNumber(num)在做什麼。爲什麼它沒有寫setEmployeeNumber = number呢?我的所有其他評論是否正確?在此先感謝 /** Employee類 */有人可以請解釋這部分代碼的含義是什麼? setEmployeeNumber(num)
public class Employee//creating employee class
{//declaring fields
private String name;
private String employeeNumber;
private String hireDate;
public Employee(String n, String num, String date)//construtor for Employee class with param n,num,date
{
name = n; //assigning value of n to name
setEmployeeNumber(num);//set employee
hireDate = date;//assign value of date to hireDate
}
public Employee()//non parametrized constructor
{
name = "";//set name to empty string
employeeNumber = "";//set employeeNumber to empty string
hireDate = "";//set hireDate to empty string
}
public void setName(String n)//setter with param n
{
name = n;//assign value of n to name
}
public void setEmployeeNumber(String e)//setter with param e
{
if (isValidEmpNum(e))//if string e is a valid employee number store the value of e in employee number else set employee number to empty string
employeeNumber = e;
else
employeeNumber = "";
}
public void setHireDate(String h)//setter with param h
{
hireDate = h;//assign the string h to hireDate
}
public String getName()//getter
{
return name;//return value of name
}
public String getEmployeeNumber()//getter
{
return employeeNumber;//return value of employeeNumber
}
public String getHireDate()//getter
{
return hireDate;//return value of hireDate
}
private boolean isValidEmpNum(String e)//method to return true or false if employee number is valid or invalid
{
boolean status = true;//setting default status to True
if (e.length() != 5)//if the length of string e does not equal to 5 then employee number is invalid,set status to false
status = false;
else
{
if ((!Character.isDigit(e.charAt(0))) ||//if the first char in string e is not a digit OR the 2nd char isnt a digit of the 3rd char is not a digit or the 4th char is not a dash OR the 5th char is not a letter than employee number is invalid,set status to false
(!Character.isDigit(e.charAt(1))) ||
(!Character.isDigit(e.charAt(2))) ||
(e.charAt(3) != '-') ||
(!Character.isLetter(e.charAt(4))))
status = false;
}
return status;//returing true or false
}
public String toString()//Method to returns a string representation of the object
{
String str = "Name: " + name + "\nEmployee Number: ";//making a new string called str
if (employeeNumber == "")//if employeeNuber is invalid
str += "INVALID EMPLOYEE NUMBER";//add "INVALID EMPLOYEE NUMBER" to string str
else
str += employeeNumber;//add value of employeeNumber to string str
str += ("\nHire Date: " + hireDate);//add "\nHire Date: " and value of hireDate to string str
return str;//returning the string str
}
}
您還沒有發佈'setEmployeeNumber'的代碼......但是,根據名稱判斷,它可能會將'num'分配給'employeeNumber'變量。 – Tunaki
「創建員工類」是錯誤的。 **聲明員工類別 –
作爲旁註 - 您已完成評論。把一個評論,比如'賦予n的名稱值'到一個說'name = n;'的行是完全多餘的。你的意見不應該解釋java是如何工作的,他們應該解釋代碼的邏輯 –