java封装实例
扫描二维码
随时随地手机看文章
在类的getter/setter方法中在getter方法中要先实列话
在构造方法中无参构造(必须要构造)好处很多!!!
描述学生信息及相关联的专业信息
把Subject和Stydent的属性进行连接有3中方法
Student中的代码
Subject类中的代码
test类中的代码及输出
专业信息如下:
专业名称 计算机科学
专业编号 J0001
学制年限 4
==============================
学生信息如下:
我的名字是 张三
我的学号为 S01
性别为 性别错误
年龄为 18
所在学科为 计算机科学
学科的年制 4
==============================
学生信息如下:
我的名字是 李四
我的学号为 S02
性别为 女
年龄为 20
所在学科为 计算机科学学院
学科的年制 4
==============================
学生信息如下:
我的名字是 王五
我的学号为 S03
性别为 男
年龄为 21
所在学科为 计算机科学
学科的年制 4
新增需求:计算机专业有多少人
整代码如下
subject类
package com.jiedada.model;
/**
* 专业类
* @author 杰大大
*
*/
public class Subject {
//学科的名称,学科编号,学制年限
private String subjectName;
private String subjectNum;
private int subjectLife;
private Student student[];
private int studentNum;
//带参构造实现对属性的全部赋值
public Subject(String subjectName,String subjectNum,int subjectLife)
{
this.setSubjectName(subjectName);
this.setSubjectNum(subjectNum);
this.setSubjectLife(subjectLife);
}
public Subject(String subjectName,String subjectNum,int subjectLife,Student student[],int studentNum)
{
this.setSubjectName(subjectName);
this.setSubjectNum(subjectNum);
this.setSubjectLife(subjectLife);
this.setStudent(student);
this.setStudentNum(studentNum);
}
//无参构造
public Subject()
{
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public String getSubjectNum() {
return subjectNum;
}
public void setSubjectNum(String subjectNum) {
this.subjectNum = subjectNum;
}
public int getSubjectLife() {
return subjectLife;
}
public void setSubjectLife(int subjectLife) {
if(subjectLife<=0)
{
return;
}
else
this.subjectLife = subjectLife;
}
//专业介绍的方法名称,编号,年限
/*public void info()
{
//String str="专业信息如下\n专业名称 "+this.getSubjectName()+"\n专业编号 "+this.getSubjectNum()+"\n学制年限 "+this.getSubjectLife();
//return str;
System.out.println("专业名称 "+this.getSubjectName());
System.out.println("专业编号 "+this.getSubjectNum());
System.out.println("专业年限 "+this.getSubjectLife());
}*/
/**
* 增加学生,判断student是不是实例化了
* @return 学生的
*/
public Student[] getStudent() {
if(student==null)
{
this.student=new Student[200];
}
return student;
}
public void setStudent(Student[] student) {
this.student = student;
}
public int getStudentNum() {
return studentNum;
}
public void setStudentNum(int studentNum) {
this.studentNum = studentNum;
}
public String info() {
String str="专业信息如下:\n专业名称 "+this.getSubjectName()+"\n专业编号 "+this.getSubjectNum()+"\n学制年限 "+this.getSubjectLife();
return str;
}
public void addNum(Student student)
{
int i;
for(i = 0;i
{
if(this.getStudent()[i]==null)
{
this.getStudent()[i]=student;
break;
}
}
this.setStudentNum(i+1);
}
}
student类的完整代码
package com.jiedada.model;
public class Student {
//姓名,年龄,性别,学号
private String studentNo;
private String studentName;
private String studentSex;
private int studentAge;
private Subject studentSubject;
//无参构造
public Student() {
}
/*public Student(String studentNo, String studentName, String studentSex, int studentAge) {
this.studentNo = studentNo;
this.studentName = studentName;
this.studentSex = studentSex;
this.studentAge = studentAge;
}*/
//有参构造
public Student(String studentNo,String studentName,String studentSex,int studentAge)
{
this.setStudentNo(studentNo);
this.setStudentName(studentName);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
}
//有参构造而且返回全部属性
public Student(String studentNo,String studentName,String studentSex,int studentAge,Subject studentSubject)
{
this.setStudentNo(studentNo);
this.setStudentName(studentName);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
this.setStudentSubject(studentSubject);
}
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(studentSex=="男"|studentSex=="女")
this.studentSex = studentSex;
else
this.studentSex="性别错误";
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
if(studentAge>=10&studentAge<=100)
this.studentAge = studentAge;
else
this.studentAge=18;
}
//在创建类的Get是要把类实例化
public Subject getStudentSubject() {
if(studentSubject==null)
{
this.studentSubject=new Subject();
}
return studentSubject;
}
public void setStudentSubject(Subject studentSubject) {
this.studentSubject = studentSubject;
}
/**
* 这是学生自我介绍得等等
* @return 姓名,学号,姓名,年龄,所在专业,专业年龄
*/
public String introduction()
{
String str="学生信息如下:\n我的名字是 "+this.getStudentName()+"\n我的学号为 "
+this.getStudentNo()+"\n性别为 "+this.getStudentSex()+"\n年龄为 "+this.studentAge
+"\n所在学科为 "+this.getStudentSubject().getSubjectName()+"\n学科的年制 "+this.getStudentSubject().getSubjectLife();
return str;
}
/**
* 学生的自我介绍的方法
* @param subjectName
* @param subjectLife
* @return 姓名,学号,姓名,年龄,所在专业,专业年龄
*/
public String introduction(String subjectName,int subjectLife)
{
String str="学生信息如下:\n我的名字是 "+this.getStudentName()+"\n我的学号为 "
+this.getStudentNo()+"\n性别为 "+this.getStudentSex()+"\n年龄为 "+this.studentAge
+"\n所在学科为 "+subjectName+"\n学科的年制 "+subjectLife;
return str;
}
/**
* 介绍自己
* @param subject
* @return 姓名,学号,姓名,年龄,所在专业,专业年龄
*/
public String introduction(Subject subject)
{
String str="学生信息如下:\n我的名字是 "+this.getStudentName()+"\n我的学号为 "
+this.getStudentNo()+"\n性别为 "+this.getStudentSex()+"\n年龄为 "+this.studentAge
+"\n所在学科为 "+subject.getSubjectName()+"\n学科的年制 "+subject.getSubjectLife();
return str;
}
}
test类的完整代码
package com.jiedada.test;
import com.jiedada.model.Student;
import com.jiedada.model.Subject;
public class SchoolTest {
public static void main(String[] args) {
// 调试Subject
Subject sub = new Subject("计算机科学", "J0001", 4);
System.out.println(sub.info());
System.out.println("==============================");
Student stu1=new Student("S01","张三","无",5,sub);
System.out.println(stu1.introduction());
System.out.println("==============================");
Student stu2=new Student("S02","李四","女",20);
System.out.println(stu2.introduction("计算机科学学院", 4));
System.out.println("==============================");
Student stu3=new Student("S03","王五","男",21);
System.out.println(stu3.introduction(sub));
sub.addNum(stu1);
sub.addNum(stu2);
sub.addNum(stu3);
System.out.println("选择 "+sub.getSubjectName()+"专业的人数为 "+sub.getStudentNum());
}
}