import java.util.ArrayList;
class People {
private String name;
private int age;
public People(String string, int i) {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "People [name=" + name + ", age=" + age + "]";
}
}
public class JiHeDemo {
public static void main(String[] args) {
People p = new People("lisio", 48);
p.setName("lishuai");
p.setAge(18);
ArrayList al = new ArrayList();
al.add(p);
al.add(new People("zhangsan", 22));
System.out.println(al);
}
@Override
public String toString() {
return "JiHeDemo [getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()
+ "]";
}
}
eclipse中的source帮我写好了set get toString方法,我只需要创建对象,赋值,将对象放到集合中即可。
网友评论