《Java疯狂讲义》第五章面向对象(上)作业
1、编写一个学生类,提供name,age,gender,phone,address,email成员变量,且为每个成员变量提供setter、getter方法。为学生提供默认构造器和带有所有成员变量的构造器。为学生提供方法,由于描绘吃、喝、玩、睡等行为。
1 class Student 2 { 3 String name; 4 int age; 5 String gender; 6 String phone; 7 String address; 8 String email; 9 10 public Student() 11 { 12 13 } 14 public Student(String name,int age,String gender,String phone,String address,String email) 15 { 16 this.name=name; 17 this.age=age; 18 this.gender=gender; 19 this.phone=phone; 20 this.address=address; 21 this.email=email; 22 } 23 24 protected void setter(String name,int age,String gender,String phone,String address,String email) 25 { 26 this.name=name; 27 this.age=age; 28 this.gender=gender; 29 this.phone=phone; 30 this.address=address; 31 this.email=email; 32 } 33 34 protected void getter() 35 { 36 System.out.println("学生:"+this.name); 37 System.out.println("性别:"+this.gender); 38 System.out.println("电话号:"+phone); 39 System.out.println("地址:"+this.address); 40 System.out.println("email:"+email); 41 } 42 43 protected void eat() 44 { 45 System.out.println(this.name+"正在吃东西"); 46 } 47 protected void play() 48 { 49 System.out.println(this.name+"正在玩"); 50 } 51 52 } 53 54 public class StudentTest 55 { 56 public static void main(String[] args) 57 { 58 var st1=new Student(); 59 st1.getter(); 60 Student st2=new Student("李刚",20,"男","13438310326","北京","1234567891@163.com"); 61 st2.getter(); 62 st2.eat(); 63 st2.play(); 64 65 66 } 67 }
2、利用第一题的学生类,定义一个Student[]数组保存多个Student对象作为通讯录数据。程序可通过name、email、address查询,如果找不到数据则进行友好提示。
1 class StudentResearch 2 { 3 public Student nameSearch(Student[] st,String name) 4 { 5 Student result=new Student(); 6 for(int i=0;i<st.length;i++) 7 { 8 if(st[i].name==name) 9 { 10 result=st[i]; 11 } 12 } 13 return result; 14 } 15 public Student emialSearch(Student[] st,String email) 16 { 17 Student result=new Student(); 18 for(int i=0;i<st.length;i++) 19 { 20 if(st[i].email==email) 21 { 22 result=st[i]; 23 } 24 } 25 return result; 26 } 27 public Student addressSearch(Student[] st,String address) 28 { 29 Student result=new Student(); 30 for(int i=0;i<st.length;i++) 31 { 32 if(st[i].address==address) 33 { 34 result=st[i]; 35 } 36 } 37 return result; 38 } 39 40 public static void main(String[] args) 41 { 42 Student[] st=new Student[5]; 43 st[0]=new Student("李刚",20,"男","13438310321","北京","1234567891@163.com"); 44 st[1]=new Student("李明",20,"男","13438310322","上海","1234567892@163.com"); 45 st[2]=new Student("李二",20,"男","13438310323","广东","1234567893@163.com"); 46 st[3]=new Student("李三",20,"男","13438310324","重庆","1234567894@163.com"); 47 st[4]=new Student("李四",20,"男","13438310325","成都","1234567895@163.com"); 48 49 //姓名查找 50 var p=new StudentResearch(); 51 Student namefind=p.nameSearch(st,"李二"); 52 if(namefind.name==null) 53 { 54 System.out.println("未查找到该用户"); 55 } 56 else 57 namefind.getter(); 58 59 //邮箱查找 60 Student emailfind=p.emialSearch(st,"1234567894@163.com"); 61 if(emailfind.email==null) 62 { 63 System.out.println("未查找到该用户"); 64 } 65 else 66 emailfind.getter(); 67 68 //地址查找 69 Student addressfind=p.addressSearch(st,"北京"); 70 if(addressfind.address==null) 71 { 72 System.out.println("未查找到该用户"); 73 } 74 else 75 addressfind.getter(); 76 } 77 }
3、定义普通人、老师、班主任、学生、学校这些类,提供适当的成员变量、方法描述其内部数据和行为特征,并提供主类使之运行。要求良好的封装性,将不同的类放到不同包下,增加文档注释,生成API文档。
Person类:
1 package org.one; 2 /* 3 *Description 这是一个普通的类,用于派生其他类 4 *@author star 5 *@version 1.0 6 */ 7 public class Person 8 { 9 //成员变量,保存姓名 10 protected String name; 11 //Person类的成员变量,保存年纪 12 protected int age; 13 14 /* 15 *描述人吃的方法 16 *@param name 指明谁在吃 17 *@param food 指明吃的食物 18 *@return void 19 */ 20 21 /* 22 *Person类的构造器 23 *@param name 初始化姓名 24 *@param age 初始化年龄 25 */ 26 public Person(String name,int age) 27 { 28 this.name=name; 29 this.age=age; 30 } 31 public void eat(String name,String food) 32 { 33 System.out.println(name+"正在吃"+food); 34 } 35 36 /* 37 *描述人喝的方法 38 *@return void 39 */ 40 public void drink() 41 { 42 System.out.println(name+"正在喝水"); 43 } 44 45 /* 46 *描述人玩的方法 47 *@return void 48 */ 49 public void eat() 50 { 51 System.out.println(name+"正在玩泥巴"); 52 } 53 54 /* 55 *描述人方便的方法 56 *@return void 57 */ 58 public void goToLoo() 59 { 60 System.out.println(name+"正在方便"); 61 } 62 63 /* 64 *描述人信息的方法 65 *@return void 66 */ 67 public void getter() 68 { 69 System.out.println("姓名:"+name+",年龄:"+age); 70 } 71 }
Student类:
1 package org.two; 2 import org.one.Person; 3 /* 4 *Description 学生类 5 *@author star 6 *@version 1.0 7 */ 8 public class Student extends Person 9 { 10 //成员变量——年级 11 public int grade; 12 /* 13 *学生类的构造器 14 */ 15 public Student(String name,int age,int grade) 16 { 17 super(name,age); 18 this.grade=grade; 19 } 20 21 /* 22 *描述学生学习的方法 23 *@param name 描述谁在学习 24 *@return void 25 */ 26 public void study() 27 { 28 System.out.println(name+"正在学习"); 29 } 30 public void getter() 31 { 32 System.out.println("身份:学生"); 33 System.out.println("姓名:"+name+",年龄:"+age+",年级:"+grade); 34 } 35 36 }
Teacher类:
1 package org.three; 2 import org.one.Person; 3 4 public class Teacher extends Person 5 { 6 public Teacher(String name,int age) 7 { 8 super(name,age); 9 } 10 11 /* 12 *描述学生学习的方法 13 *@param name 描述谁在教学 14 *@return void 15 */ 16 public void teache() 17 { 18 System.out.println(name+"老师正在上课"); 19 } 20 public void getter() 21 { 22 System.out.println("身份:老师"); 23 System.out.println("姓名:"+name+",年龄:"+age); 24 } 25 26 }
School类:
1 package org.four; 2 import org.two.*; 3 import org.three.*; 4 public class School 5 { 6 7 protected Teacher[] Te; 8 protected Student[] St; 9 10 //登记学生信息 11 public School(Teacher[] Te,Student[] St) 12 { 13 this.Te=Te; 14 this.St=St; 15 } 16 public void getter() 17 { 18 for(int i=0;i<Te.length;i++) 19 { 20 Te[i].getter(); 21 } 22 for(int i=0;i<St.length;i++) 23 { 24 St[i].getter(); 25 } 26 } 27 }
主类入口:
1 import org.one.Person; 2 import org.two.Student; 3 import org.three.Teacher; 4 import org.four.School; 5 class Test 6 { 7 public static void main(String[] args) 8 { 9 Student s1=new Student("李一",12,6); 10 Student s2=new Student("李二",13,7); 11 Student s3=new Student("李三",14,8); 12 Student[] ss={s1,s2,s3}; 13 14 Teacher t1=new Teacher("张三",30); 15 Teacher t2=new Teacher("张四",35); 16 Teacher t3=new Teacher("张五",36); 17 Teacher[] ts={t1,t2,t3}; 18 19 School sch=new School(ts,ss); 20 sch.getter(); 21 22 s1.study(); 23 t1.teache(); 24 25 s2.drink(); 26 } 27 }
4、改写第一题的类,利用组合来实现复用
1 class Student 2 { 3 String name; 4 int age; 5 String gender; 6 String phone; 7 String address; 8 String email; 9 10 public Student() 11 { 12 13 } 14 public Student(String name,int age,String gender,String phone,String address,String email) 15 { 16 this.name=name; 17 this.age=age; 18 this.gender=gender; 19 this.phone=phone; 20 this.address=address; 21 this.email=email; 22 } 23 24 protected void setter(String name,int age,String gender,String phone,String address,String email) 25 { 26 this.name=name; 27 this.age=age; 28 this.gender=gender; 29 this.phone=phone; 30 this.address=address; 31 this.email=email; 32 } 33 34 protected void getter() 35 { 36 System.out.println("学生:"+this.name); 37 System.out.println("性别:"+this.gender); 38 System.out.println("电话号:"+phone); 39 System.out.println("地址:"+this.address); 40 System.out.println("email:"+email); 41 } 42 43 protected void eat() 44 { 45 System.out.println(this.name+"正在吃东西"); 46 } 47 protected void play() 48 { 49 System.out.println(this.name+"正在玩"); 50 } 51 52 } 53 54 class Find 55 { 56 private Student[] students; 57 public Find(Student[] students) 58 { 59 this.students=students; 60 } 61 62 public Student NameFind(String name) 63 { 64 Student result=null; 65 for(Student stu:students) 66 { 67 if(stu.name==name) 68 { 69 result=stu; 70 } 71 } 72 return result; 73 } 74 75 public Student EmailFind(String email) 76 { 77 Student result=null; 78 for(Student stu:students) 79 { 80 if(stu.email==email) 81 { 82 result=stu; 83 } 84 } 85 return result; 86 } 87 88 public Student PhoneFind(String phone) 89 { 90 Student result=null; 91 for(Student stu:students) 92 { 93 if(stu.phone==phone) 94 { 95 result=stu; 96 } 97 } 98 return result; 99 } 100 } 101 public class StudentTest 102 { 103 public static void main(String[] args) 104 { 105 106 Student st1=new Student("李一",20,"男","13438310321","北京","1234567891@163.com"); 107 Student st2=new Student("李二",20,"男","13438310322","北京","1234567892@163.com"); 108 Student st3=new Student("李三",20,"男","13438310323","北京","1234567893@163.com"); 109 Student st4=new Student("李四",20,"男","13438310324","北京","1234567894@163.com"); 110 Student st5=new Student("李五",20,"男","13438310325","北京","1234567895@163.com"); 111 Student[] stus={st1,st2,st3,st4,st5}; 112 113 Find F=new Find(stus); 114 var bool1=(F.NameFind("李二").name==null); 115 if(bool1) 116 System.out.println("查无此人"); 117 else 118 F.NameFind("李二").getter(); 119 120 var bool2=(F.PhoneFind("13438310329")==null); 121 if(bool2) 122 System.out.println("查无此人"); 123 else 124 F.PhoneFind("13438310329").getter(); 125 } 126 }

浙公网安备 33010602011771号