我想在getName()
上执行Visit
方法,该方法包含名称的返回方法,但它打印出来" null"
public void getAllVisits() {
Visit v = new Visit();
for (int i = 0; i < myVisits.size(); i++) {
System.out.println(v.getName());
System.out.println(v.getAmount());
System.out.println(v.getDate());
}
}
这是访问类
中的代码public Visit(){}
public Visit(double visitAmount, String Name)
{
amount = visitAmount;
doctorName = Name;
}
public String getName()
{
return doctorName;
}
答案 0 :(得分:1)
您没有正确初始化您的访问对象,您应该致电:
Visit v = new Visit(100, "Visit Name");
另外,正如有人指出的那样,循环不会做任何事情,因为它总是调用v的相同实例。你可能想要的是:
List<Visit> myVisits;
public void someOtherMethod() {
myVisits = new ArrayList<Visit>();
Visit visit1 = new Visit(100, "Visit Name");
myVisits.add(visit1);
}
public void getAllVisits() {
for (Visit visit : myVisits) {
System.out.println(visit.getName());
System.out.println(visit.getAmount());
System.out.println(visit.getDate());
}
}