正在回答 回答被采纳积分+1
package com.object.project;
public class StudentTwo {
// 成员属性:学生姓名、学生年龄、学生性别、学号
private String studentName;
private int studentAge;
private String studentSex;
private String studentNo;
// 无参构造方法
public StudentTwo() {
}
// 带参构造方法
public StudentTwo(String studentName, int studentAge, String studentSex, String studentNo) {
this.setStudentName(studentName);
this.setStudentAge(studentAge);
this.setStudentSex(studentSex);
this.setStudentNo(studentNo);
}
// get/set方法
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
if (studentAge <= 10||studentAge>=100)
return;
this.studentAge = studentAge;
}
public String getStudentSex() {
return studentSex;
}
public void setStudentSex(String studentSex) {
if (studentSex.equals("男")||studentSex.equals("女")) {
this.studentSex = studentSex;
}else {
this.studentSex="男";
}
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String introduction() {
String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n年龄:"
+ this.getStudentAge() + "岁"+"\n性别:" + this.getStudentSex();
return str;
}
}
package com.object.project;
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);
this.setMyStudents(myStudents);
this.setStudentNum(studentNum);
}
// 带参构造方法,实现对全部属性的赋值
public Subject(String subjectName, String subjectNo, int subjectLife,Student[] myStudents) {
this.setSubjectName(subjectName);
this.setSubjectNo(subjectNo);
this.setSubjectLife(subjectLife);
this.setMyStudents(myStudents);
this.setStudentNum(studentNum);
}
// set/get方法
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;
}
public void setSubjectLife(int subjectLife) {
if (subjectLife <= 0) {
return;
}
this.subjectLife = subjectLife;
}
// 防止获取的数组未被初始化,对其进行限制
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(StudentTwo stu) {
/*1、把学生信息保存到数组中
* 2、将学生个数保存到studentNum
*/
//1、把学生信息保存到数组中
for(int i=0;i<this.getMyStudents().length;i++) {
if(this.getMyStudents()[i]==null) {
this.getMyStudents()[i]=stu;
//2、将学生个数保存到studentNum
this.studentNum=i+1;
return;
}
}
}
}
package com.test;
import com.object.project.StudentTwo;
import com.object.project.Subject;
public class SchoolTest {
public static void main(String[] args) {
//实例化对象one
Subject one = new Subject("人力资源管理","J0123",-6);
if(one.getSubjectLife()==0)
one.setSubjectLife(4);
System.out.println(one.info());
System.out.println("========================");
//实例化对象各个学生
StudentTwo stu1=new StudentTwo("张三",99,"男","123");
if(stu1.getStudentAge()==0)
stu1.setStudentAge(18);
System.out.println(stu1.introduction());
System.out.println("========================");
StudentTwo stu2=new StudentTwo("李四",18,"女","123");
System.out.println(stu2.introduction());
System.out.println("========================");
StudentTwo stu3=new StudentTwo("王五",19,"男","123");
System.out.println(stu3.introduction());
System.out.println("========================");
//测试指定专业中的学生报名人数
one.addStudent(stu1);
one.addStudent(stu2);
one.addStudent(stu3);
}
}
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星