在java中创建虚拟对象
我正在开发一个有两个类的java程序。一个名为employee的类具有声明和验证,如名称、年龄和员工编号。在我的主类中,我想验证员工编号和单独的编号。如何创建一个虚拟对象(或一些人调用的虚拟访问器)来获取它?我的程序通常是这样的:在java中创建虚拟对象,java,object,Java,Object,我正在开发一个有两个类的java程序。一个名为employee的类具有声明和验证,如名称、年龄和员工编号。在我的主类中,我想验证员工编号和单独的编号。如何创建一个虚拟对象(或一些人调用的虚拟访问器)来获取它?我的程序通常是这样的: package empdatabase; public class empdatabase { public static employee [] emp = new employee[50]; public static int count = 0; publ
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
package数据库;
公共类数据库
{
公共静态员工[]emp=新员工[50];
公共静态整数计数=0;
公共静态void main(字符串[]args){
}//末级干管
公共静态void usermenu()
{
字符串输入=新字符串(“”);
int-choice=0;
input=JOptionPane.showInputDialog(“用户菜单”+“\n 1.员工注册”
+“\n 2.员工登录”)
choice=Integer.parseInt(输入);
开关(选择)
{
案例1:
如果您的employee.ValidateLetter
和employee.checkNumber
方法未使用employee
类的任何字段,则应将其删除
现在,您可以在不使用任何特定的employee
对象的情况下调用它们:
if (employee.ValidateLetter(input)) {
...
}
也请考虑下面的代码,因为它会使代码更容易阅读。例如,例如“代码>雇员。ISValueNo.<代码>,而不是<代码>雇员。ValidateLetter < /代码> .< /P>哦,IF计数语句的意思是50,而不是100U,可以使用C克隆()。方法如果你只是想要对象的镜像谢谢你的帮助伙计们!它就像一个符咒:)
if (employee.ValidateLetter(input)) {
...
}