emp.java

来自「这个是学习网络编程的好好文档! 里面有一些老师发给的学习jsp的课件!」· Java 代码 · 共 49 行

JAVA
49
字号
import java.util.*; //use class: Random & Vector
public class Emp
{
  private static Vector allIDs = new Vector();
  private static Random r = new Random();
  private static final String SIGN = "#";
  private String id;
  
  public Emp(String n)  // Constructor
  {
    setID(n);
  }
  public void setID(String name)
  {
    validateID(name);
  }
  public String getID()
  {
    return id;
  }
  public static void list()
  {
    Enumeration e = allIDs.elements();
    while(e.hasMoreElements())
      System.out.println(e.nextElement());
  }
  private void validateID(String name)
  {
    String s = new String("");
    while(allIDs.contains(name+s))
      s = SIGN + Math.abs(r.nextInt());
    id = name + s;
    allIDs.addElement(id);
  }
  public static void main(String args[])
  {
    Emp e1 = new Emp("John");
    Emp e2 = new Emp("John");
    Emp e3 = new Emp("John");
    Emp e4 = new Emp("John");
    
    System.out.println("e1:"+e1.getID());
    System.out.println("e2:"+e2.getID());
    System.out.println("e3:"+e3.getID());
    System.out.println("e4:"+e4.getID());
    
    Emp.list();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?