Reputation: 23
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
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
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