在学习中,我们只知道一个类最多只能有一个类,那么看下面的代码,可得,两个类中都有两个函数,且都有main()方法,那么StaticTest类调用Employee时,为什么不会抛出异常?那又为什么不会加载Empolyee类中的main()方法呢?- public class StaticTest {
- public static void main(String[] args) {
- Employee[] staff = new Employee[3];
-
- staff[0] = new Employee("zhangsan",4000);
- staff[1] = new Employee("lishi",6500);
- staff[2] = new Employee("wangwu",6000);
-
- 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 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 static void main(String[] args){
- Employee e = new Employee("zhaoqi",5000);
- System.out.println(e.getName()+" "+e.getSalary());
- }
- }
复制代码 |