【发布时间】:2021-07-25 23:18:15
【问题描述】:
我正在读一本书,它有一个示例,该示例在其文件中的不同类中有 2 个主要方法,一个主要方法用于测试目的,但如果有任何方法,我无法理解单独编译类的方法,请建议我 这是代码
/**
* @author achintya
*/
public class StaticTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];
staff[0] = new Employee("tom",4000);
staff[1] = new Employee("dick",60000);
staff[2] = new Employee("harry",65000);
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("harry",50000);
System.out.println(e.getName() + " " + e.getSalary());
}
}
【问题讨论】: