Java学习之路
2020年12月17日22:45:09
Student类:
package javasubject;
import javasubject.*;
import javasubjecttest.studentTest;
public class Student {
//成员属性:
//学号、姓名、性别、年龄
private String studentNo;
private String studentName;
private String studentSex;
private int studentAge;
private Subject studentSubject;
// ========================================================================================================================
//无参构造
public Student(){}
//多参数(5)构造
public Student(String studentName,String studentNo,String studentSex,int studentAge,Subject studentSubject){
this.setStudentName(studentName);
this.setStudentNo(studentNo);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
this.setStudentSubject(studentSubject);
}
//多参数(4)构造
public Student(String studentName,String studentNo,String studentSex,int studentAge){
this.setStudentName(studentName);
this.setStudentNo(studentNo);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
//this.setStudentSubject(studentSubject);
}
// ======================================================================================================================================
// =====get和set方法=====
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){
this.studentSex = studentSex;
}
public int getStudentAge(){
return studentAge;
}
public void setStudentAge(int studentAge){
if(studentAge<10 || studentAge>100){
this.studentAge=18;//限制年龄,其他同理
}else
this.studentAge = studentAge;
}
// ===============================================================
//获取专业对象,如果没有实例化,先实例化之后再返回
// ===========专业对象信息============================
public Subject getStudentSubject() {
if(this.studentSubject==null)
this.studentSubject=new Subject();
return studentSubject;
}
public void setStudentSubject(Subject studentSubject) {
this.studentSubject = studentSubject;
}
// ================================================================
// 学生自我介绍的方法
// subjectName
// subjectLife
// 自我介绍的信息,包括姓名、学号、性别、年龄、所学专业名称、学制年限
// ==========================================================================================================================================
// 方法一:
public String introduction(String subjeectName,int subjectLife){
String str="该学生的个人信息为:\n姓名:"+this.getStudentName()+"\n学号:"+this.getStudentNo()+"\n性别:"
+this.getStudentSex()+"\n年龄:"+this.getStudentAge()+"\n"
+"所报的专业:"+subjeectName+"\n学制:"+subjectLife;
return str;
}
// ==========================================================================================================================================
// 方法二:
public String introduction(Subject mySubject){
String str="该学生的个人信息为:\n姓名:"+this.getStudentName()+"\n学号:"+this.getStudentNo()+"\n性别:"
+this.getStudentSex()+"\n年龄:"+this.getStudentAge()+"\n"
+"所报的专业:"+mySubject.getSubjectName()+"\n专业名称:"+mySubject.getSubjectName()+"\n专业编号:"+mySubject.getSubjectNo();
return str;
}
// ===========================================================================================================================================
//方法三
public String introduction(){
String str="该学生的个人信息为:\n姓名:"+this.getStudentName()+"\n学号:"+this.getStudentNo()+"\n性别:"
+this.getStudentSex()+"\n年龄:"+this.getStudentAge()+"\n专业名称:"
+this.getStudentSubject().getSubjectName()+"\n专业编号:"+this.getStudentSubject().getSubjectNo();
return str;
}
}
Subject类:
package javasubject;
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.setSubjectName(subjectName);
this.setSubjectNo(subjectNo);
this.setSubjectLife(subjectLife);
}
// 带参构造,学科名称,学科编号,学科学年,
public Subject(String subjectName,String subjectNo,int subjectLife,Student[] myStudents){
this.setSubjectName(subjectName);
this.setSubjectNo(subjectNo);
this.setSubjectLife(subjectLife);
this.setMyStudents(myStudents);
}
//设置名称
public void setSubjectName(String subjectName){
this.subjectName=subjectName;
}
public String getSubjectName(){
return this.subjectName;
}
//设置学号
public void setSubjectNo(String subjectNo){
this.subjectNo=subjectNo;
}
public String getSubjectNo() {
return this.subjectNo;
}
//设置学年;要求学年>0
public void setSubjectLife(int subjectLife){
if(subjectLife<=0)
return;
this.subjectLife=subjectLife;
}
public int getSubjectLife() {
return this.subjectLife;
}
// 获取选修专业的学生信息,如果保存学生信息的数组未被初始化,则先初始化长度200
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 String info(){
String str="专业信息如下:\n专业名称:"+this.getSubjectName()+"\n专业编号:"+this.getSubjectNo()+"\n专业学年:"+this.getSubjectLife()+"年\n";
return str;
}
public void addStudent(Student stu){
/**
* 1、将学生保存到数组中
* 2、将学生个数保存到studentNum
*/
//将学生保存到数组中
for(int i=0;i<this.getMyStudents().length;i++){
if(this.getMyStudents()[i]==null){
this.getMyStudents()[i]=stu;
//将学生个数保存到studentNum中
this.studentNum=i+1;
return;
}
}
}
}
测试类
Subjecttest:
package javasubjecttest;
import javasubject.Student;
import javasubject.Subject;
public class Subjecttest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("======================================");
Subject sub1=new Subject("计算机科学与应用","Z202",2);
Subject sub2=new Subject("软件工程","Z201",4);
System.out.println(sub1.info());
System.out.println("======================================");
System.out.println(sub2.info());
System.out.println("======================================");
// 测试Student类
Student stu1 = new Student("张三","0014","男",21,sub2);
System.out.println(stu1.introduction());
// 方法一:
System.out.println("======================================");
Student stu2 = new Student("大饼子","0001","男",18,sub2);
System.out.println(stu2.introduction("计算机科学与应用",2));
System.out.println("======================================");
// 方法二:
Student stu3 = new Student("大饼子一号","0002","男",18);
System.out.println(stu3.introduction(sub1));
System.out.println("======================================");
// 测试指定专业中到底有多少学生报名
sub1.addStudent(stu1);
sub1.addStudent(stu2);
sub2.addStudent(stu2);
// sub1.addStudent(stu3);
System.out.println(sub1.getSubjectName()+"专业已经有"+sub1.getStudentNum()+"个学生参与报名");
System.out.println(sub2.getSubjectName()+"专业已经有"+sub2.getStudentNum()+"个学生参与报名");
}
}