美文网首页
第五章:面向对象基础——实例讲解、类设计分类(学生类)。

第五章:面向对象基础——实例讲解、类设计分类(学生类)。

作者: 孤意的学习笔记 | 来源:发表于2017-09-27 11:40 被阅读0次

    本章目标

    • 掌握类的基本分析思路
    • 应用思路分析具体的题目

    1、具体内容

    class Student{
        private String stuno ;
        private String name ;
        private float math ;
        private float english ;
        private float computer ;
        public Student(){}
        public Student(String s,String n,float m,float e,float c){
            this.setStuno(s) ;
            this.setName(n) ;
            this.setMath(m) ;
            this.setEnglish(e) ;
            this.setComputer(c) ;
        }
        public void setStuno(String s){
            stuno = s ;
        }
        public void setName(String n){
            name = n ;
        }
        public void setMath(float m){
            math = m ;
        }
        public void setEnglish(float e){
            english = e ;
        }
        public void setComputer(float c){
            computer = c ;
        }
        public String getStuno(){
            return stuno ;
        }
        public String getName(){
            return name ;
        }
        public float getMath(){
            return math ;
        }
        public float getEnglish(){
            return english ;
        }
        public float getComputer(){
            return computer ;
        }
        public float sum(){     // 求和操作
            return  math + english + computer ;
        }
        public float avg(){     // 求平均值
            return this.sum() / 3 ;
        }
        public float max(){     // 求最高成绩
            float max = math ;  // 数学是最高成绩
            max = max>computer?max:computer ;
            max = max>english?max:english ;
            return max ;
        }
        public float min(){     // 求最低成绩
            float min = math ;  // 数学是最高成绩
            min = min<computer?min:computer ;
            min = min<english?min:english ;
            return min ;
        }
    };
    public class ExampleDemo01{
        public static void main(String args[]){
            Student stu = null ;            // 声明对象
            stu = new Student("stu-16","张兴华",95.0f,89.0f,96.0f) ;
            System.out.println("学生编号:" + stu.getStuno()) ;
            System.out.println("学生姓名:" + stu.getName()) ;
            System.out.println("数学成绩:" + stu.getMath()) ;
            System.out.println("英语成绩:" + stu.getEnglish()) ;
            System.out.println("最高分:" + stu.max()) ;
            System.out.println("最低分:" + stu.min()) ;
        }
    };
    
    输出结果:
    学生编号:stu-16
    学生姓名:张兴华
    数学成绩:95.0
    英语成绩:89.0
    最高分:96.0
    最低分:89.0
    

    2、总结

    题目的分析需要一点点慢慢的积累

    相关文章

      网友评论

          本文标题:第五章:面向对象基础——实例讲解、类设计分类(学生类)。

          本文链接:https://www.haomeiwen.com/subject/ttgyextx.html