1 题目
编写程序实现如下功能:已知Person类包含三个公共域变量(姓名、性别、年龄)和一个构造方法,Student类是Person类的派生类,包含两个新的公共域变量(学号、班号)、两个公共方法(修改年龄、显示基本信息)及一个构造方法。定义一组学生对象,并初始化他们的基本信息,然后依次输出。
2 源代码
Preson.java
/*实现person类*/
public class Person
{
public String name;
public char sex;
public int age;
Person(String name,char sex,int age)
{
this.name = name;
this.sex = sex;
this.age = age;
}
}
Student.java
/*实现Student类*/
public class Student extends Person
{
public String classno;
public String sno;
//构造函数
Student(String sno,String classno,String name,char sex, int age)
{
super(name,sex,age);
this.classno = classno;
this.sno = sno;
}
//更新学生的年龄信息
void updateAge(int age)
{
this.age = age;
}
//输出学生信息
void print()
{
System.out.println("学生信息:" + '\n');
System.out.println(" " + "学号" + " " + "班号" + " " + "姓名" + " " + "性别" + " " + "年龄" + '\n');
System.out.println(this.sno + " " + this.classno + " " + this.name + " " + this.sex + " " + this.age + '\n');
}
}
TestStudentPerson.java
public class TestStudentPerson
{
public static void main(String[] args)
{
char i;
Student student[] = new Student[6];//定义一个数组
//对数组里面的元素进行初始化
student[0] = new Student("201704166051","电信1702班","Boly",'Y',18);
student[1] = new Student("201704166052","电信1702班","Lily",'X',18);
student[2] = new Student("201704166053","电信1702班","Bob",'X',19);
student[3] = new Student("201704166054","电信1702班","Tom",'Y',19);
student[4] = new Student("201704166055","电信1702班","Jack",'Y',18);
student[5] = new Student("201704166056","电信1702班","Roly",'Y',19);
//显示各学生信息
for(i = 0;i < 6;i++)
{
student[i].print();
}
}
}