I have 4 classes. 1) Employee
class 2) Nurse
class that extends Employee
3) Doctor
class that also extends Employee
4) Supervisor
class that extends Doctor
我有4节课。 1)员工类2)扩展员工的护士班3)也扩展员工的博士班4)扩展博士的监督班
Inside Supervisor I have a property: private Employee[] arrayOfEmployees;
在Supervisor中我有一个属性:private Employee [] arrayOfEmployees;
Basically an array of Employees that inside are doctors and nurses. now i want to build a function inside the Supervisor class that will return the number of nurses within the array.
基本上是一系列员工,里面是医生和护士。现在我想在Supervisor类中构建一个函数,它将返回数组中的护士数量。
My problem is that I don't know how to access the array because the array type is Employee and I look for Nurse.
我的问题是我不知道如何访问数组,因为数组类型是Employee,我寻找Nurse。
Can someone help me with this function?
有人可以帮我这个功能吗?
4 个解决方案
#1
2
If you use java 8 you can use streams for this:
如果您使用java 8,则可以使用以下流:
int numNurses = Arrays
.stream(employeeArray)
.filter(e -> e instanceof Nurse.class)
.count();
#2
0
Just with the instanceof keyword.
只需使用instanceof关键字。
if (arrayOfEmployees[i] instanceof Nurse) {
Nurse nurse = (Nurse) arrayOfEmployees[i];
}
#3
0
Using java 8 and streams
使用java 8和流
//array of employees 3 Nurses & 2 Docs
E[] aOfE = new E[] { new N(), new N(), new N(), new D(), new D() };
Predicate<E> pred = someEmp -> N.class.isInstance(someEmp);
System.out.println(Arrays.stream(aOfE).filter(pred).count());
where the class:
班级:
E=Employee, N=Nurse, D=Doctor
or using lambdas
或者使用lambdas
E[] aOfE = new E[] { new N(), new N(), new N(), new D(), new D() };
System.out.println(Arrays.stream(aOfE).filter(someEmp -> N.class.isInstance(someEmp)).count());
#4
0
public class Main {
public static void main(String[] args) {
Supervisor supervisor = new Supervisor();
supervisor.arrayOfEmployees = new Employee[] {new Nurse(), new Doctor(), new Doctor(), new Nurse()};
//will be 2
long numberOfNurses = supervisor.numberOfNurses();
System.out.println(numberOfNurses);
}
}
class Employee {}
class Doctor extends Employee {}
class Nurse extends Employee {}
class Supervisor extends Doctor {
Employee[] arrayOfEmployees;
long numberOfNurses() {
return Stream.of(arrayOfEmployees).filter(e -> e instanceof Nurse).count();
}
}
#1
2
If you use java 8 you can use streams for this:
如果您使用java 8,则可以使用以下流:
int numNurses = Arrays
.stream(employeeArray)
.filter(e -> e instanceof Nurse.class)
.count();
#2
0
Just with the instanceof keyword.
只需使用instanceof关键字。
if (arrayOfEmployees[i] instanceof Nurse) {
Nurse nurse = (Nurse) arrayOfEmployees[i];
}
#3
0
Using java 8 and streams
使用java 8和流
//array of employees 3 Nurses & 2 Docs
E[] aOfE = new E[] { new N(), new N(), new N(), new D(), new D() };
Predicate<E> pred = someEmp -> N.class.isInstance(someEmp);
System.out.println(Arrays.stream(aOfE).filter(pred).count());
where the class:
班级:
E=Employee, N=Nurse, D=Doctor
or using lambdas
或者使用lambdas
E[] aOfE = new E[] { new N(), new N(), new N(), new D(), new D() };
System.out.println(Arrays.stream(aOfE).filter(someEmp -> N.class.isInstance(someEmp)).count());
#4
0
public class Main {
public static void main(String[] args) {
Supervisor supervisor = new Supervisor();
supervisor.arrayOfEmployees = new Employee[] {new Nurse(), new Doctor(), new Doctor(), new Nurse()};
//will be 2
long numberOfNurses = supervisor.numberOfNurses();
System.out.println(numberOfNurses);
}
}
class Employee {}
class Doctor extends Employee {}
class Nurse extends Employee {}
class Supervisor extends Doctor {
Employee[] arrayOfEmployees;
long numberOfNurses() {
return Stream.of(arrayOfEmployees).filter(e -> e instanceof Nurse).count();
}
}