Reputation: 88
I'm starting in java and I don't if a big organization, like I want, it can do. I have to many parameters to include in a class and I want to access something similar like that: For example I have the class Student who implements the class Person. But he class Student have many parameters and I want to classified it it like this:
Student John = new Student();
John.physical.setHeight(178);
John.physical.setWeight(70);
John.academic.setQualification(7.5);
John.academic.setSubject(Maths);
John.administrative.setAccountNumber(XXXXX);
John.administrative.setPassport(123456789);
...
Can I do that?
Upvotes: 0
Views: 194
Reputation: 2107
There are several points: 1) you should move all nested logical units like "physical", "academic", "administrative" to dedicated entities, 2) you should introduce appropriate constructors for each entity that will receive all initialization parameters, e.g. constructor for "physical" will be next Physical(short height, short weight) ("Physical" is the entity name), 3) you should use 'Fluent interface' introduced by Mr. Fowler and Mr. Evans. So, your code will be reorganized in next manner:
Student john = new Student();
john.setPhysical(new Physical(178, 70))
.setAcademic(new Academic(7.5, Maths))
.setAdministrative(new Administrative(/*account number*/, /*passport number*/));
Update.
Each of new classes should be placed outside "Student" class. They will be responsible for specific aspect of "Student" class and satisfy 'S' rule from 'SOLID' principle and instance of "Student" class will own instances of that classes. This will make that classes less coupled, because, for example, if "Academic" class should be changed than "Student" class will not be modified, also you can introduce several types of "Academic" entities, etc.
Upvotes: 1
Reputation: 410
AS the others already pointed out in the comments, you can use nested classes:
Example:
public class Student {
class Physical {
int height;
int weight;
void setHeight(int height) {
this.height = height;
}
void setWeight(int weight) {
this.weight = weight;
}
}
class Academic {
// ...
}
class Administrative {
// ...
}
Physical physical = new Physical();
// ..
}
now you can access with:
John.physical.setHeight(178);
Upvotes: 2
Reputation: 1957
You can achieve this by creating separate classes for the category types. E.g you can have a "PhysicalCharacteristics" class with height and weight properties, an "AdministrativeDetails" class with accountNumber and passport properties etc. Then you would place instances of these classes as properties in the Student class. So you will be able to access/edit properties by getting them from those class properties. For example to set height you would do John.getPhysicalCharacteristics().setHeight(178);
Do not forget to initialize these classes in the Student constructor to avoid null reference exceptions:
For example if your Student class contains a "PhysicalCharacteristics physicalCharacteristics;" variable you should initialize it in the constructor like this:
public Student(){
physicalCharacteristics = new PhysicalCharacteristics();
}
Upvotes: 0