gpt4 book ai didi

java - 方法被调用早于定义

转载 作者:行者123 更新时间:2023-11-29 04:12:17 25 4
gpt4 key购买 nike

<分区>

我有这些代码行:

Family family = new Family("A");
Man man = new Man("B");
Woman woman = new Woman("C");
family.addPerson(man);
family.addPerson(woman)
System.out.println(family.toString() + ": " + family.getFamilyMembers());

这是家庭类:

public class Family {

private String name;
private ArrayList<Person> family = new ArrayList<>();

public Family(String name) {
this.name = name;
}

public void addPerson(Person person) {
this.family.add(person);
}

public String getFamilyMembers() {
for (Person person : this.family) {
System.out.println(person.toString());
}
return "";
}

@Override
public String toString() {
return this.name;
}

}

我不明白的是,当我运行程序时 family.getFamilyMembers();在 family.toString() 获取之前执行。对此有什么解释吗?

控制台输出:

B
C
A:

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com