三.模拟学生成绩管理系统

1.创建抽象Person类:

属性:

        姓名;

        年龄

方法:

        构造方法 get() and set()

        抽象方法:显示信息

2.创建学生类Student,继承Person类:

        属性:

                学号;

                入学日期

        方法:

                构造方法:学号使用static关键字来添加。

                get() and set()

                显示信息

3.创建教师类Teacher,继承Person类:

        属性:

                工号

        方法:

                构造方法

                get() and set()

                显示信息

4.创建测试类Test:

        多态的使用:

                创建两个Person对象,一个引用Teacher对象,另 一个引用Student对象,调用她们的显示信息方法。

                创建两个Student对象,存放在同一数组中,然后输出数组元素 的每一个显示信息方法。

四.继续完善学生成绩管理系统

1.创建Score类,用来存放课程信息:

        属性:

                课程号;

                课程名;

                分数(跟学生有关,构造时为默认值)

        方法:

                构造方法

                get() and set()

                显示信息

2.创建GetScore接口,用来给学生上成绩:

        方法:

                setStudentScore,传入学生和分数两个

3.修改学生Student类:

        属性:

                添加课程数组,

                数据元素为2即可。

        方法:

                打印学生成绩方法:

                显示学生姓名和学生的所有课程成绩

                添加设置值的方法:setStudentResult(int i,double result) 给当前第i个课程赋分数。

                给课程数组添加get()和set()方法

4.修改教师Teacher类,实现GetScore接口:

        属性:

                添加课程号属性

        方法:

                显示信息:

                添加显示课程号

                给课程号添加get()和set()方法

                实现setStudentScore方法:给传递的学生的课程上成绩

5.修改测试类Test:

        创建两个学生

        创建两个教师,分别给老师设置不同的课程。

        创建两门课程,根据老师课程设置课程,然后传给学生。

        老师给学生上成绩

        显示学生成绩 

父类:Person

public abstract class Person {
    //属性
    private String name;//姓名
    private int age;//年龄

    //方法
    //显示信息
    public abstract void printMessage();

    //空构造
    public Person() {
    }
    //满构造
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //get() set()
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 子类:Student

import javax.print.attribute.standard.NumberOfDocuments;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Student extends Person {
    //Student类继承Person
    //属性
    private int sid; //学号
    private String date;//日期
    private Score[] scores = new Score[2]; //成绩

    //static 变量
    public static int NumberOfStudent=0;

    //显示信息方法
    public void printMessage(){
        System.out.println("姓名:" +super.getName() +  ",年龄是:" + super.getAge()
                +",学号是 :" + this.sid+ ",入学日期是:" + this.date);
    }
    //显示成绩方法
    public void printStudentScore(){
        System.out.println("姓名:"+super.getName());
        //使用父类的方法需要使用super.+方法获取
        for (int i = 0; i < this.scores.length; i++) {
            this.scores[i].printMessage();
            System.out.println(this.scores[i].getResult());
        }
    }

    //输入学生成绩的方法
    public void setStudentResult(int i,double result){
        this.scores[i].setResult(result);
    }

    //空构造
    public Student(){
        this.sid=++NumberOfStudent; //学号
        Date d=new Date();
        //Date类中的Date()方法可以获取现在的日期及时间
        DateFormat df =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.date=df.format(d);//空构造也需要获取日期
    }
    public Student(String name, int age) {
        super(name, age);
        this.sid = sid;
        Date d=new Date();
        DateFormat df =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置具体的格式MM(月)和mm(分钟)需要区分
        //DateFormat类是一个时间格式的类,SimpleDateFormat是DateFormat的子类(多态)
        //创建一个父类的子类,这样就能使用父类和子类的方法和属性
        this.date=df.format(d);
        //调用format()方法使时间的格式变成合适我们的格式,且类型变成String类型
        this.sid=++NumberOfStudent;
    }

    public int getSid() {
        return sid;
    }

    public String getDate() {
        return date;
    }

    public Score[] getScores() {
        return scores;
    }

    public void setScores(Score[] scores) {
        this.scores = scores;
    }
}

子类:Teacher

public class Teacher extends Person{
    //属性
    private int tid; //工号
    private int scid //课程号

    //显示信息
    @Override
    public void printMessage() {
        System.out.println("姓名:" +super.getName() +  ",年龄是:" + super.getAge()
                +",教师工号是 :" + this.tid+"所授课程为:"+this.scid);
    }
    //空构造
    public Teacher(){
    }

    //满构造
    public Teacher(String name, int age, int tid) {
        super(name, age);
        this.tid = tid;
    }

    //get() 和 set()
    public int getTid() {
        return tid;
    }

    public void setTid(int tid) {
        this.tid = tid;
    }
    public int getScid() {
        return scid;
    }

    public void setScid(int scid) {
        this.scid = scid;
    }
}

 学生成绩类

public class Score {
    //属性
    private int said;  //课程号
    private String  name;//课程名
    private double result=0;//成绩

    //方法
    public void printMessage(){
        System.out.println("课程号:"+ this.said +",名字是:"+this.name);
    }

    //空构造
    public Score(){
    }

    //满构造
    public Score(int said, String name) {
        this.said = said;
        this.name = name;
    }

    //get() 和 set()
    public int getSaid() {
        return said;
    }

    public void setSaid(int said) {
        this.said = said;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getResult() {
        return result;
    }

    public void setResult(double result) {
        this.result = result;
    }
}

接口:设置学生成绩

public interface GetScore {
    public void setStudentScoore(Student student,double result);

}

 测试类1

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
//        Person person1=new Teacher("岳不群",58,1);
//        Person person2=new Student("令狐冲",28);
//        person1.printMessage();
//        person2.printMessage();
//    }
//    public static void printPersonMessage(Person p) {
//        p.printMessage();

//
//        Student student1=new Student("令狐冲",25);
//        Student student2=new Student("岳灵珊",18);
//        Student[] students=new Student[2];
//        students[0]=student1;
//        students[1]=student2;
//        for (int i = 0; i < students.length; i++) {
//            students [i].printMessage();
        //输出student类型数组的对象
//        }


        Student[] students = new Student[2];
        students[0] = new Student("令狐冲", 25);
        students[1] = new Student("岳灵珊", 18);
        for (int i = 0; i < students.length; i++) {
            students[i].printMessage();
        }
    }

}

测试类2

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[2];
        Student students1 = new Student("令狐冲", 25);
        Student students2 = new Student("岳灵珊", 18);

        Teacher teacher1= new Teacher("左冷禅",40,1);
        teacher1.setScid(1);
        Teacher teacher2= new Teacher("岳不群",40,2);
        teacher2.setScid(2);

        Score score1 = new Score(1,"操作系统");
        Score score2 = new Score(2,"编译原理");
        Score[] scores1 = new Score[2];
        scores1[0]=score1;
        scores1[1]=score2;
        students1.setScores(scores1);

        //student2赋值
        Score score3 = new Score(1,"操作系统");
        Score score4 = new Score(2,"编译原理");
        Score[] scores2 = new Score[2];
        scores2[0]=score3;
        scores2[1]=score4;
        students2.setScores(scores2);

        teacher1.setStudentScore(students1, 70);
        teacher1.setStudentScore(students2, 80);

        teacher2.setStudentScore(students1, 89);
        teacher2.setStudentScore(students2, 85);

        students1.printStudentScore();
        students2.printStudentScore();
    }

}