没有必要向寻求您帮助的人吐口水。
@user2943394,是的,你的设计在这里不好。OO 的存在就是为了简化您的编码。
这是一个快速示例,您可以如何做您需要做的事情,这样它看起来很不错。但请注意,这只是 2 分钟的代码废品,而不是最先进的设计。
public class Person {
private String firstName;
private String lastName;
private int age;
// Constructor
public Person(...) {
...
}
// Getters & Setters
public int getAge() {
return this.age;
}
public void setAge(int age) {
// you dont want to pass the wrong value, so you might want to check for that also
this.age = age;
}
...
}
public class PersonRepo {
private Map<Integer, Person> personMap;
public PersonRepo() {
this.personMap = new HashMap<Integer, Person>();
loadPersons();
}
private void loadPersons() {
// load them from text file, or whicever source you have
// or simply add them manually
}
public Person getPersonById(int id) {
return this.personMap.get(id);
}
}
public class PersonService {
private PersonRepo personRepo;
public PersonService() {
this.personRepo = new PersonRepo();
}
public void incrementPersonAge(int personId, int step) {
Person person = this.personRepo.getPersonById(personId);
int oldAge = person.getAge();
int newAge = oldAge + step;
person.setAge(newAge);
}
}
public class PersonTester() {
public static void main(String[] args) {
PersonService personService = new PersonService();
personService.incrementPersonAge(12, 1);
}
}
如果你真的坚持自己的方式,你可以这样做:
int oldAge = (Integer) idNmber.get(0).get(1);
int newAge = oldAge + 1;
idNumber.get(0).set(1, newAge);