package testStatic;
public class StaticTest {
public static void main(String[] args) {
Employee[] ep=new Employee[3];
ep[0]=new Employee("Tom",2000); //创建实例放入数组中
ep[1]=new Employee("Jeny",4000);
ep[2]=new Employee("Hoke",8000);
for(Employee e: ep) {
e.setId();
System.out.println("name "+e.getName()+", id "+e.getId()+", salray "+e.getSalary());
}
int n=Employee.getNextId();
System.out.println("Next avalable id="+n);
}
}
class Employee{
private static int nextId=1; //静态变量
private String name;
private int id;
private double salary;
public Employee(String name,double salary) {
this.name=name;
this.salary=salary;
}
public int getId() {
return id;
}
public void setId() {
id=nextId;
nextId++;
}
public static int getNextId() {
return nextId;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
}
包含静态方法的使用,数组的使用,创建实例放入数组中