制作通讯录及遍历:
import static java.lang.System.*; class Student{ private String name; private int age; private String gender; private String phone; private String address; private String email; public void setName(String name){ this.name=name; } public String getName(){ return this.name; } public void setAge(int age){ this.age=age; } public int getAge(){ return this.age; } public void setGender(String gender){ this.gender=gender; } public String getGender(){ return this.gender; } public void setPhone(String phone){ this.phone=phone; } public String getPhone(){ return this.phone; } public void setAddress(String address){ this.address=address; } public String getAddress(){ return this.address; } public void setEmail(String email){ this.email=email; } public String getEmail(){ return this.email; } public Student(){ } public Student(String name,int age,String gender,String phone,String address,String email){ setName(name); setAge(age); setGender(gender); setPhone(phone); setAddress(address); setEmail(email); } public void eat(){ out.println(this.getAge()+"岁的"+this.getName()+"经常喜欢去"+this.getAddress()+"吃好吃的东西!"); } public void drink(){ out.println(this.getAge()+"岁的"+this.getName()+"经常喜欢去"+this.getAddress()+"喝东西!"); } public void play(){ out.println(this.getAge()+"岁的"+this.getName()+"经常喜欢去"+this.getAddress()+"游玩!"); } public void sleep(){ out.println(this.getAge()+"岁的"+this.getName()+"经常喜欢去"+this.getAddress()+"吃喝玩睡!"); } } public class AddressList{ public Student[] getArray(){ Student[] student=new Student[5]; Student p1=new Student(); p1.setName("张三"); p1.setAge(12); p1.setGender("男"); p1.setPhone("18519739201"); p1.setAddress("北京南锣鼓巷"); p1.setEmail("383795105@qq.com"); student[0]=p1; Student p2=new Student(); p2.setName("李四"); p2.setAge(15); p2.setGender("男"); p2.setPhone("18519739202"); p2.setAddress("北京南锣鼓巷后海"); p2.setEmail("383795105@qq.com"); student[1]=p2; Student p3=new Student(); p3.setName("王五"); p3.setAge(20); p3.setGender("男"); p3.setPhone("18519739203"); p3.setAddress("北京南锣鼓巷后海紫禁城"); p3.setEmail("383795105@qq.com"); student[2]=p3; Student s=new Student("赵六",30,"女","65465464112","北京颐和园","65432113484@163.com"); student[3]=s; Student t=new Student("刘七",35,"男","65465464112","北京颐和园八十条","65432113484@163.com"); student[4]=t; return student; } public void outprint(Student[] student){ Student p=new Student(); for(int i=0;i<student.length;i++){ p=student[i]; out.println(p.getName()); out.println(p.getAge()); out.println(p.getGender()); out.println(p.getPhone()); out.println(p.getAddress()); out.println(p.getEmail()); p.eat(); p.drink(); p.play(); p.sleep(); out.println(); } } public static void main(String[] args){ AddressList al=new AddressList(); al.outprint(al.getArray()); } }
运行结果:
热心技术,并兼吃喝,偶谈风月,不言国事.