编写出一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)等域。然后对Person 类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩。最后在Main函数中对student类的功能进行验证。
public class Demo02 {
@Test
public void test() {
Student s_1=new Student("小明",10,"男");
s_1.chengjiluru();
s_1.pingjun();
}
}
class Person{
private String name;
private int age;
private String sex;
public Person() {
}
public Person(String name,int age,String sex) {
this.name=name;
this.age=age;
this.sex=sex;
}
}
class Student extends Person{
private double[] score=new double[5];
public Student(String name,int age,String sex) {
super(name,age,sex);
}
public void chengjiluru() {
Scanner scan=new Scanner(System.in);
System.out.println("请分别输入(成绩):语文、英语、数学、政治、化学");
for(int i=0;i<5;i++) {
score[i]=scan.nextDouble();
}
}
public void pingjun() {
double sum=0;
for(int i=0;i<5;i++) {
sum+=score[i];
}
double jun=sum/5;
System.out.println("平局成绩为:"+jun);
}
}