当前位置:编程学习 > JAVA >>

如何在eclipse里面分别运行java里面的两个主方法

/**
 * This program demonstrates static methods.
 * @version 1.01 2004-02-19
 * @author Cay Horstmann
 */
public class StaticTest
{
   public static void main(String[] args)
   {
      // fill the staff array with three Employee objects
      Employee[] staff = new Employee[3];

      staff[0] = new Employee("Tom", 40000);
      staff[1] = new Employee("Dick", 60000);
      staff[2] = new Employee("Harry", 65000);

      // print out information about all Employee objects
      for (Employee e : staff)
      {
         e.setId();
         System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
               + e.getSalary());
      }

      int n = Employee.getNextId(); // calls static method
      System.out.println("Next available id=" + n);
   }
}

class Employee
{
   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; // set id to next available id
      nextId++;
   }

   public static int getNextId()
   {
      return nextId; // returns static field
   }

//   public static void main(String[] args) // unit test
//   {
//      Employee e = new Employee("Harry", 50000);
//      System.out.println(e.getName() + " " + e.getSalary());
//   }

   private String name;
   private double salary;
   private int id;
   private static int nextId = 1;
}
--------------------编程问答-------------------- 主函数所在类必须是public,而public类则必须定义在以该类名为文件名的java文件中。

所以你要同时运行两个主函数,就要独立的写两个java文件,而不能这样放在一起。 --------------------编程问答-------------------- 我现在想运行StaticTest里面的主方法,可是老是提示:错误: 在类 Employee 中找不到主方法, 请将主方法定义为:
   public static void main(String[] args)
--------------------编程问答-------------------- 首先:分成两个文件了没有?

其次:public class StaticTest 必须在 StaticTest.java 这个文件内。 --------------------编程问答-------------------- 我已经按照你的这么做了,  可是还是会用这个错误

Employee.java 里面把主函数注释了
如果不注释,他就不运行StaticTest.java里面的主函数..
如果注释了,就报错误.
--------------------编程问答-------------------- 那么Eclipse在Run的菜单上,把之前自动生成的运行配置项(名字大概是:Run Configuration...)先手工删除掉,然后重新试试吧。 --------------------编程问答-------------------- 谢谢了,按照你的方法,我调试成功了.
补充:Java ,  Eclipse
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,