流程
专业类:
- 先创建Subject(专业类):
- 添加专业类的属性:
- 创建成员属性的get与set方法:
- 给属性添加不合理判定:
- 构建无参构造器和多餐构造器:
- 添加专业信息的描述方法:
- 编写调用方法添加属性测试:
学生类:
- 创建Student(学生)类:
- 添加学生成员属性:
- 创建成员属性的get和set方法(不合理性判定方法):
- 创建无参构造器和多餐构造器:
- 描述学生信息方法:
- 编写调用方法添加属性进行测试:
构建方法将学生类和专业类关联起来
1.重载info方法,添加专业信息,并调用测试
2. 重载info方法,将专业对象作为参数,并调用测试:
完整代码如下:
package com.dodoke.stu_manager.bean;
/**
* 专业类
* 用于描述高校专业信息
* @author user
*
*/
public class Subject {
//成员属性:专业名称;专业编号;专业年限
private String subjectName;
private String subjectNo;
private int 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;//直接退出该方法,不在执行,是subjectLife属性白痴原有的默认
}
this.subjectLife = subjectLife;
}
public Subject() {
}
public Subject(String subjectName, String subjectNo, int subjectLife) {
super();
this.setSubjectLife(subjectLife);
this.setSubjectName(subjectName);
this.setSubjectNo(subjectNo);
}
/**
* 该方法用于描述专业信息
* @return 返回描述专业相关信息的字符串
* 在正式项目中,对于信息的输出不会利用System.out.println(),java程序一般会提供信息出来,具体的信息输出方式由使用者决定
*/
public String info(){
//字符串的拼接,尽量不要写成一长串的形式,会让人看得难受
String str = "专业信息如下:\n专业名称:" + this.getSubjectName() + "\n";
str += "专业编号:" + this.getSubjectNo() + "\n";
str += "学制年限:" + this.getSubjectLife() + "年";
return str;
}
}
package com.dodoke.stu_manager.bean;
public class Student {
//成员属性:学号;姓名;性别;年龄
private String stuCode;
private String stuName;
private String sex;
private int age;
public String getStuCode() {
return stuCode;
}
public void setStuCode(String stuCode) {
this.stuCode = stuCode;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
//String类的equal()方法,用于判断两个String对象值是否相同,相同返回true,不相同则返回false
if(sex.equals("男") || sex.equals("女")){
this.sex = sex;
} else {
this.sex = sex;
}
}
public int getAge() {
return age;
}
/**
*
* @param age年龄限制在10-100之间,除此之外设置为19岁
*/
public void setAge(int age) {
if(age > 100 || age < 10){
this.age = 19;
return;
}
this.age = age;
}
public Student(String stuCode, String stuName, String sex, int age) {
super();
this.stuCode = stuCode;
this.stuName = stuName;
this.sex = sex;
this.age = age;
}
public Student(){
}
/**
* 学生自我介绍
* @return 用于返回描述学生信息字符串
*/
public String info() {
String str = "学生信息如下:\n姓名:" + this.getStuName() + "\n";
str += "学号:" + this.getStuCode()+ "\n";
str += "性别:" + this.getSex()+ "\n";
str += "年龄:" + this.getAge();
return str;
}
/**
* 运用重载的方法将两个类进行关联
* 学生自我介绍+所以学专业介绍
* @param subName
* @param subLife
* @return
*/
public String info(String subName, int subLife) {
String str = "学生信息如下:\n姓名:" + this.getStuName() + "\n";
str += "学号:" + this.getStuCode()+ "\n";
str += "性别:" + this.getSex()+ "\n";
str += "年龄:" + this.getAge();
str += "\n所报专业名称:" + subName;
str += "\n学制年限:" + subLife;
return str;
}
/**
* 学生自我介绍+所以学专业介绍
* 将自己创建的类作为数据类型并要求传入该类型的参数
* @param sub
* @return
*/
public String info(Subject sub) {
String str = "学生信息如下:\n姓名:" + this.getStuName() + "\n";
str += "学号:" + this.getStuCode()+ "\n";
str += "性别:" + this.getSex()+ "\n";
str += "年龄:" + this.getAge();
str += "\n所报专业名称:" + sub.getSubjectName();
str += "\n学制年限:" + sub.getSubjectLife();
return str;
}
}
package com.dodoke.stu_manager.test;
import com.dodoke.stu_manager.bean.Student;
import com.dodoke.stu_manager.bean.Subject;
public class Test {
public static void main(String[] args){
Subject sub = new Subject("计算机科学与应用","J0001",4);
System.out.println(sub.info());
System.out.println("=============================");
Student stu0 = new Student("S01","张三","男", 18);
System.out.println(stu0.info());
//将学生类和专业类关联起来
//方案1:直接传入所需参数
System.out.println("=============================");
Student stu1 = new Student("S02","李四","女", 17);
System.out.println(stu1.info(sub.getSubjectName(),sub.getSubjectLife()));
//方案2:将包含所需信息的对象作为参数整体传入
System.out.println("=============================");
Student stu2 = new Student("S03","王五","男", 18);
System.out.println(stu2.info(sub));
}
}