/**
@version 1.01 2004-02-19
@author Cay Horstmann
*/import java.util.*;public class ConstructorTest
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects
Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry", 40000);
staff[1] = new Employee(60000);
staff[2] = new Employee(); // print out information about all Employee objects
for (Employee e : staff)
System.out.println("name=" + e.getName()
+ ",id=" + e.getId()
+ ",salary=" + e.getSalary());
}
}class Employee
{
// three overloaded constructors
public Employee(String n, double s)
{
name = n;
salary = s;
} public Employee(double s)
{
// calls the Employee(String, double) constructor
this("Employee #" + nextId, s);
} // the default constructor
public Employee()
{
// name initialized to ""--see below
// salary not explicitly set--initialized to 0
// id initialized in initialization block
} 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 = ""; // instance field initialization
private double salary; // static initialization block
static
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
} // object initialization block 什么是对象初始化块?什么时候被初始化?
{
id = nextId;
nextId++;
}
}
@version 1.01 2004-02-19
@author Cay Horstmann
*/import java.util.*;public class ConstructorTest
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects
Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry", 40000);
staff[1] = new Employee(60000);
staff[2] = new Employee(); // print out information about all Employee objects
for (Employee e : staff)
System.out.println("name=" + e.getName()
+ ",id=" + e.getId()
+ ",salary=" + e.getSalary());
}
}class Employee
{
// three overloaded constructors
public Employee(String n, double s)
{
name = n;
salary = s;
} public Employee(double s)
{
// calls the Employee(String, double) constructor
this("Employee #" + nextId, s);
} // the default constructor
public Employee()
{
// name initialized to ""--see below
// salary not explicitly set--initialized to 0
// id initialized in initialization block
} 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 = ""; // instance field initialization
private double salary; // static initialization block
static
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
} // object initialization block 什么是对象初始化块?什么时候被初始化?
{
id = nextId;
nextId++;
}
}
解决方案 »
- java 一个类中的方法调用另一个类的方法返回空值,怎么解决?
- 请问, 怎么在JTable的Cell中显示图片?
- HttpClient进行半自动注册的问题,post提交参数的问题
- java异常的思考与学习心得-初学者可以来看看
- 请教大家 import java.io.* 时出现这样的 error 是怎么了~~
- 请问了:怎么在JTable的单元格里加JPanel?
- Runtime.exec();命令行调用连续问题??
- 谁能提供一个用“异或”来加密解密的实例,送分!
- 那里有最新<jbuilder开发人员指南>下载
- 这样的Sql语句怎么会取不到记录集出来?
- 求助,在dos中出了11个错误,不知错了什么?
- 刚从C/S转到B/S,菜菜地问:B/S的报表是如何打印的?
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
}
上面这段是类被加载时调用 // object initialization block 什么是对象初始化块?什么时候被初始化?
{
id = nextId;
nextId++;
}
实例化该类的对象时调用
1. 父类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行
2. 子类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行
3. 父类实例成员和实例初始化块 ,按在代码中出现的顺序依次执行
4. 父类构造方法
5. 子类实例成员和实例初始化块 ,按在代码中出现的顺序依次执行
6. 子类构造方法
@version 1.01 2004-02-19
@author Cay Horstmann
*/ import java.util.*; public class ConstructorTest
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects
Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry", 40000);
staff[1] = new Employee(60000);
staff[2] = new Employee(); // print out information about all Employee objects
for (Employee e : staff)
System.out.println("name=" + e.getName()
+ ",id=" + e.getId()
+ ",salary=" + e.getSalary());
}
} class Employee
{
// three overloaded constructors
public Employee(String n, double s)
{
name = n;
salary = s;
} public Employee(double s)
{
// calls the Employee(String, double) constructor
this("Employee #" + nextId, s);
} // the default constructor
public Employee()
{
// name initialized to ""--see below
// salary not explicitly set--initialized to 0
// id initialized in initialization block
} 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 = ""; // instance field initialization
private double salary; // static initialization block
static
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
} // object initialization block 什么是对象初始化块?什么时候被初始化?
{
id = nextId;
nextId++;
}
}