Lesson_6 作业_1 ---- 封装学生类
1 /*********************************************************** 2 * Lesson_6 作业_1 3 * 2013-01-16 4 * by CocoonFan 5 * 6 ************************************************************ 7 *************************作业描述*************************** 8 * 9 * 定义了一个学生的类Student,包括成员变量“学号”、“班号”、 10 * “姓名”、“性别”、“年龄”,成员方法:“获得学号”、“获得班号”、 11 * “获得性别”、“获得姓名”、“获得年龄”。请为Student类定义构造 12 * 函数初始化所有的成员变量。增加一个方法“修改年龄”。 13 * boolean setStudentAge(int newAge)。新年龄如果在1至 14 * 100岁的话,将原来年龄修改为新年龄,并返回修改成功true的信息。 15 * 如果超出范围,显示“非法学生年龄”,并返回false的信息. 16 * 17 ************************************************************/ 18 19 public class TestStudent{ 20 public static void main(String []args){ 21 /* 22 for(int i = 0; i < 255; i++){ 23 System.out.print((char)i + "-->" + i+ " "); 24 }*/ 25 26 Student cocoonFan = new Student("CocoonFan","男","113224","ITE-1103",20); 27 System.out.println("这个人是正常的······"); 28 System.out.println("姓名:" + cocoonFan.getStudentName()); 29 System.out.println("性别:" + cocoonFan.getStudentSex()); 30 System.out.println("学号:" + cocoonFan.getStudentNumber()); 31 System.out.println("班号:" + cocoonFan.getClassNumber()); 32 System.out.println("年龄:" + cocoonFan.getStudentAge()); 33 System.out.println(); 34 35 Student wan = new Student(); 36 wan.setStudentSex("人妖"); 37 wan.setStudentAge(120); 38 System.out.println("姓名:" + wan.getStudentName()); 39 //System.out.println("性别:" + wan.getStudentSex()); 40 System.out.println("学号:" + wan.getStudentNumber()); 41 System.out.println("班号:" + wan.getClassNumber()); 42 System.out.println("年龄:" + wan.getStudentAge()); 43 44 } 45 } 46 47 class Student{ 48 private String studentName; 49 private String studentSex; 50 private String studentNumber; 51 private String classNumber; 52 private int studentAge; 53 54 //构造方法 55 public Student(){} 56 public Student(String studentName, String studentSex, 57 String studentNumber, String classNumber, int studentAge){ 58 this.studentName = studentName; 59 this.studentSex = studentSex; 60 this.studentNumber = studentNumber; 61 this.classNumber = classNumber; 62 this.studentAge = studentAge; 63 } 64 65 //get和set姓名 66 public void setStudentName(String studentName){ 67 this.studentName = studentName; 68 } 69 public String getStudentName(){ 70 return studentName; 71 } 72 73 //get和set性别 74 public void setStudentSex(String studentSex){ 75 if(!studentSex.equals("男") && !studentSex.equals("女")){ 76 System.out.println("非法设置性别,性别只能为 男或女。"); 77 }else{ 78 this.studentSex = studentSex; 79 } 80 } 81 public String getStudentSex(){ 82 if(studentSex.equals("男") || studentSex.equals("女")){ 83 return studentSex; 84 }else{ 85 return "性别未知\n"; 86 } 87 } 88 89 //get和set学号 90 public void setStudentNumber(String studentNumber){ 91 this.studentNumber = studentNumber; 92 } 93 public String getStudentNumber(){ 94 return studentNumber; 95 } 96 97 //get和set班级号 98 public void setClassNumber(String classNumber){ 99 this.classNumber = classNumber; 100 } 101 public String getClassNumber(){ 102 return classNumber; 103 } 104 105 //set和get年龄 106 public boolean setStudentAge(int newAge){ 107 if(newAge > 0 && newAge < 100){ 108 this.studentAge = newAge; 109 return true; 110 }else{ 111 System.out.println("非法学生年龄"); 112 return false; 113 } 114 } 115 public int getStudentAge(){ 116 if(studentAge == 0){ 117 System.out.println("学生年龄尚未设置!"); 118 return -1; 119 }else{ 120 return studentAge; 121 } 122 } 123 124 }
运行结果