zoukankan      html  css  js  c++  java
  • Java如何设计并编写类

    Unit3如何设计并编写类

    一、术语

    1. 类就是类型的简称,是抽象的、不具体的、不明确的。比如:学生、汽车、手机、电脑

    2. 对象

      对象是类的实例,是具体的,明确的,实际干活靠对象完成。比如:李楠,华为P20

    3. 属性

      比如:学生的姓名,性别,身高,体重

    4. 方法

      在Java中把函数叫做方法。

      方法完成特定的功能,具有动词性质。

    5. OOP:面向对象编程

      Object Oriented Programming

    二、理解

    1. 面向理解为:按照、使用、针对;面向对象编程就是按对象编程、使用对象编程、针对对象编程。

    2. 万物皆对象

    3. 程序是对象的集合,对象之间协作完成功能,对象之间彼此通信

    4. 面向对象思想来源于人的实际生活,人的生活就是面向对象的

    三、Java编程步骤

    编写类->创建对象->对象.方法(或属性)

    四、设计类

    1. 设计类需要用UML工具,softwareIdeasModeler

      image-20210204123438681

    2. 一个类由3部分构成:类名,属性,方法

    3. 编写Student类

      public class Student {
         String studentID;
         String studentName;
         String gender = "男";
         int age;
         String nation;

         // 方法由5部分构成,
         // 1.public 访问控制符 2. 返回类型 void 3. 方法名 eat 4. 形参 5. 方法体{}

         /**
          * 吃方法
          * @param food
          * @return 好吃
          */
         public String eat(String food) {
             System.out.println("正在吃:"+food);
             return "好吃";
        }
         public void speak() {
             System.out.println("大家好");
        }

         public void bite(String name) {
             System.out.println(name + ":被咬了");
        }
         public String bite(String school,String name) {
             System.out.println(school + "学校的" + name + ":被咬了");
             return name + "喊:疼啊!";
        }
      }
    4. 生成Javadoc文档

      javadoc -encoding utf-8 Student.java
    5. 编写Calc类,实现加法,减法,乘法,除法运算

      public class Calc {
         /**
          * 实现加法运算
          * @param num1 第一个整数
          * @param num2 第二个整数
          * @return 加法计算得到的结果
          */
         public int add(int num1, int num2) {
             return num1+num2;
        }

         /**
          * 实现减法运算
          * @param num1 第一个整数
          * @param num2 第二个整数
          * @return 减法计算得到的结果
          */
         public int sub(int num1, int num2) {
             return num1-num2;
        }

         /**
          * 实现乘法运算
          * @param num1 第一个整数
          * @param num2 第二个整数
          * @return 乘法运算得到的结果
          */
         public int mul(int num1, int num2) {
             return num1 * num2;
        }

         /**
          * 实现除法运算
          * @param num1 第一个整数
          * @param num2 第二个整数
          * @return
          */
         public int div(int num1, int num2) {
             return num1/num2;
        }
      }

    五、面试题

    1. 面向对象的特征有哪些?

      封装,继承,多态,抽象。重要的三大特征:封装,继承,多态。

    2. 面向对象编程步骤?

      编写类->创建对象->对象.属性(或方法)

    六、任务

      1. 教务系统中需要计算学生年龄,设计学生类,提供计算年龄的方法,根据出生年份计算年龄

        public class StudentAge {
           /**
            * 根据出生日期计算学生的年龄
            * @param birth 学生的出生年份
            * @return 学生的年龄
            */
           public int receiveAge(int birth) {
               GregorianCalendar gregorianCalendar = new GregorianCalendar();
               int year = gregorianCalendar.get(Calendar.YEAR);
               return year-birth;
          }
        }
        // 新建一个测试类进行测试
        public class Test {
           public static void main(String[] args) {
               StudentAge studentAge = new StudentAge();
               System.out.println("1999年出生的学生今年" + studentAge.receiveAge(1999) + "岁了");
          }
        }
      2. 设计教务系统中的老师类Teacher,具体包括:

        属性:教师工号,教师姓名,教师年龄,教师学历,教师职称,月薪。其中教师学历默认为”硕士“,教师职称默认为”讲师“;月薪默认为3000.提供3个一般方法:计算教师年龄方法:参数是教师出生年份,计算教师工龄:参数是教师入职年份,计算教师年薪:按13个月计算。

        public class Teacher {
           /**
            * 教师工号
            */
           private int id;
           /**
            * 教师姓名
            */
           private String name;
           /**
            * 教师年龄
            */
           private int age;
           /**
            * 教师学历
            */
           private String education = "硕士";
           /**
            * 教师职称
            */
           private String professional = "讲师";
           /**
            * 月薪
            */
           private int pay = 3000;

           public int getId() {
               return id;
          }

           public void setId(int id) {
               this.id = id;
          }

           public String getName() {
               return name;
          }

           public void setName(String name) {
               this.name = name;
          }

           public int getAge() {
               return age;
          }

           public void setAge(int age) {
               this.age = age;
          }

           public String getEducation() {
               return education;
          }

           public void setEducation(String education) {
               this.education = education;
          }

           public String getProfessional() {
               return professional;
          }

           public void setProfessional(String professional) {
               this.professional = professional;
          }

           public int getPay() {
               return pay;
          }

           public void setPay(int pay) {
               this.pay = pay;
          }

           /**
            * 计算教师的年龄
            * @param age 教师出生年份
            * @return 教师的年龄
            */
           public int calculateAge(int age) {
               GregorianCalendar gregorianCalendar = new GregorianCalendar();
               int year = gregorianCalendar.get(Calendar.YEAR);
               return year-age;
          }

           /**
            * 计算教师工龄
            * @param beginyear 教师的入职年份
            * @return 教师的工龄
            */
           public int workYear(int beginyear) {
               GregorianCalendar gregorianCalendar = new GregorianCalendar();
               int year = gregorianCalendar.get(Calendar.YEAR);
               return year-beginyear;
          }

           /**
            * 计算教师的年薪
            * @return 教师的年薪
            */
           public int calSal() {
               return this.pay*13;
          }
        }
        // 测试类
        public class Test {
           public static void main(String[] args) {
               Teacher teacher = new Teacher();
               System.out.println("教师的年龄为"+teacher.calculateAge(1997)+"岁");
               System.out.println("教师的年薪为"+teacher.calSal()+"元");
               System.out.println("教师的工龄为"+teacher.workYear(2001));
          }
        }
      3. 设计类:在教务系统中,老师输入3位学生张彤,李楠和程浩然的Java成绩,得到他们的平均分。

        public class Score {
           public float avgScore(float a, float b, float c) {
               return Math.round(a+b+c)/3;
          }
        }
        // 测试类
        public class Test {
           public static void main(String[] args) {
               Score score = new Score();
               System.out.println("三位学生的平均分位:"+score.avgScore(80, 75, 65));
          }
        }


    软件下载提取码:qwer
  • 相关阅读:
    OJ:自己实现一个简单的 priority_queue
    OJ:访问 const 成员函数问题
    OJ:重载 << 运算符
    Qt 编程中 namespace Ui { class Widget; } 解析
    QT 实现图片旋转的两种方法
    QTimer 的使用
    QT 完美实现圆形按钮
    QT 设置有效绘图区域
    基于 LWIP 建立 TCP Server 与主机通信实验
    大整数相乘 分治法 和 循环暴力法
  • 原文地址:https://www.cnblogs.com/ty0910/p/14375379.html
Copyright © 2011-2022 走看看