4.6.1 Overloading 重载
Overloading occurs if several methods have the same name but different parameters.
比较常见的就是一个class有多个constructors。Example:
// Java program to demonstrate working of method
// overloading in Java.
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum(). This sum takes two double parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
4.6.2 Default Field Initialization
In a class, if you don't initialize a field, it is automatically initialized to a default (0, false, or null).
Default values: numbers to 0; boolean values to false, and object references to null.
4.6.3 The Constructor with No Arguments
Example 1:
public Employee()
{
name = "";
salary = 0;
hireDay = LocalDate.now();
}
如果我们创建一个class的时候,不写constructor,这个class自动就会有一个不带参数的constructor,且这个constructor将所有的instance fields设置default values。也就是数字变量的值为0,boolean变量的值为false,对象变量的值为null。
如果一个class有至少一个带参数的constructor,但没有no-argument constructor,那么在创建新的对象时就不能不提供参数。也就是说,比如,Employee(String name, double salary, int y, int m, int d),Employee这个class有这样一个constructor且没有no-argument constructor,那么e = new Employee(); 就会导致error。
4.6.6 Calling Another Constructor
另一个说法叫constructor chaining。
具体例子:
// Java program to illustrate Constructor Chaining
// within same class Using this() keyword
class Temp
{
// default constructor 1
// default constructor will call another constructor
// using this keyword from same class
Temp()
{
// calls constructor 2
this(5);
System.out.println("The Default constructor");
}
// parameterized constructor 2
Temp(int x)
{
// calls constructor 3
this(5, 15);
System.out.println(x);
}
// parameterized constructor 3
Temp(int x, int y)
{
System.out.println(x * y);
}
public static void main(String args[])
{
// invokes default constructor first
new Temp();
}
}
程序输出为:
75
5
The Default constructor
Constructor Chaining to other class using super() keyword
具体例子:
// Java program to illustrate Constructor Chaining to
// other class using super() keyword
class Base
{
String name;
// constructor 1
Base()
{
this("");
System.out.println("No-argument constructor of base class");
}
// constructor 2
Base(String name)
{
this.name = name;
System.out.println("Calling parameterized constructor of base");
}
}
class Derived extends Base
{
// constructor 3
Derived()
{
System.out.println("No-argument constructor of derived");
}
// parameterized constructor 4
Derived(String name)
{
// invokes base class constructor 2
super(name);
System.out.println("Calling parameterized constructor of derived");
}
public static void main(String args[])
{
// calls parameterized constructor 4
Derived obj = new Derived("test");
// Calls No-argument constructor
// Derived obj = new Derived();
}
}
4.6.7 Initialization Blocks
举例:
class Employee
{
private static int nextId;
private int id;
private String name;
private double salary;
// object initialization block
// this block will be executed whenever an object is created
// and this block is executed before the constructor
{
id = nextId;
nextId++;
}
// constructor
public Employee(String n, double s)
{
...
}
...
}
If the static fields of your class require complex initialization code, use a static initialization block.
// static initialization block
static
{
Random generator = new Random();
nextId = generator.nextInt(10000);
}
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();
// 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)
{
// calls the Employee(String, double) constructor
this("Employee #" + nextId, s);
}
public Employee()
{
// name initialized to ""
// 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;
}
}