三个学生,分别学了不同专业的场景。
类:
专业类:专业名称、编号、学制年限。
学生类:姓名、学号、性别、年龄
目录结构:
Subject.java
package com.immoc.model;
/**
* @description 学科类
* @author dongwenjie
*
*/
public class Subject {
// 成员属性:学科名称、学科编号、学制年限
private String subjectName;
private String subjectNo;
private int subjectLife;
// 无参构造
public Subject() {
}
// 有参构造,实现对属性的全部赋值
public Subject(String subjectName, String subjectNo, int subjectLife) {
this.setSubjectName(subjectName);
this.setSubjectNo(subjectNo);
this.setSubjectLife(subjectLife);
}
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 info() {
String str = "";
str+="学科名称:"+this.getSubjectName()+"\n学科编号:"+this.getSubjectNo()+"\n学制年限:"+this.getSubjectLife()+"年";
return str;
}
}
SchoolTest.java
package com.immoc.test;
import com.immoc.model.Student;
import com.immoc.model.Subject;
public class SchoolTest {
public static void main(String[] args) {
Subject kemu = new Subject("语文", "No12345", 18);
System.out.println(kemu.info());
/*
* 学科名称:语文 学科编号:No12345 学制年限:18年
*/
System.out.println("==============");
Student dongwenjie = new Student("01", "董文杰", "男", 18);
Student liqingfei = new Student("02", "李秦飞", "女", -30);
Student zhaoxiaogang = new Student("03", "赵小刚", "男", 26);
System.out.println(dongwenjie.introduction());
System.out.println(liqingfei.introduction());
System.out.println(zhaoxiaogang.introduction());
}
}
Student.java
package com.immoc.model;
public class Student {
// 成员属性:学号、姓名、性别、年龄
private String studentNo;
private String studentName;
private String studentSex;
private int studentAge;
// 无参构造
public Student() {
super();
}
// 多参构造方法,实现对全部属性的赋值
public Student(String studentNo, String studentName, String studentSex, int studentAge) {
this.setStudentNo(studentNo);
this.setStudentName(studentName);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
}
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) {
if ("男".equals(studentSex) || "女".equals(studentSex)) {
this.studentSex = studentSex;
}else {
this.studentSex = "男";
}
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
if(studentAge<10 || studentAge>100) {
this.studentAge = 18;
}else {
this.studentAge = studentAge;
}
}
/**
* 学生介绍的方法
* @return
*/
public String introduction() {
String str = "";
str += "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:" + this.getStudentSex()
+ "\n年龄:" + this.getStudentAge();
return str;
}
}
学生类,学科类,测试类三个都有了,想在学生当中添加所学专业信息,做法有如下几种:
①重写学生introduction方法,加上学科和年限两个参数 (容易理解,参数列表长)
/**
* 学生介绍的方法
* @return
*/
public String introduction(String subjectName, int subjectLift) {
String str = "";
str += "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:" + this.getStudentSex()
+ "\n年龄:" + this.getStudentAge()+"\n学科:"+subjectName+"\n学科年限:"+subjectLift;
return str;
}
调用如下:
System.out.println(dongwenjie.introduction("语文", 18));
②将Subject当做参数传入introduction方法中。(相比①更加简单,获取参数方便)
/**
* 学生介绍的方法
* @return
*/
public String introduction(Subject SubjectInfo) {
String str = "";
str += "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:" + this.getStudentSex()
+ "\n年龄:" + this.getStudentAge()+"\n学科:"+SubjectInfo.getSubjectName()+"\n学科年限:"+SubjectInfo.getSubjectLife();
return str;
}
package com.immoc.test;
import com.immoc.model.Student;
import com.immoc.model.Subject;
public class SchoolTest {
public static void main(String[] args) {
Subject kemu = new Subject("语文", "No12345", 18);
Student zhaoxiaogang = new Student("03", "赵小刚", "男", 26);
System.out.println(zhaoxiaogang.introduction(kemu));
}
}
第二种可以传入对象,一次性可以拿到对象的所有信息。是一种比较好的方案。
③每个学生既然都有科目信息,那可以将Subject类当做学生的一个成员属性,这种方式也是比较好的。(科目类被当做是学生类的一个属性,关联性更强。)