package java_model;
public class Student {
//成员属性:学号、姓名、性别、年龄
private String studentNo;
private String studentName;
private String studentSex;
private int studentAge;
//无参构造方法
public Student(){
}
//带参构造方法,实现对全部属性的赋值
public Student(String studentNo, String studentName, String studentSex, int studentAge){
this.setstudentNo(studentNo);
this.setstudentName(studentName);
this.setstudentSex(studentSex);
this.setstudentAge(studentAge);
}
public void setstudentNo(String studentNo){
this.studentNo = studentNo;
}
public String getstudentNo(){
return studentNo;
}
public void setstudentName(String studentName){
this.studentName = studentName;
}
public String getstudentName(){
return studentName;
}
public void setstudentSex(String studentSex){
//限制性别只能是“男”或者“女”,反之,强制赋值为“男”
// if(studentSex == "男" || studentSex == "女")
// this.studentSex = studentSex;
// else
// this.studentSex = "男";
if(studentSex.equals("男") || studentSex.equals("女"))
this.studentSex = studentSex;
else
this.studentSex = "男";
}
public String getstudentSex(){
return studentSex;
}
/**
* 给年龄赋值,限定必须在10-100之间,反之赋值为18
* @param studentAge 传入年龄
*/
public void setstudentAge(int studentAge){
if(studentAge<10 || studentAge>100)
this.studentAge = 18;
else
this.studentAge = studentAge;
}
public int getstudentAge(){
return studentAge;
}
/**
* 学生自我介绍的方法
* @return 自我介绍的信息,包括姓名、学号、性别、年龄
*/
public String introduction(){
String str = "学生信息如下:\n姓名:"+this.getstudentName()+
"\n学号:"+this.getstudentNo()+"\n性别:"+
this.getstudentSex()+"\n年龄:"+this.getstudentAge();
return str;
}
}
package java_test;
import java_model.Student;
import java_model.Subject;
public class SchoolTest {
public static void main(String[] args) {
//测试Subject
Subject sub1 = new Subject("计算机科学与应用", "J0001", 4);
System.out.println(sub1.info());
System.out.println("===================================");
//测试Student
Student stu1 = new Student("S01", "张三", "男", 17);
System.out.println(stu1.introduction());
}
}
输出:
专业信息如下:
专业名称:计算机科学与应用
专业编号:J0001
学制年限:4年
===================================
学生信息如下:
姓名:张三
学号:S01
性别:男
年龄:17