【发布时间】:2016-06-02 10:43:05
【问题描述】:
我被要求为代码编写 cmets,但我不明白 setEmployeeNumber(num) 行在做什么。为什么不写成 setEmployeeNumber= number?我的所有其他 cmets 都对吗?提前致谢 /** 员工类 */
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变量。 -
“创建员工类”是错误的。它是声明员工类
-
作为旁注 - 你已经过度评论了。在写有
name = n;的行中添加诸如“将n 的值分配给名称”之类的注释是完全多余的。你的 cmets 不应该解释 java 是如何工作的,他们应该解释代码的逻辑