本帖最后由 王博 于 2012-10-4 19:49 编辑
/*
目标:创建三个员工对象,包括名字,编号,工资
*/
class Employee
{
private String name;
private double salary;
private int id;
private static int nextId = 1;
public Employee(String n, double s)
{
name=n;
salary =s;
id = 0;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
public void setId() //从这里。。。
{ //.......
id = nextId; //.....
nextId++; //......
// .......
} //.....
public static int getNextId() //....
{ //.....
return nextId; //到这里。。是什么意思啊?这是在定义什么啊?
}
}
public class ObjectArray
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];
staff[0] = new Employee("张媛媛",140000);
staff[1] = new Employee("李吕辉",260000);
staff[2] = new Employee("王自立",165000);
for (int i =0;i<staff.length ;i++ )
{
Employee e = staff;
e.setId();
System.out.println(" 姓名= "+e.getName()+" , 编号= "+e.getId()+" ,工资="+e.getSalary());
}
int n = Employee.getNextId();
System.out.println(" 下一个有效编号="+n);
}
}
|