小弟新学JAVA,对JAVA核心技术一书中的一个例程不懂,特来求教
程序如下:import java.util.*;public class ConstructorTest {
public static void main(String[] args)
{
Employee[] staff=new Employee[3];
staff[0]=new Employee("harry potter", 400);
staff[1]=new Employee(120);
staff[2]=new Employee(30);
for(Employee e:staff)
System.out.println("name="+e.getName()
+",salary="+e.getSalary()
+",id="+e.getId());
}}class Employee{
public Employee(String n, double s)
{
name=n;
salary=s;
//System.out.println("Employee #"+nextId);
}
public Employee(double s)
{
//this call the Employee(String double) Constructor
this("Employee #"+nextId,s);
System.out.println("Employee #"+nextId);
}
public Employee()
{
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
private static int nextId;
//private static int id_S;
private int id;
private String name;
private double salary;
static
{
Random generator=new Random();
nextId=generator.nextInt(10000);
System.out.println("Static initial block first!!!! "+nextId);
}
{
id=nextId;
//id_S=id;
nextId++;
System.out.println("Initial block first!!!! "+nextId);
}
}
程序的运行结果如下:
Static initial block first!!!! 5327
Initial block first!!!! 5328
Initial block first!!!! 5329
Employee #5329
Initial block first!!!! 5330
Employee #5330
name=harry potter,salary=400.0,id=5327
name=Employee #5328,salary=120.0,id=5328
name=Employee #5329,salary=30.0,id=5329
我对类Employee中的第二个构造函数
public Employee(double s)
{
//this call the Employee(String double) Constructor
this("Employee #"+nextId,s);
System.out.println("Employee #"+nextId);
}中的this("Employee #"+nextId,s)的nextId不理解,
我感觉对于第二个对象中name应该为Employee #5329,但是结果为什么5328
程序如下:import java.util.*;public class ConstructorTest {
public static void main(String[] args)
{
Employee[] staff=new Employee[3];
staff[0]=new Employee("harry potter", 400);
staff[1]=new Employee(120);
staff[2]=new Employee(30);
for(Employee e:staff)
System.out.println("name="+e.getName()
+",salary="+e.getSalary()
+",id="+e.getId());
}}class Employee{
public Employee(String n, double s)
{
name=n;
salary=s;
//System.out.println("Employee #"+nextId);
}
public Employee(double s)
{
//this call the Employee(String double) Constructor
this("Employee #"+nextId,s);
System.out.println("Employee #"+nextId);
}
public Employee()
{
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
private static int nextId;
//private static int id_S;
private int id;
private String name;
private double salary;
static
{
Random generator=new Random();
nextId=generator.nextInt(10000);
System.out.println("Static initial block first!!!! "+nextId);
}
{
id=nextId;
//id_S=id;
nextId++;
System.out.println("Initial block first!!!! "+nextId);
}
}
程序的运行结果如下:
Static initial block first!!!! 5327
Initial block first!!!! 5328
Initial block first!!!! 5329
Employee #5329
Initial block first!!!! 5330
Employee #5330
name=harry potter,salary=400.0,id=5327
name=Employee #5328,salary=120.0,id=5328
name=Employee #5329,salary=30.0,id=5329
我对类Employee中的第二个构造函数
public Employee(double s)
{
//this call the Employee(String double) Constructor
this("Employee #"+nextId,s);
System.out.println("Employee #"+nextId);
}中的this("Employee #"+nextId,s)的nextId不理解,
我感觉对于第二个对象中name应该为Employee #5329,但是结果为什么5328
建议看下代码格式方面的资料。
按顺序是1,2,3....