知识总结:
本题在之前的基础上增加案例:统计总共有多少学生报名
主要改造在subject.java中:
1.属性增加:
private Student[]myStudents;
privateint studentNum;
2.构造方法增加:
//带4参构造:实现对学科名称、编号、学制年限、学生信息赋值
public Subject(Stringname, String subjectNo,int subjectLife,Student[]myStudents) {
// this.subjectName=subjectName;
this.setSubjectName(name);// 安全起见
this.setSubjectNo(subjectNo);
this.setSubjectLife(subjectLife);
this.setMyStudents(myStudents);
}
getter/setter方法增加:
public Student[] getMyStudents() {
if(this.myStudents==null)//如果没有初始化,实例化开辟空间
{
this.myStudents=new Student[200];
}
returnmyStudents;
}
public void setMyStudents(Student[] myStudents) {
this.myStudents =myStudents;
}
//学生个数
public int getStudentNum() {
returnstudentNum;
}
public void setStudentNum(int studentNum) {
this.studentNum =studentNum;
}
add方法增加:
public void addStudent(Studentstu)
{
/*
* 1,将学生保存在数组中
* 2,将学生个数保存到studentNum[]数组当中
*/
//1.
for(inti=0;i<this.getMyStudents().length;i++)
{
if(this.getMyStudents()[i]==null)//找到空地,插入学生
{
this.getMyStudents()[i]=stu;
//2.
this.studentNum=i+1;
return;
}
}
}
subject.java
package com.imooc.model;
/**
* 专业类
* @author zhang ziwei
*
*/
public class Subject {
// 成员属性:学科名称,学科编号,学制年限,报名选修的学生信息、报名选修的学生个数
private String subjectName;
private String subjectNo;
private int subjectLife;
private Student[] myStudents;
private int studentNum;
//无参构造
public Subject() {
}
//带3参构造:实现对学科名称、编号、学制年限赋值
public Subject(String name, String subjectNo, int subjectLife) {
// this.subjectName=subjectName;
this.setSubjectName(name);// 安全起见
this.setSubjectNo(subjectNo);
this.setSubjectLife(subjectLife);
}
//带4参构造:实现对学科名称、编号、学制年限、学生信息赋值
public Subject(String name, String subjectNo, int subjectLife,Student[] myStudents) {
// this.subjectName=subjectName;
this.setSubjectName(name);// 安全起见
this.setSubjectNo(subjectNo);
this.setSubjectLife(subjectLife);
this.setMyStudents(myStudents);
}
/**
* 获取选修专业的学生信息的,如果保存的学生信息未被初始化,则先初始化长度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;
}
// 名称
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;
}
public void setSubjectLife(int subjectLife) {
if (subjectLife <= 0) {
return;
}
this.subjectLife = subjectLife;
}
/**
* 专业介绍的方法
*
* @return 专业介绍的相关信息,包括名称、编号、年限
*/
public String info() {
String str = "专业信息如下:\n专业名称:" + this.getSubjectName() + "\n专业编号:" + this.getSubjectNo() + "\n学制年限:"
+ this.getSubjectLife();// 对于很长的字符串打印可以用command+shift+f(ctrl+shift+f)格式化使得格式更好看
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)//找到空地,插入学生
{
this.getMyStudents()[i]=stu;
//2.
this.studentNum=i+1;
return;
}
}
}
}
Student.java:
package com.imooc.model;
public class Student {
// 成员属性:学号,姓名,性别,年龄
private String student_id;
private String student_name;
private String student_sex;
private int student_age;
//实现关联学生,学科关联的,解决方法3:将专业信息作为成员属性存在
private Subject student_subject;//注意,类型是一个对象
// 无参数构造
public Student() {
super();
}
// 4参构造,实现对学号,姓名,年龄赋值,创建对象的赋值
public Student(String student_id, String student_name, String student_sex, int student_age,Subject student_subject) {
this.setStudent_id(student_id);
this.setStudent_name(student_name);
this.setStudent_sex(student_sex);
this.setStudent_age(student_age);
this.setStudent_subject(student_subject);
}
//3参构造,实现对学号,姓名,年龄赋值
public Student(String student_id, String student_name, String student_sex, int student_age) {
this.setStudent_id(student_id);
this.setStudent_name(student_name);
this.setStudent_sex(student_sex);
this.setStudent_age(student_age);
}
// getter/setter接口
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getStudent_sex() {
return student_sex;
}
public void setStudent_sex(String student_sex) {
// 限制性别只能是男或者女,反之我们强制性男
if (student_sex.equals("男") | student_sex.equals("女")) // 也可以写成(student_sex="男"student_sex="女")
{
this.student_sex = student_sex;
} else
this.student_sex = "男";
}
public int getStudent_age() {
return student_age;
}
public void setStudent_age(int student_age) {
if (student_age <= 60 & student_age >= 10) {
this.student_age = student_age;
} else
this.student_age = 18;// 强制赋值
}
/**
* 获取专业对象,如果没有实例化,先实例化再返回
* @return 专业对象信息
*/
public Subject getStudent_subject(){
if(this.student_subject==null)//防止getStudent_subject()是没有实例化的
{
this.student_subject=new Subject();//无参构造的重要性体现出来
}
return student_subject;
}
public void setStudent_subject(Subject student_subject) {
this.student_subject = student_subject;
}
/**无参
* 学生自我介绍
*
* @return 自我介绍的信息,包括姓名,学号,性别,年龄
*/
public String introduction() {
String str = "学生信息如下:\n姓名:" + this.getStudent_name() + "\n学号:" + this.getStudent_id() + "\n性别:"
+ this.getStudent_sex() + "\n年龄:" + this.getStudent_age()+"\n所报专业名称:" + this.getStudent_subject().getSubjectName() + "\n学制年限:"
+ this.getStudent_subject().getSubjectLife();//!!尤其注意这种表示先调用对象,再通过对象调用方法
return str;
}
/**解决方法1:参数传参
* 学生自我方法介绍,实现学生学科相关联
* @param subject_name
* @param subject_life
* @return 自我介绍的信息,包括姓名,学号,性别,年龄,所学专业名称,学制年限
*/
public String introduction(String subject_name, int subject_life) {
String str = "学生信息如下:\n姓名:" + this.getStudent_name() + "\n学号:" + this.getStudent_id() + "\n性别:"
+ this.getStudent_sex() + "\n年龄:" + this.getStudent_age() + "\n所报专业名称:" + subject_name + "\n学制年限:"
+ subject_life;
return str;
}
/**解决方法2:对象传参
* 学生自我方法介绍,实现学生学科相关联
* @param mySubject(构建了一个对象)
* @return自我介绍的信息,包括姓名,学号,性别,年龄,所学专业名称,学制年限
*/
public String introduction(Subject mySubject){
String str = "学生信息如下:\n姓名:" + this.getStudent_name() + "\n学号:" + this.getStudent_id() + "\n性别:"
+ this.getStudent_sex() + "\n年龄:" + this.getStudent_age() + "\n所报专业名称:" + mySubject.getSubjectName() + "\n学制年限:"
+ mySubject.getSubjectLife();
return str;
}
}
SchoolTest.java:
package com.imooc.test;
import com.imooc.model.*;//导入所有类
public class SchoolTest {
public static void main(String[] args) {
//测试subject类
Subject sub1=new Subject("计算机科学与应用","J0001",4);
System.out.println(sub1.info());
System.out.println("=====================================");
//测试学生
Student stu1=new Student("cs6518822","刘华华","女",20);
System.out.println(stu1.introduction());
System.out.println("=====================================");
//测试关联学生1,方法中传入参数
Student stu2=new Student("cs6518732","张子蔚","男",17);
System.out.println(stu2.introduction("计算机科学与应用",4));
System.out.println("=====================================");
//测试关联学生2,方法中传入对象
Student stu3=new Student("cs6518797","胡京督","男",21);
System.out.println(stu3.introduction(sub1));
System.out.println("=====================================");
//测试关联学生3,构造方法中传入对象
Student stu4=new Student("cs6518779","刘凯瑞","男",20,sub1);
System.out.println(stu4.introduction());
System.out.println("=====================================");
//测试指定专业的中到底有多少学生报名
sub1.addStudent(stu1);
sub1.addStudent(stu2);
sub1.addStudent(stu3);
sub1.addStudent(stu4);
System.out.println("计算机科学与应用的专业中已有:"+sub1.getStudentNum()+"学生进行报名");
}
}
执行情况:(最后一排显示报名总数)