我知道如何“setValue”“getValue”,但是在这种情况下你如何“getValue”?
public void setInfo(String name, int age) {
setName(name);
setAge(age);
}
有没有办法同时获取string和int的Info?
最佳答案
根据代码,我假设姓名和年龄都是 InfoObject 的一部分,因此您可以在 getInfo() 调用中返回整个对象, 前-
public InfoObject getInfo() {
return infoObject();
}
因为你不能从同一个函数返回两个值。如果您不想使用这种方式,那么您必须为姓名和年龄分别编写两个单独的方法。喜欢:
public String getName() {
return this.name;
}
和
public int getAge() {
return this.age;
}
这样您就可以使代码简洁且易于理解。
关于java - 如何同时用String和int实现setter和getter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37802825/