java的static 初始化块

import java.util.*;
class Constructor
{
  public static void main(String[] args)
{
Employee[] staff=new Employee[3];

staff[0]=new Employee("harry",4000);
staff[1]=new Employee(60000);
staff[2]=new Employee();

for(Employee e:staff)
{
  System.out.println("name="+e.getName()+",id="+e.getId()+",salary="+e.getSalary());
}
}
}
class Employee
{
public Employee(String name,double salary)
{
this.name=name;
this.salary=salary;
}

public Employee(double salary)
{
this("Employee #"+nextId,salary);
}

public Employee()
{
}

public String getName()
{
return this.name;
}

public double getSalary()
{
return this.salary;
}

public int getId()
{
return id;
}

private static int nextId;
private int id;
private String name="";
private double salary;

static
{
Random generator=new Random();
nextId=generator.nextInt(10000);
}

{
id=nextId;
nextId++;
}

}

posted @ 2013-09-05 09:30  一叶落香  阅读(204)  评论(0编辑  收藏  举报