[Java] 大学生抽象类 →→→→→进入此内容的聊天室

来自 , 2019-03-23, 写在 Java, 查看 160 次.
URL http://www.code666.cn/view/464d828b
  1.  
  2.  
  3. import java.util.Scanner;
  4. public class Main{
  5.     public static void main(String[] args) {
  6.          Scanner scan = new Scanner(System.in);
  7.          int no = scan.nextInt();
  8.          String name = scan.next();      
  9.          String sex = scan.next();            
  10.          String major = scan.next();
  11.          int score = scan.nextInt();
  12.          CollegeStudent c = new CollegeStudent(no, name, sex, major, score);
  13.          c.getGrade();
  14.          no = scan.nextInt();
  15.          name = scan.next();      
  16.          sex = scan.next();      
  17.          major = scan.next();
  18.          String supervisor = scan.next();
  19.          score = scan.nextInt();
  20.          GraduateStudent g = new GraduateStudent(no, name, sex, major, supervisor, score);
  21.          g.getGrade();
  22.  
  23.          scan.close();  
  24.     }
  25. }  
  26.  
  27. abstract class Student{
  28.     private int no;
  29.     private String name;
  30.     private String sex;
  31.     private int score;
  32.     public Student(int _no, String _name, String _sex, int _score){
  33.         no = _no;
  34.         name = _name;
  35.         sex = _sex;
  36.         score = _score;
  37.     }
  38.     public int getNo() {
  39.         return no;
  40.     }
  41.     public String getName() {
  42.         return name;
  43.     }
  44.     public String getSex() {
  45.         return sex;
  46.     }
  47.     public int getScore() {
  48.         return score;
  49.     }
  50.     public abstract void getGrade();
  51. }
  52.  
  53. class CollegeStudent extends Student{
  54.     private String major;
  55.     public CollegeStudent(int _no, String _name, String _sex, String _major, int _score) {
  56.         super(_no, _name, _sex, _score);
  57.         major = _major;
  58.     }
  59.     public int getNo() {
  60.         return super.getNo();
  61.     }
  62.     public String getName() {
  63.         return super.getName();
  64.     }
  65.     public String getSex() {
  66.         return super.getSex();
  67.     }
  68.     public String getMajor() {
  69.         return major;
  70.     }
  71.     public void getGrade() {
  72.         if(super.getScore()>=80 && super.getScore()<=100)
  73.                 System.out.println("A");
  74.         else if(super.getScore()>=70 && super.getScore()<80)
  75.                 System.out.println("B");
  76.         else if(super.getScore()>=60 && super.getScore()<70)
  77.                 System.out.println("C");
  78.         else if(super.getScore()>=50 && super.getScore()<60)
  79.                 System.out.println("D");
  80.         else
  81.                 System.out.println("E");
  82.     }
  83. }
  84.  
  85. class GraduateStudent extends CollegeStudent{
  86.     private String supervisor;
  87.     public GraduateStudent(int _no, String _name, String _sex, String _major,  String _supervisor, int  _score) {
  88.         super(_no, _name, _sex, _major, _score);
  89.         supervisor = _supervisor;
  90.     }
  91.     public void getGrade() {
  92.         if(super.getScore()>=90 && super.getScore()<=100)
  93.                 System.out.println("A");
  94.         else if(super.getScore()>=80 && super.getScore()<90)
  95.                 System.out.println("B");
  96.         else if(super.getScore()>=70 && super.getScore()<80)
  97.                 System.out.println("C");
  98.         else if(super.getScore()>=60 && super.getScore()<70)
  99.                 System.out.println("D");
  100.         else
  101.                 System.out.println("E");
  102.     }
  103. }
  104.  

回复 "大学生抽象类"

这儿你可以回复上面这条便签

captcha