zoukankan      html  css  js  c++  java
  • 类设计分析

    1根据要求写出类所包含的属性。

    2所有的属性都必须进行封装(private)

    3封装之后的属性通过setter和getter设置和取得

    4如果需要可以加入若干构造方法

    5再根据其他要求添加相应的方法

    6类中的所有方法都不要直接输出,而是交给被调用处输出。

    package day06;
    class Student{                                                                               //定义学生类
          private String name;                                                                   //学生姓名
          private  String stuno;                                                                 //学生编号
          private float math;                                                                    //数学成绩
          private float English;                                                                 //英语成绩
          private float computer;                                                                //计算机成绩
          public Student() {
                                                                                                   //定义无参构造
          }
       public Student(String stuno,String name,float math,float English,float computer) {    //定义5个参数的构造方法,为类中的属性初始化
           this.setStuno(stuno);                                                              //设置编号
           this.setName(name);
           this.setMath(math);
           this.setEnglish(English);
           this.setComputer(computer);
       }
       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 r) {
           English=r;
       }
       public void setComputer(float e) {
           computer=e;
       }
       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 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 day06 {
        public static void main(String args[]) {
               Student stu=null;
               stu =new Student("MLDN-33","李华",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.getComputer());
               System.out.println("最高分:"+stu.max());
               System.out.println("最低分:"+stu.min());
               
        }
    }
  • 相关阅读:
    sed使用之处理文件命令, 注意不是sed的选项
    oracle label security(OLS)相关主体流程整理
    nginx配置使用, 入门到实践
    shell中sed的使用及选项, 编辑器中的&作用
    echo带颜色文本, shell中单引号和双引号区别, [], 反引号及位置参数使用
    vue-cli流程初识笔记
    Python之函数
    Django之ModelForm组件
    CRM【第三篇】: crm业务
    CRM【第二篇】: stark组件
  • 原文地址:https://www.cnblogs.com/huhaijie1/p/12422379.html
Copyright © 2011-2022 走看看