java main函数 静态,Java:在main()方法中调用静态方法

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

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.

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;

}

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

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

generateEmployees();

otherwise you'll need to do

Employee.generateEmployees();

(if the Employee class contains generateEmployees()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值