我在下面有一個Employee和Employees類。如何獲取所有員工的數量?
//Employee.java
public class Employee {
private String name;
private String email;
public Employee (String name, String email) {
this.name = name;
this.email = email;
}
//Employees.java
import java.util.ArrayList;
public class Employees {
private ArrayList<Employee> employees;
public Employees(){
this.employees = new ArrayList<Employee>();
employees.add(new Employee("Thomas Muller","[email protected]"));
}
}
uj5u.com熱心網友回復:
您可以定義一個簡單的getCount()方法來獲取員工總數:
public class Employees {
...
public int getCount() {
return this.employees.size();
}
}
稍后您可以在Employees實體上呼叫它:
Employees employees = new Employees();
int totalCount = employees.getCount();
System.out.println("Total count of employees:" totalCount);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/310990.html
