I´m trying to change the object salaDeEspera[i] from Paciente to Enfermo as the salaDeEspera array is Pacientes type and I wanna put it inside the habitaciones array which is Enfermos type
public void atender(Pacientes[] salaDeEspera, Enfermo[] habitaciones, Enfermeros enfermero1, Doctores doctor1) {
for (int i = 0; i < salaDeEspera.length; i ) {
enfermero1.examinar(salaDeEspera[i].getNombre());
doctor1.atender(salaDeEspera[i].getNombre());
if (doctor1.enfermo==true) {
habitaciones[i]= salaDeEspera[i].getNombre();
}
CodePudding user response:
You can either create a method that copies the data from the first type to the second... OR create another class that contains the common data of each class and have your own classes extend that class. Example:
public class people {}
and then add this to both your classes
public class Pacientes extends people {}
for your function, you can pass in the type people
public void atender(people[] salaDeEspera, people[] habitaciones, Enfermeros enfermero1, Doctores doctor1) {
...
}
!!Be aware that you'll need to actually type cast them in the function.
CodePudding user response:
You can't (literally) change the type of an object. Its fundamental type is fixed (for ever) when it is created.
You may be able to create a new Enfermo
from the state of a Pacientes
. But you are creating a new object, not changing the type of an existing object.
You may be able to wrap a Pacientes
inside an Enfermo
... but once again you are creating a new object.
You may be able to cast a Pacientes
to an Enfermo
... but only if:
- the type
Pacientes
is a supertype ofEnfermo
and the object is really aEnfermo
already, or - the type
Enfermo
is a supertype ofPacientes
.
I don't think we can be more specific without details of what Pacientes
and Enfermo
are, what they mean, how they are related, and .... how you are actually using them.