重载
如果多个方法有相同的名字,不同的参数,便产生了重载。编译器必须挑选出具体执行哪个方法,他通过用各个方法给出的参数类I型那个与特定方法调用所使用的值类型进行匹配来挑选出相应的方法。如果编译器找不到匹配的参数,就会产生编译时错误,因为根本不存在匹配,或者就没有一个比其他的更好。(这个过程称为重载解析)
*因此,要完整地描述一个方法需要指出方法名以及参数类型。这叫方法的签名。
*不能有两个名字相同,参数类型也相同却返回不同类型值的方法。
默认域初始化
无参数的构造器
package testbotoo; import java.util.Random; public class ConstructorTest { public static void main(String[] args){ // fill the staff array with three Employee object Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry", 4000); staff[1] = new Employee(6000); staff[2] = new Employee(); for (Employee e : staff) System.out.println("name = "+ e.getName()+",> e.getSalary()); } } class Employee { private static int nextId; private int id; private String name = ""; //instance field initalization private double salary; //static initialization block static { Random generator = new Random(); //set nextId to a random number between and 999 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); } // the default constructor public Employee() { // name initvalized to "" --see above // 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; } }