How to extend a class using another constructor?

It’s hard for me to extend a person’s class to a patient

I have a Person class with a constructor similar to

  public Person(String firstname,String surname) {
        fFirstname=firstname;
        fSurname=surname;
    }

Then i have a class of patients

public class Patient expands Personality

I want to have a constructor for a patient that looks something like

public Patient(String hospNumber) {
    fFirstname = lookup(hospNumber,"firstname");
    fSurname = lookup(hospNumber,"surname");
}

However, I am getting a complaint about the need to create a Patient (String, String) constructor. I understand why this is so, but I can’t understand how to expand the class of a person for a patient.

+4
source share
3 answers

Just pass the result of these two method calls to the constructor super:

public Patient(String hospNumber) {
    super(lookup(hospNumber,"firstname"), lookup(hospNumber,"surname"));
}

, this, , , .

+4

Person class

public Person() {

}

Person super 2-arg

public Patient(String hospNumber) {
    super(lookup(hospNumber,"firstname"), lookup(hospNumber,"firstname"));
}
+2

.

public Patient(String hospNumber) {
   super(lookup(hospNumber,"firstname"), lookup(hospNumber,"surname"));
}

lookup , . :

public Patient(String hospNumber, String firstname, String surname) {
  super(firstname,surname);
}

- .

, Person. , Person(), - . super() .

: 1 , .

super() ( , ).

+2

Source: https://habr.com/ru/post/1648215/


All Articles