Java 有人能解释一下这部分代码是什么意思吗?setEmployeeNumber(num)
我被要求为代码编写注释,但我不清楚setEmployeeNumber(num)行到底在做什么。为什么不改为写setEmployeeNumber=number?我的其他注释都对吗?提前谢谢 /** 雇员阶级 */Java 有人能解释一下这部分代码是什么意思吗?setEmployeeNumber(num),java,class,jgrasp,Java,Class,Jgrasp,我被要求为代码编写注释,但我不清楚setEmployeeNumber(num)行到底在做什么。为什么不改为写setEmployeeNumber=number?我的其他注释都对吗?提前谢谢 /** 雇员阶级 */ public class Employee//creating employee class {//declaring fields private String name; private String employeeNumber
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
}
您正在调用一个方法。注释应该与构造函数中的其他注释没有什么不同,但是如果您想成为技术性的注释
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
}
方法
setEmployeeNumber(字符串e)
最有可能用于设置唯一的字母数字(包含a-Z&0-9)。您只需评论一下//将employeeNumber分配给e
您还没有发布setEmployeeNumber
的代码。。。但是,根据名称判断,它可能将num
赋值给employeeNumber
变量。“创建employee类”是错误的。它将员工类声明为旁注-您的评论已经过多了。将类似“将n的值赋给name”的注释放在一行中,该行表示name=n代码>是完全冗余的。您的注释不应该解释java是如何工作的,它们应该解释代码的逻辑
public void setEmployeeNumber(String num) //setter with param num
{
employeeNumber= num; //assign value of num to employeeNumber
}