案例简介
通过一个简单的案例具体的实现一下封装。
通过java语言和面向对象的思想,模拟一个场景的实现。
案例: 学校开设了计算机科学与应用这个专业,专业编号: J0001;学制年限: 4年;
现在有三个学生报名了该学校。
实现的效果图:
package cn.mtianyan.computer;public class Subject { public Subject(String name, String code, int year){ this.setName(name); this.setCode(code); this.setYear(year); } public void showInfo(){ System.out.println("专业信息如下:"); System.out.println("专业名称: " + this.name); System.out.println("专业编号: " + this.code); System.out.println("学制年限: " + this.year +"年"); System.out.println("====================="); } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } private String code; private int year; }
package cn.mtianyan.computer;public class Student { public Student(String name,String studentID, String sex,int age,Subject subject){ this.setName(name); this.setStudentID(studentID); this.setAge(age); this.setSex(sex); this.subject = subject; } public void showInfo(){ System.out.println("=================="); System.out.println("姓名: "+name); System.out.println("学号: "+studentID); System.out.println("性别: "+sex); System.out.println("年龄: "+age); System.out.println("所报专业名称: "+ subject.getName()); System.out.println("学制年限: "+ subject.getYear()); } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStudentID() { return studentID; } public void setStudentID(String studentID) { this.studentID = studentID; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } private String studentID; private String sex; private int age; private Subject subject; }
package cn.mtianyan.computer;public class Test { public static void main(String[] args) { Subject subject = new Subject("计算机科学与应用","J0001",4); subject.showInfo(); Student student1 = new Student("张三","S01","男",18, subject); student1.showInfo(); Student student2 = new Student("李四","S02","女",17, subject); student2.showInfo(); Student student3 = new Student("王五","S03","男",18, subject); student3.showInfo(); } }
运行结果:
上面是我个人的实现.
综合案例
计算机科学与应用是一个对象。 三个学生是三个对象。学科专业类 & 专业类
类:
专业: 专业名称、编号、学制年限
学生: 姓名、学号、性别、年龄
编写Subject类
啊啊啊,英语真季二茶,Get到了。Subject是学科。
分包存储: cn.mtianyan.model cn.mtianyan.test
package cn.mtianyan.model;/** * 专业类 * @author mtianyan */public class Subject { // 成员属性:学科名称、学科编号、学制年限、报名选修的学生信息、报名选修的学生个数 private String subjectName; private String subjectNo; private int subjectLife; private Student[] myStudents; // 专业学生数组 private int studentNum; // 无参构造方法 public Subject() { } // 带参构造,带参构造,实现对全部属性的赋值 public Subject(String subjectName, String subjectNo, int subjectLife) { // this.subjectName=subjectName; this.setSubjectName(subjectName); this.setSubjectNo(subjectNo); this.setSubjectLife(subjectLife); } public void setSubjectName(String subjectName) { this.subjectName = subjectName; } public String getSubjectName() { return this.subjectName; } public String getSubjectNo() { return subjectNo; } public void setSubjectNo(String subjectNo) { this.subjectNo = subjectNo; } public int getSubjectLife() { return subjectLife; } // 设置学制年限,限制必须>0 public void setSubjectLife(int subjectLife) { if (subjectLife <= 0) return; this.subjectLife = subjectLife; } /** * 获取选修专业的学生信息 如果保存学生信息的数组未被初始化,则,先初始化长度200 * @return 保存学生信息的数组 */ public Student[] getMyStudents() { if(this.myStudents==null) this.myStudents=new Student[200]; return myStudents; } public void setMyStudents(Student[] myStudents) { this.myStudents = myStudents; } public int getStudentNum() { return studentNum; } public void setStudentNum(int studentNum) { this.studentNum = studentNum; } /** * 专业介绍的方法 * @return 专业介绍的相关信息,包括名称、编号、年限 */ public String info() { String str = "专业信息如下:\n专业名称:" + this.getSubjectName() + "\n专业编号:" + this.getSubjectNo() + "\n学制年限:" + this.getSubjectLife() + "年"; return str; } public void addStudent(Student stu){ /* * 1、将学生保存到数组中 * 2、将学生个数保存到studentNum * */ //1、将学生保存到数组中 for(int i=0;i<this.getMyStudents().length;i++){ if(this.getMyStudents()[i]==null){ stu.setStudentSubject(this); this.getMyStudents()[i]=stu; //2、将学生个数保存到studentNum this.studentNum=i+1; return; } } } }
这里我们的info方法在设计的时候返回String而不是直接在方法内打印,体现了单一职责原则,这样的设计使得这些字符串如果后期不是在控制台打印,而是做其他处理更方便。
编写Student类
package cn.mtianyan.model;public class Student { // 成员属性:学号、姓名、性别、年龄、专业 private String studentNo; private String studentName; private String studentSex; private int studentAge; private Subject studentSubject; // 无参构造方法 public Student() { } //多参构造方法,实现对学号、姓名、性别、年龄的赋值 public Student(String studentNo, String studentName, String studentSex, int studentAge) { this.setStudentNo(studentNo); this.setStudentName(studentName); this.setStudentSex(studentSex); this.setStudentAge(studentAge); } // 多参构造方法,实现对全部属性的赋值 public Student(String studentNo, String studentName, String studentSex, int studentAge,Subject studentSubject) { this.setStudentNo(studentNo); this.setStudentName(studentName); this.setStudentSex(studentSex); this.setStudentAge(studentAge); // this.studentAge=studentAge; this.setStudentSubject(studentSubject); } public String getStudentNo() { return studentNo; } public void setStudentNo(String studentNo) { this.studentNo = studentNo; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getStudentSex() { return studentSex; } public void setStudentSex(String studentSex) { // 限制性别只能是“男”或者“女”,反之,强制赋值为“男” if(studentSex.equals("男") | studentSex.equals("女")){ this.studentSex = studentSex; }else { this.studentSex = "男"; } } public int getStudentAge() { return studentAge; } /** * 给年龄赋值,限定必须在10--100之间,反之赋值为18 * * @param studentAge * 传入的年龄 */ public void setStudentAge(int studentAge) { if (studentAge < 10 || studentAge > 100) this.studentAge = 18; else this.studentAge = studentAge; } /** * 获取专业对象,如果没有实例化,先实例化后再返回 * @return 专业对象信息 */ public Subject getStudentSubject() { if(this.studentSubject==null) this.studentSubject=new Subject(); return studentSubject; } public void setStudentSubject(Subject studentSubject) { this.studentSubject = studentSubject; } /** * 学生自我介绍的方法 * * @return 自我介绍的信息,包括姓名、学号、性别、年龄 */ public String introduction() { String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:" + this.getStudentSex() + "\n年龄:" + this.getStudentAge()+ "\n所报专业名称:" + this.getStudentSubject().getSubjectName() + "\n学制年限:" + this.getStudentSubject().getSubjectLife(); return str; } /** * 学生自我介绍的方法 * @param subjectName 所学专业名称 * @param subjectLife 学制年限 * @return 自我介绍的信息,包括姓名、学号、性别、年龄、所学专业名称、学制年限 */ public String introduction(String subjectName, int subjectLife) { String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:" + this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n所报专业名称:" + subjectName + "\n学制年限:" + subjectLife; return str; } /** * 学生自我介绍的方法 * @param mySubject 所选专业的对象 * @return自我介绍的信息,包括姓名、学号、性别、年龄、所学专业名称、学制年限 */ public String introduction(Subject mySubject){ String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:" + this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n所报专业名称:" + mySubject.getSubjectName() + "\n学制年限:" + mySubject.getSubjectLife()+"\n专业编号:"+mySubject.getSubjectNo(); return str; } }
作者:天涯明月笙
链接:https://www.jianshu.com/p/8610f0818a99
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦