zoukankan      html  css  js  c++  java
  • Java prepare

    1. 分解

    关于如何分解 ?

    分解是将问题分解,要具有逻辑性,而不是将程序分解。

    - 分解后的函数只做 1 件事,具有普遍性,这样就可以反复利用

    - 函数包含 1 ~ 15 行

    - 给函数起一个好名字, 一目了然知道函数的意义 ( 能给方法起 一个简单的好的名字,并且方法也是做该名字对应的事情,是很关键的,是好的分配方案 )

    - comment ( 因为只做一件事,所以,注释明确 )

    In genreal, deciding how to decompose a program is not easy. In fact, as the problems become more complex, choosing an appropriate decomposition will turn out to be one of the more difficult aspects of programing.

    In object-oriented program, the programmer’s attention shifts away from the procedural specification of operations and focuses instead on modeling the behavior of conceptually integrated unites called objects.

    One of the primary advantages of the object-oriented paradigm is that it encourages programmers to recognize the fundamental relationship between the state of an object and its behavior.

    尽管分解这个方法饱受批评,但是也还是大有用处,另外可以参考一些其他算法。

    算法 + 数据结构 = 程序, 算法依赖数据结构,算法定下来后,依赖数据结构确定算法实现。

    2. 可读性

    程序的可读性是非常重要的,当需要修改程序时( 大部分都需要修改 ).

    3. 自顶向下方法 ( & 分解 , 面向对象也适用 )

    也叫 逐步求精方法 Stepwise Refinement , which consists of solving problems by starting with the problem as a whole. You break the whole problem down into pieces, and then solve each piece, breaking those down further if necessary.

    例子 : 收集所有的 beeper.

    以上的自顶向下,不是指程序的顺序自上而下,而是指层次,从外到内 :

    • level 1 : you should start the design of your program from the top, which refers to the level of the program that is conceptually highest and most abstract. At this level, the beeper tower problem is clearly divided into three independent phases. First, Karel has to collect all the beepers. Second, Karel has to deposit them on the last intersection, Third, Karel has to return to its home position. This conceptual decomposition of the problem suggests that the run method for this program will have the following structure :
    level 1
    • level 2-1 : 首先考虑第一个函数,收集所有的 beeper, 要考虑使用一个 while循环,将所有的beeper收集,First of all, you should think about the conditional test. ( 循环停止的条件 ). You want Karel to stop when it hits the wall at the end of the row. Thus, you want Karel to keep going as long as the space in front is clear.

        

    level 2-1   
    level 2-1-1
    level 2-1-1-1
    level 2-2
    level 2-3
    level 2-3-1
     

    4. 方法论

    Given the fact that writing programs on your own and getting them to run on the computer are essential to learning about programming.

    Things that seem very clear on the page can be difficult to put into practice. ( 真的很对 )

    In object-oriented programming , the programmer’s attention shifts away from the procedural specification of operations and focuscs instead on modeling the behavior of conceptually integrated units called objects.

    One of the primary advantages of the object-oriented paradigm is that it encourages programmers to recognize the fundamental relationship between the state of an object and its behavior.

    The state of an object consists of a set of attributes that pertain to that object and might change over time.

    5. 注释 模板

    开头注释 : they are extremely important as a means of documenting the design of larger, more complex programs. ( 整个文件开头 )

    /*
     * File : HelloWorld.java
     * -----------------------------
     * The program will show " hello, world ".
     */

    函数注释 ( 方法 ) : 要写清楚函数的参数,结果/返回,做什么

    单行注释 : // 简单说明

    6. 常犯错误

    - 无限循环

    - 边界问题

    7. 算法

    算法: 解决问题的办法,好的算法设计,是解决问题的关键,( 而算法又依赖于数据在计算机中的存储 - 数据结构 )

    comming up with the right algorithm often leads to extremely simple code.


    video 01

    1. if 语句 always use {}
    2. for 循环执行顺序
    - 初始化
    - 判断条件
    - 执行语句
    - 自增

    3. 编程人员面临的最重要挑战是要想办法减少程序概念上的复杂性,大型程序通常难以从总体上理解。能够让人理解这些程序的唯一方法时将它们分解为更简单,更易处理的部分。这个过程就是分解。

    分解是一条基本侧列,应用于编程过程的各个层次,在 Java 中,大型系统首先分解成 程序包,程序包再分解成类,类本身就是一套方法。

    学习如何找出最有用的分解需要大量的实践,如果合适定义单个子任务,每个子任务最为单元有概念上的完整性,会让程序从整体上更容易理解,如果选择的子任务不合适,分解就会遇到麻烦,选择特定分解没有必须遵守的规则,通过事件会知道如何应用该方法。

    - 逐步细化(假设程序很大,需要分解,下一步就是将整个问题分解为主要组件,明白主要子任务是什么,然后再重复分解子任务,进行下去,直到单个任务都非常简单,可以说作为单个方法执行,这个过程称为 从上到下设计。

    学习简单示例是理解逐步细化过程的最好方法:

    假设要画 一个由 3 节车厢组成的火车。( 车头,车身,车尾 ), 具体如下:

    首先: 火车看起来很复杂,但是都是由 GRect 等等,已知类组成的。( 条件准备 )

    level1 : 分成 3 个方法: drawEngine , drawBoxcar , drawCaboose

    好处 : 程序更容易编写, 当我们考虑 drawEngine 方法时,不需要考虑 drawBoxcar, drawCaboose, 这样就使程序简化。

             程序更容易阅读,因为结构清晰,所以比较方便程序员阅读。

    接下来就是决定这些方法如何获得正确的绘图信息 ( 参数 ) , 有以下 2 种 方法 :

    a : 以参数的形式从调用者那里传递信息。

    b : 通过定义有合适值的命名变量来包含作为程序的一部分信息。

    综合以上 : 如果 单个调用者可能给形参提供不同的值时,使用实参。

                   调用者如果对选择的值满意,使用命名常量

    在火车图片示例中,调用者可能对控制车轮半径不感兴趣,因此,它的值可以指定为命名常量。另一方面,调用者确实需要指定特定车厢应该放置在画布的什么位置,因此应该通过实参传达这种信息。

    所以: 可以确定方法如下 :

    method
    1 private void drawEngine(double x, double y)
    2 
    3 private void drawBoxcar(double x, double y, Color color)
    4 
    5 private void drawCabbose(double x, double y)

    另外其中参数定义情况  :

    value
    1 private static final double CAR_WIDTH = 75;
    2 
    3 private static final double CAR_HEIGHT = 36;

    接下来,不是亟不可待的开始每个方法的完成 。-寻找共性- 而是观察这三个方法之间有没有什么共性之类的,例如:画火车的内容,我们会发现,画车头,车身,车尾,基本上框架是一样的,一个矩形。。等等,那么,我们可以定义一个画框架的方法,然后再根据以上3个方法,在框架的基础上在画 ( 貌似可以定义一个类啊,什么的,但是因为目前没有涉及到 )

    逐步细化是一种相当重要的技能,与此同时,面向对象语言还提供了另一种简化问题的方法,这种方法不是将程序分解为连续执行更简单的子任务,而是通过定义类得层次结构( 反映了相同分解策略 )实现相同的目标。用 Java 编程时,这种策略通常有明显的优势。

    C 语言 与 java 语言的编译过程

     -- C 语言

    -- Java 语言

    可以看到, java 编译后的结果是一个 JAR 文件, 而这个 JAR 文件可以到处运行在带有 JAVE 运行环境的机器上. (一处编译, 到处运行) 

    命令: javac 实际上是java编译器将源文件编译为 类文件.  

  • 相关阅读:
    PHP 使用 Redis
    Redis /etc/redis.conf 常用配置
    Redis 常用操作
    Redis 操作哈希数据
    Redis 操作有序集合数据
    Redis 操作集合数据
    Redis 操作列表数据
    Redis 操作字符串数据
    Redis 安装
    Redis 简介
  • 原文地址:https://www.cnblogs.com/moveofgod/p/2815245.html
Copyright © 2011-2022 走看看