info
Posted 振华213
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了info相关的知识,希望对你有一定的参考价值。
package com.imooc.info; public class Student { // 学生姓名如下: // 姓名 // 学号 // 性别 // 年龄 // 新增专业属性 private String StuName; private String StuNo; private String StuSex; private int StuAge; private Subject StuSubject; // 无参构造方法 public Student() { } // 多参构造方法,实现对全部属性的赋值 public Student(String stuName, String stuNo, String stuSex, int stuAge, Subject stuSubject) { this.setStuName(stuName); this.setStuNo(stuNo); this.setStuSex(stuSex); this.setStuAge(stuAge); this.setStuSubject(stuSubject); } // 多参构造方法,实现对姓名、学号、性别、年龄----四参 public Student(String stuName, String stuNo, String stuSex, int stuAge) { this.setStuName(stuName); this.setStuNo(stuNo); this.setStuSex(stuSex); this.setStuAge(stuAge); } public String getStuName() { return StuName; } public void setStuName(String stuName) { StuName = stuName; } public String getStuNo() { return StuNo; } public void setStuNo(String stuNo) { StuNo = stuNo; } public String getStuSex() { return StuSex; } // 限制性别只能为"男"或者"女",否则为"未知" public void setStuSex(String stuSex) { if (stuSex == "男" || stuSex == "女") { StuSex = stuSex; } else StuSex = "未知"; } public int getStuAge() { return StuAge; } /** * 给年龄赋值,限定必须在10-100之间,反之赋值为18 * * @param stuAge */ public void setStuAge(int stuAge) { if (stuAge < 10 || stuAge > 100) { this.StuAge = 18; } else StuAge = stuAge; } /** * 获取专业对象,如果没有实例化,先实例化后再返回 * * @return 专业对象信息 */ public Subject getStuSubject() { if (this.StuSubject == null) { this.StuSubject = new Subject(); } return StuSubject; } public void setStuSubject(Subject stuSubject) { StuSubject = stuSubject; } /** * 学生自我介绍的方法 * * @param SubName * 所报专业名称 * @param SubYear * 学制年限 * @return自我介绍的信息,包括姓名、学号、性别、年龄 */ public String Stu() { String messageStudent = "==========================\\n学生信息如下:\\n姓名:" + this.getStuName() + "\\n学号:" + this.getStuNo() + "\\n性别:" + this.getStuSex() + "\\n年龄:" + this.getStuAge(); return messageStudent; } //方法一 public String Stu(String SubName, int SubYear) { String messageStudent = "==========================\\n学生信息如下:\\n姓名:" + this.getStuName() + "\\n学号:" + this.getStuNo() + "\\n性别:" + this.getStuSex() + "\\n年龄:" + this.getStuAge() + "\\n所报专业名称:" + SubName + "\\n学制年限:" + SubYear; return messageStudent; } //方法二 /** * 学生自我介绍的方法 * * @param mySubject * 所选专业的对象 * @return自我介绍的信息,包括姓名、学号、性别、年龄,以及通过对象传入的所报专业名称、学制年限 */ public String Stu(Subject mySubject) { String messageStudent = "==========================\\n学生信息如下:\\n姓名:" + this.getStuName() + "\\n学号:" + this.getStuNo() + "\\n性别:" + this.getStuSex() + "\\n年龄:" + this.getStuAge() + "\\n所报专业名称:" + mySubject.getSubName() + "\\n学制年限:" + mySubject.getSubYear(); return messageStudent; } //方法三 public String Stu2() { String messageStudent = "==========================\\n学生信息如下:\\n姓名:" + this.getStuName() + "\\n学号:" + this.getStuNo() + "\\n性别:" + this.getStuSex() + "\\n年龄:" + this.getStuAge() + "\\n所报专业名称:" + this.getStuSubject().getSubName() + "\\n专业年限:" + this.getStuSubject().getSubYear(); return messageStudent; } }
package com.imooc.info;
public class Subject {
// 成员属性:学科名称、学科编号、学制年限、报名选修的学生信息、报名选修的学生个数
private String SubName;
private String SubNo;
private int SubYear;
private Student[] myStudents;
private int studentNum;
// 无参构造方法
public Subject() {
}
// 多参构造方法,实现对学科名称、编号、学制年限的赋值
public Subject(String subName, String subNo, int subYear) {
this.setSubName(subName);
this.setSubNo(subNo);
this.setSubYear(subYear);
}
// 多参构造方法,实现对全部属性的赋值
public Subject(String subName, String subNo, int subYear,
Student[] myStudents) {
this.setSubName(subName);
this.setSubNo(subNo);
this.setSubYear(subYear);
this.setMyStudents(myStudents);
}
public String getSubName() {
return SubName;
}
public void setSubName(String subName) {
SubName = subName;
}
public String getSubNo() {
return SubNo;
}
public void setSubNo(String subNo) {
SubNo = subNo;
}
public int getSubYear() {
return SubYear;
}
/**
* 获取选修专业的学生信息,如果保存学生信息的数组未被初始化,则,先初始化长度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 void setSubYear(int subYear) {
SubYear = subYear;
}
public int getStudentNum() {
return studentNum;
}
public void setStudentNum(int studentNum) {
this.studentNum = studentNum;
}
public String Sub() {
String messageSubject = "专业信息如下:\\n专业名称:" + this.getSubName()
+ "\\n专业编号:" + this.getSubNo() + "\\n学制年限:" + this.getSubYear()
+ "年";
return messageSubject;
}
public void addStudent(Student stu) {
/*
* 1、将学生保存到数组中 2、将学生个数保存到studentNum
*/
// 1、将学生保存到数组中
int i;
for (i = 0; i < this.getMyStudents().length; i++) {
if (this.getMyStudents()[i] == null) {
this.getMyStudents()[i] = stu;
break;
}
}
// 2、将学生个数保存到studentNum
this.studentNum = i + 1;
}
}
package com.imooc.info;
import com.imooc.info.Student;
import com.imooc.info.Subject;
public class Test {
public static void main(String[] args) {
Subject sub1=new Subject("计算机科学与应用","J0001",4);
System.out.println(sub1.Sub());
Student stu1=new Student("张三","S01","男",18,sub1);
System.out.println(stu1.Stu2());
Student stu2=new Student("李四","S02","男",17);
System.out.println(stu2.Stu("计算机科学与应用", 4));
Student stu3=new Student("王五","S03","男",18);
System.out.println(stu3.Stu(sub1));
//测试制定专业中到底有多少学生报名
//sub1.addStudent(stu1);
//sub1.addStudent(stu2);
//sub1.addStudent(stu3);
System.out.println(sub1.getSubName()+"的专业共招生"+sub1.getStudentNum()+"人");
}
}
以上是关于info的主要内容,如果未能解决你的问题,请参考以下文章