publicclassYinYongTest{publicstaticvoidmain(String[]args){Employa=newEmploy("啊啊啊",100000);System.out.println("a的名字是"+a.getName()+",a的ID是"+a.getId()+",a的工资是...
public class YinYongTest{
public static void main(String[] args){
Employ a = new Employ("啊啊啊", 100000);
System.out.println("a的名字是" + a.getName() + ",a的ID是" + a.getId() + ",a的工资是" + a.getSalary());
Employ b = new Employ(20000);
System.out.println("b的名字是" + b.getName() + ",b的ID是" + b.getId() + ",b的工资是" + b.getSalary());
}
}
class Employ{
public Employ(String n, double s){
name = n;
salary = s;
}
public Employ(double s){
this("Employ #" + id, s);
}
private String name ;
private double salary ;
private static int nextId;
private static int id;
static{
Random generator = new Random();
nextId = generator.nextInt(10000);
}
{
id = nextId;
nextId++;
}
}
用第一个构造器创建一个对象,再用第二个构造器创建一个对象,但是第二个对象的name总是“Employ #“加上第一个对象的id,而不是第一个对象id+1,Employ类里面有get、set方法,为了简洁没有放上来。
展开