• java基础学习05(面向对象基础01--类实例分析)


    面向对象基础01(类实例分析)

    实现的目标

    1.如何分析一个类(类的基本分析思路)

    分析的思路

    1.根据要求写出类所包含的属性
    2.所有的属性都必须进行封装(private)
    3.封装之后的属性通过setter和getter设置和取得
    4.如果需要可以加入若干构造方法 
    5.再根据其它要求添加相应的方法
    6.类中的所有方法都不要直接输出,而是交给被调用处调用

    Demo

    定义并测试一个名为Student的类,包括属性有"学号"、"姓名"以及3门课程"数学"、"英语","计算机"的成绩,包括的方法有计算3门课程的"总分"、"平均分"、"最高分"和"最低分"。

    1.本类的属性及类型

    2.定义出需要的方法(构造方法、普通方法)

    3.类图

    4.编写代码

    /**
    *1.定义一个Student类并测试该类
    */
    class Student{
        private String stuid;       //学号
        private String name;        //姓名
        private float math;         //数学成绩
        private float english;      //英语成绩
        private float computer;     //计算机成绩
    
        public Student(){
    
        }
        //定义5个参数的构造方法,为类中的属性初始化
        public Student(String stuid,String name,float math,float english,float computer){
            this.stuid = stuid;
            this.name = name;
            this.math = math;
            this.english = english;
            this.computer = computer;
        }
    
        public void setStuid(String stuid){
            this.stuid = stuid;
        }
        public String getStuid(){
            return this.stuid;
        }
    
        public void setName(String name){
            this.name = name;
        }
        public String getName(){
            return this.name;
        }
    
        public void setMath(float math){
            this.math = math;
        }
        public float getMath(){
            return this.math;
        }
    
        public void setEnglish(float english){
            this.english = english;
        }
        public float getEnglish(){
            return this.english;
        }
    
        public void setComputer(float computer){
            this.computer = computer;
        }
        public float getComputer(){
            return this.computer;
        }
    
        public float sum(){
            float sum = this.math + this.english + this.computer;
            return sum;
        }
        public float avg(){
            float avg = (this.math + this.english + this.computer)/3;
            //float avg = this.sum()/3;
            return avg;
        }
        public float max(){
            float max = this.math;
            max = this.english > max ? this.english:max;
            max = this.computer > max ? this.computer:max; 
            return max;
        }
        public float min(){
            float min = this.math;
            min = this.english < min ? this.english:min;
            min = this.computer < min ? this.computer:min; 
            return min;
        }   
    }
    /**
    *1.编写测试类,测试以上代码
    */
    class Demo01{
        public static void main(String [] args){
            Student stu1 = new Student("1114020116","张三",95.0f,78.0f,85.0f); //实例化Student对象并通过构造方法赋值
            System.out.println("学号:"+stu1.getStuid());
            System.out.println("姓名:"+stu1.getName());
            System.out.println("数学成绩:"+stu1.getMath());
            System.out.println("英语成绩:"+stu1.getEnglish());
            System.out.println("计算机成绩:"+stu1.getComputer());
    
            System.out.println("成绩总和:"+stu1.sum());
            System.out.println("成绩平均值:"+stu1.avg());
            System.out.println("最高分:"+stu1.max());
            System.out.println("最低分:"+stu1.min());
        }
    }
    
    Process started >>>
    学号:1114020116
    姓名:张三
    数学成绩:95.0
    英语成绩:78.0
    计算机成绩:85.0
    成绩总和:258.0
    成绩平均值:86.0
    最高分:95.0
    最低分:78.0
    <<< Process finished. (Exit code 0)
  • 相关阅读:
    keepalive高可用
    springboot中bean的重定义
    jni有关知识点总结
    java并发问题总结
    openoffice将word转pdf中文乱码或消失的坑
    Redis实现分布式锁(Set和Lua)
    Redis java使用
    Redis 分区
    Redis 管道技术
    Redis 连接
  • 原文地址:https://www.cnblogs.com/billyzh/p/5343111.html
Copyright © 2020-2023  润新知