赞
踩
GregorianCalendar today = new GregorianCalendar();
或者
GregorianCalendar deadline = new GregorianCalendar(2009, Calendar.DECEMBER, 31);
这种特征叫做重载.如果多个方法有相同的名字,不同的阐述,便产生了重载.
- public Employee()
- {
- name = "";
- salary = 0;
- hireDay = new Date();
- }
如果在编写一个类时没有编写构造器,那么系统就会提供一个无参数构造器.这个构造器将所有的实例域设置为默认值.于是,实例域中的数值型数据设置为0,布尔型数据设置为 false,所有对象变量设置为 null .
new ClassName()
就必须提供一个默认的构造器(即不带参数的构造器).
- class Employee
- {
- private String name = "";
- ...
- }
在执行构造器之前,先执行赋值操作.当一个类的所有构造器都希望把相同的值赋予某个特定的实例域时,这种方式特别有用.- class Employee
- {
- private static int nextId;
- private int id = assignId();
- ...
- private static int assignId()
- {
- int r = nextId;
- nextId++;
- return r;
- }
- ...
- }
注释:在C++中,不能直接初始化类的实例域.所有的域必须在构造器中设置.但是,有一个特殊的初始化器列表语法,如下所示:
- Employee::Employee(String n, double s, int y, int m, int d)
- : name(n), salary(s), hireDay(y, m, d)
- {}
C++使用这种特殊的语法来调用域构造器.在Java中没有这种必要,因为对象没有子对象,只有指向其他对象的指针.
- public Employee(String n, double s)
- {
- name = n;
- salary = s;
- }
但这样有一个缺陷:只有阅读代码才能够了解参数n和参数s的含义.于是有些
程序员在每个参数前面加上一个前缀"a":
- public Employee(String aName, double aSalary)
- {
- name = aName;
- salary = aSalary;
- }
这样很清晰.
- public Employee(double s)
- {
- // call Employee(String, double)
- this("Employee #" + nextId, s);
- nextId++;
- }
当调用 new Employee(6000)时,Employee(double)构造器将调用Employee(String, double)构造器.
- class Employee
- {
- private static nextId;
-
- private int id;
- private String name;
- private double salary;
-
- // object initialization block
- {
- id = nextId;
- nextId++;
- }
- public Employee(String n, double s)
- {
- name = n;
- salary = s;
- }
- public Employee()
- {
- name = "";
- salary = 0;
- }
- ...
- }

在这个示例中,无论使用哪种构造器构造对象,id域都在对象初始化块中被初始化.
首先运行初始化块,然后才运行构造器的主体部分.
- 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", 4000);
- staff[1] = new Employee(6000);
- 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
- {
- 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++;
- }
- // three overloaded constructors
- public Employee(String n, double s)
- {
- name = n;
- salary = s;
- }
- public Employee(double s)
- {
- // call the Employee(String, double) constructor
- this("Employee #" + nextId, s);
- }
- // the default construct
- public Employee()
- {
- }
- public String getName()
- {
- return name;
- }
- public double getSalary()
- {
- return salary;
- }
- public int getId()
- {
- return id;
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。