JAVA基础语法07
笔记 学生信息管理
package com.azj.model;
public class Student {
// 成员属性:学号,姓名,性别,年龄,专业
private String studentNo;
private String studentName;
private String studentSex;
private int studentAge;
private Subject studentSubject; //方案3 关联性强
// 无参构造
public Student() {
}
// 有参构造
public Student(String studentNo, String studentName, String studentSex, int studentAge) {
this.setStudentName(studentName);
this.setStudentNo(studentNo);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
}
/**
* 添加限制判断:null,进行实例化
* @return 专业对象信息
*/
public Subject getStudentSubject() {
if(this.studentSubject==null)
this.studentSubject = new Subject();
return studentSubject;
}
public void setStudentSubject(Subject studentSubject) {
this.studentSubject = 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;
}
/**
* 判断性别:default male, .equals()比较的类型只能为String类型。
* @param studentSex
*/
public void setStudentSex(String studentSex) {
if(studentSex.equals("female"))
this.studentSex = "female";
else
this.studentSex = "male";
}
public int getStudentAge() {
return studentAge;
}
/**
* 给年龄限制:10-100,default 18
* @param studentAge
*/
public void setStudentAge(int studentAge) {
if(studentAge<10 || studentAge>100)
this.studentAge = 18;
else
this.studentAge = studentAge;
}
/**
* 学生自我介绍
* @return 返回学生的相关信息(学号,姓名,性别,年龄)
*/
public String introduction() {
String str = "学生信息如下:\n学号:" + this.getStudentNo() + "\n姓名:" + this.getStudentName() + "\n性别:"
+ this.getStudentSex() + "\n年龄:" + this.getStudentAge()+"\n"+this.getStudentSubject().inform();
return str;
}
/**
* 通过类对象引用获取相关信息
* @param subject
* @return 返回学生的相关信息(学号,姓名,性别,年龄,专业名称,专业编号,学期年限)
*/
public String introduction(Subject subject) {
String str = "学生信息如下:\n学号:" + this.getStudentNo() + "\n姓名:" + this.getStudentName() + "\n性别:"
+ this.getStudentSex() + "\n年龄:" + this.getStudentAge()+"\n专业名称:" + subject.getSubjectName() + "\n专业编号:" + subject.getSubjectNo() + "\n学制年限:"
+ subject.getSubjectLife();;
return str;
}
}
package com.azj.model;
/**
* 专业类
*
* @author zzh
*
*/
public class Subject {
// 成员属性:学科名称,编号,年限,报名学生信息,报名学生个数
private String subjectName;
private String subjectNo;
private int subjectLife;
private Student[] myStudent;
private int studentNum;
// 无参构造
public Subject() {
}
// 带参构造,实现对属性的全部赋值
public Subject(String subjectName, String subjectNo, int subjectLife) {
// 建议使用set方法赋值,在set方法中有时会设置限制要求
this.setSubjectName(subjectName);
this.setSubjectNo(subjectNo);
this.setSubjectLife(subjectLife);
}
/**
* 获取选修学生学习,如果未初始化则进行初始化
*
* @return 学生信息
*/
public Student[] getMyStudent() {
if (this.myStudent == null)
this.myStudent = new Student[10];
return myStudent;
}
public void setMyStudent(Student[] myStudent) {
this.myStudent = myStudent;
}
public int getStudentNum() {
return studentNum;
}
public void setStudentNum(int studentNum) {
this.studentNum = studentNum;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = 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;
}
/**
* 专业介绍的方法
*
* @return 专业介绍相关信息,包括名称,编号,年限
*/
public String inform() {
String str = "专业信息如下:\n专业名称:" + this.getSubjectName() + "\n专业编号:" + this.getSubjectNo() + "\n学制年限:"
+ this.getSubjectLife();
return str;
}
/**
* 1. 将学生保存到数组中 2. 学生个数保存到studentNum
*
* @param stu
*/
public void addStudent(Student stu) {
for (int i = 0; i < this.getMyStudent().length; i++)
if (this.getMyStudent()[i] == null) {
// 学生与专业之间的关联。this 指的是当前调用该方法的对象 ,双向连接
stu.setStudentSubject(this);
this.getMyStudent()[i] = stu;
this.studentNum = i + 1;
return;
}
}
}
package com.ajz.test;
import com.azj.model.*;;
public class schoolTest {
public static void main(String[] args) {
// 测试 Subject
Subject sub1 = new Subject("计算机学习与技术","J0001",4);
// System.out.println(sub1.inform());
System.out.println("-----------------------------");
// Student stud = new Student("0001","小明","333",8);
// System.out.println(stud.introduction());
// System.out.println("-----------------------------");
// System.out.println(new Student("0002","tom","female",19).introduction(sub1));
System.out.println("-----------------------------");
Student stud = new Student("0002","Tom","female",19);
Student stud2 = new Student("0002","Tom","female",19);
// System.out.println(stud.introduction());
sub1.addStudent(stud);
sub1.addStudent(stud2);
System.out.println(sub1.getStudentNum());
}
}