//Major.java 中
package com.imooc.student;
public class Major {
private String name;
private String id;
private int year;
public Major(String name,String id,int year) {
setName(name);
setId(id);
setYear(year);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
//Student.java 中
package com.imooc.student;
public class Student {
private String name;
private String id;
private String gender;
private int age;
private Major major;
//new Major(1,2,3)
public Student(String name,String id,String gender,int age,Major major) {
setName(name);
setId(id);
setGender(gender);
setAge(age);
setMajor(major);//这里添加了其他类的内容,不符合单一职责原则,需要进行解耦
}
//有更好的写法
//不是直接打印,而应该是返回字符串,这样复用性比较高,单一原则,只需要提供功能,不需要决定用什么方式输出
public void infoPrint()
{
System.out.println("姓名:"+this.getName());
System.out.println("学号:"+this.getId());
System.out.println("性别:"+this.getGender());
System.out.println("年龄:"+this.getAge());
System.out.println("所报专业名称:"+this.getMajor().getName());
System.out.println("学制年限:"+this.getMajor().getYear());
}
public Major getMajor() {
return major;
}
public void setMajor(Major major) {
this.major = major;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//Test.java中
package com.imooc.student;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Major majorOne = new Major("计算机科学与技术","J0001",4);
Major majorTwo= new Major("计算机科学与技术","J0001",4);
Major majorThree= new Major("计算机科学与技术","J0001",4);
Student one = new Student("张三","S01","男",18,majorOne);
Student two = new Student("李四","S02","女",17,majorTwo);
Student three = new Student("王五","S03","女",18,majorThree);
one.infoPrint();
two.infoPrint();
three.infoPrint();
}
}
从上面的程序很明显看出许多问题。
1.单一职责原则
1.Major.java 和 Student.java 应该单独放在一个包内,Test.java应该单独放在另一个包内
2.public void infoPrint() 中不应该直接打印应该直接返回需要打印的字符串,在Test.java中再进行打印。
修改后的源码:
//Major.java
package com.imooc.student;
public class Major {
private String name;
private String id;
private int year;
/**
* 有参构造方法
* @param name
* @param id
* @param year
*/
public Major(String name,String id,int year) {
setName(name);
setId(id);
setYear(year);
}
/**
* 无参构造方法
*/
public Major() {
}
public String infoPrint() {
String str = "专业名称:"+this.getName()+"\n专业编号:"+this.getId()+"\n学制年限:"+this.getYear();
return str;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
//Student.java
package com.imooc.student;
public class Student {
private String name;
private String id;
private String gender;
private int age;
private Major major;
/**
* 有参构造函数
* @param name
* @param id
* @param gender
* @param age
*/
public Student(String name,String id,String gender,int age) {
setName(name);
setId(id);
setGender(gender);
setAge(age);
}
/**
* 有参构造函数
* @param name
* @param id
* @param gender
* @param age
*/
public Student(String name,String id,String gender,int age ,Major major) {
setName(name);
setId(id);
setGender(gender);
setAge(age);
setMajor(major);
}
/**
* 无参构造函数,一般加上
*/
public Student() {
}
/**
* Student信息与Major信息关联的方案一
* @param subjectName 学科名
* @param subjectYear 学科年限
* @return
*/
public String infoPrint(String subjectName, int subjectYear) {
String str = "姓名:" + this.getName() + "\n学号:" + this.getId() + "\n性别:" + this.getGender() + "\n年龄:"
+ this.getAge() + "\n学科名称:" + subjectName + "\n学制年限:" + subjectYear;
return str;
}
/**
* Student信息与Major信息关联的方案二
* @param mySubject 专业类对象
* @return
*/
public String infoPrint(Major mySubject) {
String str = "姓名:" + this.getName() + "\n学号:" + this.getId() + "\n性别:" + this.getGender() + "\n年龄:"
+ this.getAge() + "\n学科名称:" + mySubject.getName() + "\n学制年限:" + mySubject.getYear();
return str;
}
/**
* Student信息与Major信息关联的方案三 将Major对象作为自己的属性
* @return
*/
public String infoPrint() {
String str = "姓名:" + this.getName() + "\n学号:" + this.getId() + "\n性别:" + this.getGender() + "\n年龄:"
+ this.getAge() + "\n学科名称:" + this.major.getName() + "\n学制年限:" + this.major.getYear();
return str;
}
public Major getMajor() {
return major;
}
public void setMajor(Major major) {
this.major = major;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//Test.java
package com.imooc.student;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Major majorOne = new Major("计算机科学与技术","J0001",4);
System.out.println("============================");
//方案一
Student one = new Student("张三","S01","男",18);
System.out.println(one.infoPrint(majorOne.getName(), majorOne.getYear()));
System.out.println("============================");
//方案二
Student two = new Student("李四","S02","女",17);
System.out.println(two.infoPrint(majorOne));
System.out.println("============================");
//方案三
Student three = new Student("王五","S03","女",18,majorOne);
System.out.println(three.infoPrint(majorOne));;
System.out.println("============================");
}
}