在java中创建一个虚拟对象

时间:2013-11-26 17:13:23

标签: java object

我正在研究一个有两个类的java程序。一个名为employee的类具有声明和名称,年龄和员工编号等验证。在我的主要课程中,我想单独验证员工编号和编号。我如何创建一个虚拟对象(或一些人称为虚拟访问器)来获取它?这是我的程序通常的样子:

package empdatabase;

public class empdatabase

{
public static employee [] emp = new employee[50];
public static int count = 0;

public static void main(String[] args) {


     }//end class main


 public static void usermenu()
 {
    String input = new String("");
    int choice = 0;

    input = JOptionPane.showInputDialog("user menu"+"\n 1. employee registration"
                                      + "\n 2. employee login")

    choice = Integer.parseInt(input);

    switch(choice)

    {

     case 1:
     if (count <50){
        emp[count] = new employee();
        emp[count].getemployee();


        emp[count].disp();

        count++;
        //usermenu();
        }//end if statment
                    break;

     case 2:
                   emplogin();
                   break;

     default:

                   JOptionPane.showMessageDialog("error, not a valid choice");


     }//end usermenu


     public static void emplogin()
     {
        String input = new String("");
        input = JOptionPane.showInputDialog("enter your employee ID");

        //dummy object goes here


      }//end login


 }//end empdatabase class



class employee{


     String empNumber;
  String First;
 String Last;
 int age;


employee() 
{
     empNumber = "";
 First = "";
 Last = "";
     age = 0;



}//end employee constructor



boolean ValidateLetter(String input)
            {
    for(int i = 0; i < input.length(); i++)
    { 

        if( !Character.isLetter(input.charAt(i)))
        { 
            return false;
        }//end if Char...


    }//end while i for loop
return true;


}//end ValidateLetter


    boolean checkNumber(String input)
{
    int i = 0;
    while( i < 9)
    {
             if(!Character.isDigit(input.charAt(i)))
                 return false;
             i++;
    }//end while
    return true;

}//end check number

    void disp()
{
    JOptionPane.showMessageDialog(null, "employee number: "+ empNumber+
                                          "\n name: " + first+ " "+ last);
}//end disp

}//end class employee

这只是骨头,但它应该让你知道我需要什么。

1 个答案:

答案 0 :(得分:1)

由于您的employee.ValidateLetteremployee.checkNumber方法不使用employee类的任何字段,因此它们应为static

static boolean ValidateLetter(String input) { ... }

现在您可以在没有任何特定employee对象的情况下调用它们:

if (employee.ValidateLetter(input)) {
    ...
}

另请考虑遵循standard naming conventions,因为它会使您的代码更容易阅读。例如,Employee.isValidName而不是employee.ValidateLetter