关于toString()的问题
User类的代码:
public class User {
private String name;
private String sex;
private int age;
private String address;
public User(String name,String sex,int age,String address){
this.name=name;
this.sex=sex;
this.age=age;
this.address=address;
}
public String toString(){
return name+"\t"+sex+"\t"+age+"\t"+address;
}
main方法:
public static void main(String[] args) {
// TODO 自动生成的方法存根
List<User> users=new ArrayList<User>();
User user1=new User("小王","男",18,"南京市");
User user2=new User("小李","男",21,"盐城市");
User user3=new User("小红","女",24,"上海市");
users.add(user1);
users.add(user2);
users.add(user3);
for(int i=0;i<users.size();i++){
System.out.println(users.get(i));
}
}
userList.get(i)获得的是User对象,为什么最后把User类的属性内容都打印了
这里是怎么调用toString()方法的