zoukankan      html  css  js  c++  java
  • 班级,教师,学生类

    学习java类之间继承,以及关联关系时的代码,可以在控制台上输入,实现相应的操作

    主函数CMain

      1 package com.guigu.bingzi.main;
      2 
      3 import java.util.Scanner;
      4 
      5 
      6 public class CMain 
      7 {
      8     public static void main(String[] args) 
      9     {
     10         Scanner sc = new Scanner(System.in);
     11         System.out.println("学籍信息管理");
     12         while(true)
     13         {
     14             System.out.println("*********************");
     15             System.out.println("0.退出管理");
     16             System.out.println("1.输入班级信息");
     17             System.out.println("2.输入学生信息");
     18             System.out.println("3.输入教师信息");
     19             System.out.println("*********************");
     20             
     21             System.out.println("请输入你要操作的编号");
     22             int n = sc.nextInt();
     23             if(n==0)
     24             {
     25                 System.out.print("结束输入信息");
     26                 break;
     27             }
     28             else if(n==1)
     29             {
     30                 TClass tClass = new TClass();
     31                 while(true)
     32                 {
     33                     System.out.println("是否需要要输入班级的信息(Y/N)");
     34                     String temp = null;
     35                     temp = sc.next();
     36                     if(temp.equalsIgnoreCase("N"))    
     37                         //equals区分大小写  equalsIgnoreCase不区分大小写
     38                     {
     39                         System.out.println("结束输入班级信息");
     40                         break;
     41                     }
     42                     System.out.println("请输入班级编号:");
     43                     tClass.setNo(sc.next());
     44                     
     45                     System.out.println("请输入班级名称:");
     46                     tClass.setName(sc.next());
     47                     
     48                     System.out.println("班级编号:" + tClass.getNo());
     49                     System.out.println("请输入班级名称:" + tClass.getName());
     50                 }
     51                 /*如果有多个班级怎么办*/
     52             }
     53             else if(n==2)
     54             {
     55                 Student stu = null;
     56                 TClass tClass=new TClass();
     57                 while(true)
     58                 {
     59                     System.out.println("是否需要要输入学生的信息(Y/N)");
     60                     String temp = null;
     61                     temp = sc.next();
     62                     if(temp.equalsIgnoreCase("N"))
     63                     {
     64                         System.out.println("结束输入学生信息");
     65                         break;
     66                     }
     67                     stu = new Student();
     68                     System.out.println("请输入学生的姓名:");
     69                     stu.setName(sc.next());
     70                     
     71                     System.out.println("请输入学生的性别:");
     72                     stu.setSex(sc.next());
     73                     
     74                     System.out.println("请输入学生的年龄:");
     75                     stu.setAge(sc.nextInt());
     76                     
     77                     System.out.println("请输入学生的身高(cm):");
     78                     stu.setHeight(sc.nextInt());
     79                     
     80                     System.out.println("请输入学生的体重(kg):");
     81                     stu.setWeight(sc.nextInt());
     82                     
     83                     System.out.println("请输入学生的成绩:");
     84                     stu.setScore(sc.nextInt());
     85                     
     86                     System.out.println("请输入学生的班级编号:");
     87                     tClass.setNo(sc.next());
     88                     
     89                     System.out.println("请输入学生的班级名称:");
     90                     tClass.setName(sc.next());
     91                     
     92                     tClass.addStudent(stu);    
     93                     System.out.println("************:" +stu.toString());
     94                     
     95                     System.out.println("学生的姓名:" +stu.getName());
     96                     System.out.println("学生的性别:" + stu.getSex());
     97                     System.out.println("学生的年龄:" + stu.getAge());
     98                     System.out.println("学生的身高(cm):" + stu.getHeight());
     99                     System.out.println("学生的体重(kg):" + stu.getWeight());
    100                     System.out.println("学生的成绩:" + stu.getScore());
    101                     System.out.println("学生的班级编号:" + tClass.getNo());
    102                     System.out.println("学生的班级名称:" + tClass.getName());
    103                 }
    104             }
    105             else if(n==3)
    106             {
    107                 Teacher tea = null;
    108                 TClass tClass=new TClass();
    109                 while(true)
    110                 {
    111                     System.out.println("是否需要要输入教师的信息(Y/N)");
    112                     String temp = null;
    113                     temp = sc.next();
    114                     if(temp.equalsIgnoreCase("N"))
    115                     {
    116                         System.out.println("结束输入教师信息");
    117                         break;
    118                     }
    119                     tea = new Teacher();
    120                     
    121                     System.out.println("请输入教师的姓名:");
    122                     tea.setName(sc.next());
    123                     
    124                     System.out.println("请输入教师的性别:");
    125                     tea.setSex(sc.next());
    126                     
    127                     System.out.println("请输入教师的年龄:");
    128                     tea.setAge(sc.nextInt());
    129                     
    130                     System.out.println("请输入教师的身高(cm):");
    131                     tea.setHeight(sc.nextInt());
    132                     
    133                     System.out.println("请输入教师的体重(kg):");
    134                     tea.setWeight(sc.nextInt());
    135                     
    136                     System.out.println("请输入教师的类型:");
    137                     tea.setType(sc.nextInt());
    138                     
    139                     while(true)
    140                     {
    141                         System.out.println("是否需要要输入教师所负责班级的信息(Y/N)");
    142                         String flag = null;
    143                         flag = sc.next();
    144                         if(flag.equalsIgnoreCase("N"))
    145                         {
    146                             System.out.println("结束输入教师所教班级信息");
    147                             break;
    148                         }
    149                         System.out.println("请输入教师所教的班级编号:");
    150                         tClass.setNo(sc.next());
    151                         
    152                         System.out.println("请输入教师所教的班级名字:");
    153                         tClass.setName(sc.next());
    154 
    155                         System.out.println("**********");
    156                         tClass.addTeacher(tea);
    157                         //tea.addTClass(tClass);
    158                         System.out.println("**********");
    159                         //有错误
    160                     }
    161                     System.out.println("教师的姓名:" +tea.getName());
    162                     System.out.println("教师的性别:" + tea.getSex());
    163                     System.out.println("教师的年龄:" + tea.getAge());
    164                     System.out.println("教师的身高(cm):" + tea.getHeight());
    165                     System.out.println("教师的体重(kg):" + tea.getWeight());
    166                     System.out.println("教师的类型:" + tea.getType());
    167                     System.out.println("教师所负责班级:");
    168                     for(int i=0; i<tea.getClassList().size(); i++)
    169                     {
    170                         System.out.println(tea.getClassList().get(i).toString());
    171                     }
    172                 }
    173             }
    174             else
    175             {
    176                 
    177             }
    178         }
    179     }
    180 }
    View Code

    人类People

      1 package com.guigu.bingzi.main;
      2 
      3 public class People 
      4 {
      5     /**
      6      * 姓名
      7      */
      8     private String name = null;
      9 
     10     /**
     11      * 性别
     12      */
     13     private String sex = null;
     14     
     15     /**
     16      * 年龄
     17      */
     18     private int age = 0;
     19     
     20     /**
     21      * 身高
     22      * 单位:cm
     23      */
     24     private int height = 0;
     25     
     26     /**
     27      * 体重
     28      * 单位:kg
     29      */
     30     private int weight = 0;
     31     
     32     /**
     33      * 默认构造函数
     34      */
     35     public People()
     36     {
     37         this("","",0,0,0);
     38     }
     39     
     40     /**
     41      * 构造函数
     42      * @param name 姓名
     43      * @param sex 性别
     44      * @param age 年龄
     45      * @param height 身高 cm
     46      * @param weight 体重 kg
     47      */
     48     public People(String name,String sex,int age,int height,int weight)
     49     {
     50         this.name = name;
     51         this.sex = sex;
     52         this.age = age;
     53         this.height = height;
     54         this.weight = weight;
     55     }
     56     
     57     @Override
     58     public String toString() {
     59         return "People [name=" + name + ", sex=" + sex + ", age=" + age
     60                 + ", height=" + height + ", weight=" + weight + "]";
     61     }
     62 
     63     /**
     64      * 判断身高体重是否正常
     65      * @return 0:正常   1:偏胖  -1:偏瘦   2:异常
     66      */
     67     public int isOK()
     68     {
     69         if(this.sex.equals("男"))
     70         {
     71             if(this.height - 105 == this.weight)
     72             {
     73                 return 0; //正常
     74             }
     75             else if(this.height - 105 < this.weight)
     76             {
     77                 return 1; //偏胖
     78             }
     79             else
     80             {
     81                 return -1; //偏瘦
     82             }
     83         }
     84         else if(this.sex.equals("女"))
     85         {
     86             if(this.height - 115 == this.weight)
     87             {
     88                 return 0; //正常
     89             }
     90             else if(this.height - 105 < this.weight)
     91             {
     92                 return 1; //偏胖
     93             }
     94             else
     95             {
     96                 return -1; //偏瘦
     97             }
     98         }
     99         else
    100         {
    101             return 2; //性别不详
    102         }
    103     }
    104     public String getName() {
    105         return name;
    106     }
    107 
    108     public void setName(String name) {
    109         this.name = name;
    110     }
    111 
    112     public String getSex() {
    113         return sex;
    114     }
    115 
    116     public void setSex(String sex) {
    117         this.sex = sex;
    118     }
    119 
    120     public int getAge() {
    121         return age;
    122     }
    123 
    124     public void setAge(int age) {
    125         this.age = age;
    126     }
    127 
    128     public int getHeight() {
    129         return height;
    130     }
    131 
    132     public void setHeight(int height) {
    133         this.height = height;
    134     }
    135 
    136     public int getWeight() {
    137         return weight;
    138     }
    139 
    140     public void setWeight(int weight) {
    141         this.weight = weight;
    142     }
    143     
    144 }
    View Code

    学生类Student

     1 package com.guigu.bingzi.main;
     2 
     3 public class Student extends People
     4 {
     5     /**
     6      * 学生成绩
     7      * 百分制
     8      */
     9     private int score;
    10     
    11     /**
    12      * 学生班级
    13      */
    14     public TClass tClass;
    15     
    16     /**
    17      * 默认构造函数
    18      */
    19     public Student()
    20     {
    21         this(null,null,0,0,0,0,null);    //同一个函数总不能同时出现super和this,所以不能调用super
    22     }
    23     
    24     /**
    25      * 构造函数
    26      * @param name 姓名
    27      * @param sex 性别
    28      * @param age 年龄
    29      * @param height 身高
    30      * @param weight 体重
    31      * @param score 成绩
    32      * @param tClass 班级
    33      */
    34     public Student(String name,String sex,int age,int height,int weight,int score,TClass tClass)
    35     {
    36         super(name,sex,age,height,weight);
    37         this.score = score;
    38         this.tClass = tClass;
    39     }
    40 
    41     public int getScore() {
    42         return score;
    43     }
    44 
    45     public void setScore(int score) {
    46         this.score = score;
    47     }
    48 
    49     public TClass gettClass() {
    50         return tClass;
    51     }
    52 
    53     public void settClass(TClass tClass) {
    54         this.tClass = tClass;
    55     }
    56 
    57     public String getName() {
    58         return super.getName();
    59     }
    60 
    61     public String getSex() {
    62         return super.getSex();
    63     }
    64 
    65     public int getAge() {
    66         return super.getAge();
    67     }
    68 
    69     public int getHeight() {
    70         return super.getHeight();
    71     }
    72 
    73     public int getWeight() {
    74         return super.getWeight();
    75     }
    76 
    77     @Override
    78     public String toString() {
    79         return super.toString() + "Student [score=" + score + ", tClass=" + tClass + "]";
    80     }
    81     
    82     
    83 }
    View Code

    班级类TClass

      1 package com.guigu.bingzi.main;
      2 
      3 import java.util.ArrayList;
      4 
      5 public class TClass //因为系统中存在class关键字,前边加T,用来区分
      6 {    
      7     /**
      8      * 班级编号
      9      */
     10     private String no = null;
     11     
     12     /**
     13      * 班级名称
     14      */
     15     private String name = null;
     16     
     17     /**
     18      * 班级中存储的学生信息
     19      */
     20     private ArrayList<Student> stuList = null;
     21     
     22     /**
     23      * 班级中存储的教师信息
     24      */
     25     private ArrayList<Teacher> teaList = null;    
     26         //在开辟TClass内存时开辟了这个集合内存(在构造函数里边),不能定义在主函数里边
     27     
     28     /**
     29      * 默认构造函数
     30      */
     31     public TClass()
     32     {
     33         this("","",new ArrayList<Student>(),new ArrayList<Teacher>());
     34         //这边老师的方法有问题,应该是new ArrayList<Student>(),new ArrayList<Teacher>(),如果定义成null,会出现空指针异常
     35     }
     36     
     37     /**
     38      * 构造函数
     39      * @param no 编号
     40      * @param name    名称
     41      * @param stuList 班级中存储的学生信息
     42      * @param teaList 班级中存储的教师信息
     43      */
     44     public TClass(String no,String name,ArrayList<Student> stuList,ArrayList<Teacher> teaList)
     45     {
     46         this.no = no;
     47         this.name = name;
     48         this.stuList = stuList;
     49         this.teaList = teaList;
     50      }
     51     
     52     /**
     53      * 向班级中添加学生对象(双向关联)
     54      * @param stu 学生对象
     55      */
     56     public void addStudent(Student stu)
     57     {
     58         this.stuList.add(stu);
     59         stu.settClass(this);
     60     }
     61     
     62     /**
     63      * 向班级中添加学生对象(双向关联)
     64      * @param stu 学生对象
     65      */
     66     public void addTeacher(Teacher tea)
     67     {
     68         this.teaList.add(tea);
     69         tea.addTClass(this);
     70     }
     71     
     72     public String getNo() {
     73         return no;
     74     }
     75 
     76     public void setNo(String no) {
     77         this.no = no;
     78     }
     79 
     80     public String getName() {
     81         return name;
     82     }
     83 
     84     public void setName(String name) {
     85         this.name = name;
     86     }
     87 
     88     public ArrayList<Student> getStuList() {
     89         return stuList;
     90     }
     91 
     92     public void setStuList(ArrayList<Student> stuList) {
     93         this.stuList = stuList;
     94     }
     95 
     96     public ArrayList<Teacher> getTeaList() {
     97         return teaList;
     98     }
     99 
    100     public void setTeaList(ArrayList<Teacher> teaList) {
    101         this.teaList = teaList;
    102     }
    103     
    104 }
    View Code

    教师类Teacher

     1 package com.guigu.bingzi.main;
     2 
     3 import java.util.ArrayList;
     4 
     5 public class Teacher extends People
     6 {
     7     /**
     8      * 教师负责班级
     9      */
    10     private ArrayList<TClass> classList = null;
    11     
    12     /**
    13      * 教师类型
    14      * 0:班主任 1:授课教师
    15      */
    16     private int type;
    17     
    18     public Teacher()
    19     {
    20         classList = new ArrayList <TClass>();
    21         
    22         //this(null,null,0,0,0,0,t);    //同一个函数总不能同时出现super和this,所以不能调用super
    23     }
    24     
    25     /**
    26      * 构造函数
    27      * @param name 姓名
    28      * @param sex 性别
    29      * @param age 年龄
    30      * @param height 身高
    31      * @param weight 体重
    32      * @param type 教师类型  0:班主任   1:授课教师
    33      * @param tClass 班级
    34      */
    35     public Teacher(String name,String sex,int age,int height,int weight,int type,ArrayList<TClass> classList)
    36     {
    37         super(name,sex,age,height,weight);
    38         this.type = type;
    39         this.classList = classList;
    40     }
    41 
    42     /**
    43      * 添加授课班级
    44      * @param tClass 班级对象
    45      */
    46     public void addTClass(TClass tClass)
    47     {
    48         if(tClass==null)
    49             System.out.println("tClass is null");
    50         this.classList.add(tClass);
    51     }
    52     
    53     public ArrayList<TClass> getClassList() {
    54         return classList;
    55     }
    56 
    57     public void setClassList(ArrayList<TClass> classList) {
    58         this.classList = classList;
    59     }
    60 
    61     public int getType() {
    62         return type;
    63     }
    64 
    65     public void setType(int type) {
    66         this.type = type;
    67     }
    68 
    69     public String getName() {
    70         return super.getName();
    71     }
    72 
    73     public String getSex() {
    74         return super.getSex();
    75     }
    76 
    77     public int getAge() {
    78         return super.getAge();
    79     }
    80 
    81     public int getHeight() {
    82         return super.getHeight();
    83     }
    84 
    85     public int getWeight() {
    86         return super.getWeight();
    87     }
    88 }
    View Code
  • 相关阅读:
    面向对象编程-介绍(python3入门)
    课堂作业03
    软件工程个人作业04
    团队介绍
    学习进度条
    课堂作业02
    学习进度条
    软件工程个人作业03
    课堂作业01
    软件工程个人作业02
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3425138.html
Copyright © 2011-2022 走看看