我被要求为代码编写注释但我不明白行setEmployeeNumber(num)正在做什么。为什么不写成setEmployeeNumber = number?我的所有其他注释是否正确?提前致谢 / ** 员工类 * /
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
}
}
答案 0 :(得分:0)
您正在调用方法。注释应该与构造函数中的其他注释没有什么不同,但是如果你想成为技术人员......
setEmployeeNumber(num); // calls the method that sets the employee number to the value of num
假设您的代码中有某处
public void setEmployeeNumber(String num) //setter with param num
{
employeeNumber= num; //assign value of num to employeeNumber
}
答案 1 :(得分:0)
方法setEmployeeNumber(String e)
最有可能用于设置唯一字母数字(包含A-Z& 0-9)。你要评论的就像//assign employeeNumber to e
。