简体   繁体   English

Java:在main()方法中调用静态方法

[英]Java: Calling a static method in the main() method

I am supposed to do the following: 我应该执行以下操作:

Write a Java application (Client) program with a static method called generateEmployees( ) that returns a random list of 10 different types of Employee objects. 使用称为generateEmployees()的静态方法编写一个Java应用程序(客户端)程序,该方法将随机返回10种不同类型的Employee对象的列表。 You could either use an array or an ArrayList to store the employee objects that will be returned. 您可以使用数组或ArrayList来存储将返回的员工对象。 Use a for loop to populate randomly different types of employee objects with some random data. 使用for循环用一些随机数据填充不同类型的员工对象。 You could possibly think a range of values like 1 – 4. If random value is 1, create a HourlyEmployee object with some randomly generated data, if 2, SalariedEmployee object with some random data and so on. 您可能会想到一个范围为1-4的值。如果随机值为1,则创建带有一些随机生成的数据的HourlyEmployee对象,如果值为2,则创建带有一些随机数据的SalariedEmployee对象,依此类推。 I would leave it to your ingenuity to generate and populate these different Employee objects. 我将由您来创造和填充这些不同的Employee对象。 As these objects are generated add them to your data structure (array or ArrayList that you are using). 生成这些对象后,将它们添加到您的数据结构(正在使用的数组或ArrayList)中。 Finally the method returns this data structure. 最后,该方法返回此数据结构。

In the same application class, implement the main( ) method. 在同一应用程序类中,实现main()方法。 Call the generateEmployees( ) static method and using a for loop print the details of each of the employee along with their earnings on the terminal window. 调用generateEmployees()静态方法,并使用for循环在终端窗口上打印每个雇员的详细信息以及他们的收入。

My generateEmployees() static method is as follows (it might not be correct... also, the data hasn't been randomly generated because I'm not exactly certain how to do that, at least as far as the first and last names are concerned.): 我的generateEmployees()静态方法如下(它可能不正确……而且,由于我不确定如何做到这一点,至少在名字和姓氏方面,数据不是随机生成的有关):

public static Employee[] generateEmployees()
{
    Employee[] employees = new Employee[10];
    int randomNum = 0;

    for (int i = 0; i < 10; i++)
    {
        Random random = new Random();
        randomNum = random.nextInt(4) + 1;

         switch (randomNum)
         {
            case 0:
                employees[i] = new SalariedEmployee("Bri", "Gefroh", 123, 1000);
                break;
            case 1:
                employees[i] = new HourlyEmployee("Bri", "Gefroh", 123, 12.50, 10);
                break;
            case 2:
                employees[i] = new CommissionEmployee("Bri", "Gefroh", 123, 10000, 0.05);
                break;
            case 3:
                employees[i] = new BasePlusCommissionEmployee("Bri", "Gefroh", 123, 10000, 0.05, 2500);
                break;
         }
    }

    return employees;
}

How would I call this method and use it in the main() method? 我将如何调用此方法并在main()方法中使用它? Each of those four types of employees are subclasses of the Employee class, and each subclass has its own toString() method, which is what I belivee I'm supposed to be outputting. 这四种类型的雇员中的每一个都是Employee类的子类,每个子类都有自己的toString()方法,这是我应该输出的。

A static method is a class method, rather than an instance method. 静态方法是类方法,而不是实例方法。 It's called on the class, not an instance of the class. 它是在类上调用的,而不是类的实例。 The difference being that you can call a static method without having an instance first. 区别在于您可以先调用静态方法而无需先拥有实例。

Employee.doSomething();

vs

Employee employee = new Employee();
employee.doSomethingElse();

So, if your generateEmployees() method is in the same class as your main, all you need is 因此,如果您的generateEmployees()方法与主类位于同一类中,那么您所需要做的就是

 generateEmployees();

otherwise you'll need to do 否则你需要做

 Employee.generateEmployees();

(if the Employee class contains generateEmployees() (如果Employee类包含generateEmployees()

If the method is in the same class, you should just be able to call it like any other method: 如果该方法在同一类中,则您应该能够像其他任何方法一样调用它:

public static void main(String[] args)
{
    Employee[] employees = generateEmployees();

    // TODO: loop through and print out...
}

Since main and generateEmployees are both static, it should work. 由于maingenerateEmployees都是静态的,因此它应该可以工作。 (If generateEmployees is non-static, you'd need to create an instance of the class first). (如果generateEmployees是非静态的,则需要首先创建该类的实例)。

I'd suggest having a constant array of Strings with "names" in it, and use a random number to generate an index. 我建议在其中包含一个带有“名称”的常量字符串数组,并使用随机数生成索引。 That should help with randomising the names a little. 这应该有助于稍微随机化名称。

It's a static method, so ... it does not need to be accessed within the context of an instantiated object. 这是一个静态方法,因此...不需要在实例化对象的上下文中对其进行访问。 You can just, you know, call it from your public static void main(...) method. 您可以知道,可以从您的公共static void main(...)方法调用它。 If the class that contains your main() method is named Employee, then... 如果包含main()方法的类名为Employee,则...

Employee.generateEmployees(); 

would do the trick. 会成功的

Like Ash stated but if you need to process the records, here is no reason to introduce extra variable just do 就像Ash所说的那样,但是如果您需要处理记录,这里没有理由引入额外的变量

 public static void main(String[] args)
 {
      for(Employee employee: generateEmployees())
         print(employee); // define static print somewhere too

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM