📄 studenttest.java
字号:
/**
*学生测试类
*/
public class StudentTest
{
public static void main(String[] args)
{
//生成一个新的ClassManage类对象,职务是班长
ClassManage monitor = new ClassManage("Tom", "20031009", "班长");
Student[] student = new Student[3];//学生类数组,容量为3
//数组初始化
student[0] = monitor;
student[1] = new Student("Jack", "2003100901");
student[2] = new Student("Rose", "2002091002");
for(int i = 0; i <student.length; i++)
{
System.out.println(student[i].toString());
}
}
}
/**
*学生类,包括学生的基本信息
*/
class Student
{
private String strName = "";//学生姓名
private String strNumber = "";//学号
private String strSex = "";//性别
private String strBirthday = "";//出生年月
private String strSpeciality = "";//专业
private String strAddress = "";//地址
public Student(String name, String number)
{
strName = name;
strNumber = number;
}
public String getStudentName()
{
return strName;
}
public String getStudentNumber()
{
return strNumber;
}
public void setStudentSex(String sex)
{
strSex = sex;
}
public String getStudentSex()
{
return strSex;
}
public String getStudentBirthday()
{
return strBirthday;
}
public void setStudentBirthday(String birthday)
{
strBirthday = birthday;
}
public String getStudentSpeciality()
{
return strSpeciality;
}
public void setStudentSpeciality(String speciality)
{
strSpeciality = speciality;
}
public String getStudentAddress()
{
return strAddress;
}
public void setStudentAddress(String address)
{
strAddress = address;
}
public String toString()
{
String information = "学生姓名=" + strName + ", 学号=" + strNumber;
if( !strSex.equals("") )
information += ", 性别=" + strSex;
if( !strBirthday.equals(""))
information += ", 出生年月=" + strBirthday;
if( !strSpeciality.equals("") )
information += ", 专业=" + strSpeciality;
if( !strAddress.equals("") )
information += ", 籍贯=" + strAddress;
return information;
}
}
/**
*学生管理者类,增加了职务字段
*/
class ClassManage extends Student
{
private String strDuty = "";
public ClassManage(String name, String number, String strDuty)
{
super(name, number);//调用父类的构造器,初始化相关字段
this.strDuty = strDuty;
}
public String getDuty()
{
return strDuty;
}
//覆盖父类的方法
public String toString()
{
String str = super.toString();//调用父类中的方法
if(!strDuty.equals(""))
{
str += ", 职务=" + strDuty;
}
return str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -