Java:在main()方法中调用静态方法 [英] Java: Calling a static method in the main() method

查看:1338
本文介绍了Java:在main()方法中调用静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该执行以下操作:

使用称为generateEmployees()的静态方法编写Java应用程序(客户端)程序,该方法将随机返回10种不同类型的Employee对象的列表.您可以使用数组或ArrayList来存储将返回的员工对象.使用for循环可使用一些随机数据来随机填充不同类型的员工对象.您可能会想到一个范围为1到4的值.如果随机值为1,则创建带有一些随机生成的数据的HourlyEmployee对象,如果值为2,则创建带有一些随机数据的SalariedEmployee对象,依此类推.我将由您来创造和填充这些不同的Employee对象.生成这些对象后,将它们添加到您的数据结构(正在使用的数组或ArrayList)中.最后,该方法返回此数据结构.

Write a Java application (Client) program with a static method called generateEmployees( ) that returns a random list of 10 different types of Employee objects. You could either use an array or an ArrayList to store the employee objects that will be returned. Use a for loop to populate randomly different types of employee objects with some random data. 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. I would leave it to your ingenuity to generate and populate these different Employee objects. As these objects are generated add them to your data structure (array or ArrayList that you are using). Finally the method returns this data structure.

在同一应用程序类中,实现main()方法.调用generateEmployees()静态方法,并使用for循环在终端窗口上打印每位员工的详细信息以及他们的收入.

In the same application class, implement the main( ) method. 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()静态方法如下(它可能不正确……而且,由于我不确定如何做到这一点,所以数据不是随机生成的,至少在第一种情况下是这样.和姓氏有关.):

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.):

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;
}

如何调用此方法并将其用于main()方法?这四种类型的雇员中的每一个都是Employee类的子类,并且每个子类都有自己的toString()方法,这是我应该输出的.

How would I call this method and use it in the main() method? 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.

推荐答案

静态方法是类方法,而不是实例方法.它是在类上调用的,而不是类的实例.区别在于您可以先调用静态方法而无需先拥有实例.

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();

因此,如果您的generateEmployees()方法与主类位于同一类中,那么您所需要做的就是

So, if your generateEmployees() method is in the same class as your main, all you need is

 generateEmployees();

否则,您需要做

 Employee.generateEmployees();

(如果Employee类包含generateEmployees()

(if the Employee class contains generateEmployees()

这篇关于Java:在main()方法中调用静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆