【Java】Employee类的简单版本

public class StaticTest {
    public static void main(String[] args) {
        
    Employee[] staff = new Employee[3];
    
    staff[0] = new Employee("菠萝",75000);
    staff[1] = new Employee("萝卜",65000);
    staff[2] = new Employee("朵朵",85000);
    
    for(Employee e : staff)
    {
        e.setId();
        System.out.println("name=" +e.getName()+ ",id=" + e.getId()+",salary=" + e.getSalary());
    }
    int n = Employee.getNextId();
    System.out.println("Next available id "+ n);
        }
}


class Employee {
    private static int nextId = 1;
    private String name;
    private double salary;
    private int id;
    
    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 static void main(String[] args)
    {
        Employee e = new Employee("奥利奥",50000);
        System.out.println(e.getName()+" "+e.getSalary());
    }
}

 

 

程序包含了Employee类的一个简单版本,其中有一个静态域nextId和一个静态方法getNextId。这里将三个Employee对象写入数组,然后打印雇员信息。最后打印出下一个可用的员工标志码来展示静态方法。

Employee类也有一个静态的main方法用于单元测试。

 

posted @ 2018-02-11 18:47  ieblYang  阅读(610)  评论(0编辑  收藏  举报