day09随机点名案例封装实现
package com.tedu.demo; import java.util.ArrayList; import org.omg.CosNaming.NamingContextExtPackage.AddressHelper; public class CallName { /** * */ public static void main(String[] args) { ArrayList<Student> array = new ArrayList<Student>(); addStudents(array); printStudent(array); randomStudent(array); } //生成随机数,产生随机姓名 public static void randomStudent(ArrayList<Student> array){ int index = (int)(Math.random()*array.size()); Student s = array.get(index); System.out.println(s.getName()+" "+s.getAge()); } //遍历学生信息 public static void printStudent(ArrayList<Student> array){ for(int i=0;i<array.size();i++){ Student s = array.get(i); System.out.println(s.getAge()+" "+s.getName()); } } //添加学生信息 public static void addStudents(ArrayList<Student> array){ Student s1 = new Student(); s1.setAge(201); s1.setName("张三1"); Student s2 = new Student(); s2.setAge(202); s2.setName("张三2"); Student s3 = new Student(); s3.setAge(203); s3.setName("张三3"); Student s4 = new Student(); s4.setAge(204); s4.setName("张三4"); Student s5 = new Student(); s5.setAge(205); s5.setName("张三5"); array.add(s5); array.add(s4); array.add(s3); array.add(s2); array.add(s1); } }
package com.tedu.demo; public class Student { /* * 定义私有成员变量 * 姓名,年龄 */ private String name; private int 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; } }