import java.util.*;public class ConstructorTest
{ public static void main(String[] args)
{
Employee[] staff=new Employee[3]; staff[0]=new Employee("Harry",40000);
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());
// TODO code application logic here
}}class Employee
{ public Employee(String n,double s)
{
name=n;
salary=s; } public Employee(double s)
{
this("Employee #"+nextId, s); }
public Employee()
{ } public String getName()
{
return name; }
public double getSalary()
{
return 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(100000); }
{
id=nextId;
nextId++;
}
}3次构造器 初始化对象 后面的静态块执行了几次 是1次还是3次??
{ public static void main(String[] args)
{
Employee[] staff=new Employee[3]; staff[0]=new Employee("Harry",40000);
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());
// TODO code application logic here
}}class Employee
{ public Employee(String n,double s)
{
name=n;
salary=s; } public Employee(double s)
{
this("Employee #"+nextId, s); }
public Employee()
{ } public String getName()
{
return name; }
public double getSalary()
{
return 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(100000); }
{
id=nextId;
nextId++;
}
}3次构造器 初始化对象 后面的静态块执行了几次 是1次还是3次??
刚才好像有相同的帖子?!
这是java他爹当年这样规定的,你去问他为什么这样规定
}
{
//这种块与static 静态块 不同 每当实例化(new)一个对象,它就执行一次。
}