rosinek
rosinek

Reputation: 23

Declare variables within similar class

In my Android project, I just made something like this:

Person person;

void onSucceed(Doc doc) {
    person.setName(doc.getName);
    person.setAddres(doc.getAddres);
    ...
    person.setAge(doc.getAge);
}

These two classes are not the same, but they have a lot of similar variables. Is there any faster method to not repeatedly write person.set...?

Upvotes: 1

Views: 59

Answers (2)

L.Spillner
L.Spillner

Reputation: 1772

You can let the class Doc inherit parts of their attributes from Person .

Example:

public class Person
{
  protected int age;
  protected String name;
  protected Address addr;

  //****** SETTER / GETTER ****

}


public class Doc extends Person
{
  private Location medical_center;
  private Family fam;
  private int income;
  //etc


  //**** GETTER /SETTER ****

}

By doing so you can create a new Doc object and still use the getter and setter from the Person class. But the most important is, that as many other classes as you like to, can extend person.

Update: This solution does only make sense when Doc represents something like a doctor or anything else that is related to a Person.

Upvotes: 1

OldCurmudgeon
OldCurmudgeon

Reputation: 65793

I would create a method in the Person class to gather the details from the Doc object.

    public void useDetails(Doc doc) {
        setName(doc.getName());
        setAddres(doc.getAddres());
        setAge(doc.getAge());
    }

Upvotes: 0

Related Questions