【问题标题】:can someone please explain what this part of the code means? setEmployeeNumber(num)有人可以解释这部分代码的含义吗? setEmployeeNumber(num)
【发布时间】: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 是如何工作的,他们应该解释代码的逻辑

标签: java class jgrasp


【解决方案1】:

你正在调用一个方法。注释应该与构造函数中的其他注释没有什么不同,但如果你想成为技术...

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
}

【讨论】:

    【解决方案2】:

    setEmployeeNumber(String e) 方法最有可能用于设置唯一的字母数字数字(包含 A-Z 和 0-9)。你只需要评论//assign employeeNumber to e

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 2021-10-24
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多