面向对象程序设计练习3

3、在一个学校中有教师和学生两类人员。学生信息有身份证号后四位、学号、专业、姓名、性别、年龄及数学、英语、Java程序设计三门课程成绩。教师信息有身份证号后四位、教师号、专业、姓名、性别、年龄及授课的学时数。创建学生和教师的对象并输出对象的信息。

public class Practice5 {
    public static void main(String[] args) {
 //3、输出老师和学生对象的基本信息
//学生和教师都属于人类
        class  People{
            String name;
            String gender;
            int id;
            int identityCard;
            int age;
            String major;
//构造方法
    public People(String name, String gender, int id, int identityCard, int age, String major) {
                this.name = name;
                this.gender = gender;
                this.id = id;
                this.identityCard = identityCard;
                this.age = age;
                this.major = major;
            }
            public  void getinfo(){
                System.out.println(name+"的年龄为:"+age+",性别是:"+gender+",身份证号为:"+identityCard+",id为:"+id+",专业为:"+major);
            }
        }
//定义教师类,继承父类People
        class Teacher extends People{
           int studyTime;
            public Teacher(String name, String gender, int id, int identityCard, int age, String major) {
                super(name, gender, id, identityCard, age, major);
            }

            public Teacher(String name, String gender, int id, int identityCard, int age, String major, int studyTime) {
                super(name, gender, id, identityCard, age, major);
                this.studyTime = studyTime;
            }

            //方法重写
            public  void getinfo(){
                System.out.println(name+"老师的年龄为:"+age+",性别是:"+gender+",身份证号后四位为:"+identityCard+",教师工号为:"+id+",所教专业为:"+major+",授课时长为:"+studyTime);
            }
        }

//定义学生类,继承父类People
     class Student extends  People{
        double mathscore;
        double englishscore;
        double javascore;
         public Student(String name, String gender, int id, int identityCard, int age, String major) {
             super(name, gender, id, identityCard, age, major);
         }

         public Student(String name, String gender, int id, int identityCard, int age, String major, double mathscore, double englishscore, double javascore) {
             super(name, gender, id, identityCard, age, major);
             this.mathscore = mathscore;
             this.englishscore = englishscore;
             this.javascore = javascore;
         }
//方法重写
         public  void getinfo(){
             System.out.println(name+"学生的年龄为:"+age+",性别是:"+gender+",身份证号后四位为:"+identityCard+",学号为:"+id+",专业为:"+major+
                     ",数学成绩为:"+mathscore+",英语成绩为:"+englishscore+",Java程序设计成绩为:"+javascore);
         }
     }
//定义对象
     People p ;
        p=new Teacher("李华","男",01,3054,38,"数学",650);
        p.getinfo();
        p=new Student("Lili","女",05,2510,18,"计算机",89.5,92,96);
        p.getinfo();

      }
    }

运行结果为:

李华老师的年龄为:38,性别是:男,身份证号后四位为:3054,教师工号为:1,所教专业为:数学,授课时长为:650
Lili学生的年龄为:18,性别是:女,身份证号后四位为:2510,学号为:5,专业为:计算机,数学成绩为:89.5,英语成绩为:92.0,Java程序设计成绩为:96.0

 

你可能感兴趣的:(java,开发语言)