需求:用集合记录三个人,并遍历循环。
文件:Student.java中
import java.util.ArrayList;
public class Student {
public static void main(String[] args){
ArrayList<Stu> list=new ArrayList<>();
Stu s =new Stu("ayue",20);
Stu s1 =new Stu("a",21);
Stu s2 =new Stu("b",22);
list.add(s);
list.add(s1);
list.add(s2);
for(int i =0;i<list.size();i++){
Stu St=list.get(i);
System.out.println(St.getAge()+"岁"+St.getName());
}
}
}
文件Stu.java中
public class Stu { private String name; private int age; public Stu(){ } public Stu(String name ,int age){ this.name=name; this.age=age; } 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; } }
标签:java,name,int,age,练习,Stu,集合,public From: https://www.cnblogs.com/ayue6/p/17829287.html