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 1public void run() {collectAllBeepers() ;dropAllBeepers() ;returnHome() ;
}

  • 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-1private void collectAllBeepers {while ( frontIsClear() ) {collectOneTower() ;move() ;}collectOneTower();
}   

level 2-1-1private void collectOneTower() {turnLeft() ;collectLineOfBeepers() ;turnAround() ;moveToWall() ;turnLeft() ;
}

level 2-1-1-1private void collectLineOfBeepers() {while( beepersPresent() ) {pickBeeper() ;if ( frontIsClear() ) {move() ;}}
}

level 2-2private void dropAllBeepers() {while(beepersInBag()) {putBeeper() ; }
}

level 2-3private void returnHome() {turnAround();moveToWall();turnAround();
}

level 2-3-1private void moveToWall() {while( frontIsClear() ) {move() ;}
}
 

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 编程时,这种策略通常有明显的优势。

Java prepare相关推荐

  1. Android10.0 startActivity启动过程

    原文地址:https://skytoby.github.io/2019/startActivity%E5%90%AF%E5%8A%A8%E8%BF%87%E7%A8%8B/ 基于Android10.0 ...

  2. android serialport new 软件退出_基于Android9.0,了解Android启动流程

    先记住四个进程和三种方式. **四个进程** 1.Launcher进程 2.system_server进程 3.App进程 4.Zygote进程 **三种方式** 1.Binder方式 2.Socke ...

  3. 安卓应用安全指南 4.10 使用通知

    4.10 使用通知 原书:Android Application Secure Design/Secure Coding Guidebook 译者:飞龙 协议:CC BY-NC-SA 4.0 Andr ...

  4. 源码详解Android 9.0(P) 系统启动流程之SystemServer

    源码详解Android 9.0(P) 系统启动流程目录: 源码详解Android 9.0(P)系统启动流程之init进程(第一阶段) 源码详解Android 9.0(P)系统启动流程之init进程(第 ...

  5. 【Android】java.lang.AssertionError use looper thread, must call Looper.prepare() first!异常分析

    java.lang.AssertionError: use looper thread, must call Looper.prepare() first! 在消息处理中必须先调用Looper类的pr ...

  6. java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() ...

  7. java looper_java-无法在某些设备上尚未调用Looper.prepare...

    我知道已经有很多这样的问题,但是我看不出我做错了什么.该应用程序崩溃,但未显示任何内容.另外,该错误不会在我的设备或仿真器上发生.仅在某些设备上(例如30-40%?). 无法在未调用Looper.pr ...

  8. java.io.IOException: Prepare failed.: status=0x1

    应该是名字有空格或者中文无法识别 1.直接改成纯英文名字 2.查了查资料和网上的解决办法如下: mp.setDataSource(PATH_TO_FILE); mp.prepare(); 这个地方修改 ...

  9. java.io.IOException: Prepare failed.: status=0x1 异常解决方法

    出现上述问题的解决办法: File file = new File(strPath); FileInputStream fis = new FileInputStream(file); mediaPl ...

  10. 语音播报:System.err: java.io.IOException: Prepare Async failed.: status=0x80000000

    系统错误,可能和资源文件的访问路径有关,可以尝试修改语音文件存储路径,如沙盒路径: 如:getExternalFilesDir("xxx").getPath(),及 /storag ...

最新文章

  1. 没有统计学基础可以学python-如何系统地自学 Python?
  2. lnmp之php5.6.29安装
  3. linux课程_linux系统使用课程更新提示
  4. 【ArcGIS Pro微课1000例】0001:Win7系统ArcGIS Pro2.5安装权威教程(附软件安装包下载)
  5. 9 WM层面 临时仓储类型的仓位 主数据不存在
  6. 中职学校计算机教学探讨,原创:探讨中职学校计算机专业实训教学原稿
  7. Suse系统用户不能登录报错
  8. Unity-多核优化1-C#JobSystem
  9. Unreal Engine 4 字符串转换
  10. rhel5 安装Oracle Database 10g Release 2(II)
  11. 10G网络布线方案有多少种?
  12. xui和嘟嘟桌面哪个好_小米这款手机桌面,好用到忘记原生桌面
  13. Johnson法则 BZOJ 3709 Bohater、洛谷 P1080 国王游戏、ZOJ3689 Digging
  14. laravel 发送邮件
  15. 漏洞系列一一看我一招征服漏洞SSRF
  16. java猜数字游戏实验报告_Java实现的猜数字游戏示例
  17. 2011夏天的那点事儿 ~~乌镇雷峰塔普陀山千
  18. 解决nginx启动失败
  19. Python版 孤勇者 | 画图+演奏+音乐可视化
  20. 商业分析师应如何构建一个商业故事

热门文章

  1. MySQL查询时当offset较大时查询效率低
  2. qca9377linux无线驱动,ubuntu下安装无线网卡去驱动Qualcomm-Atheros-QCA9377
  3. 训练自己的数据_YOLOv3训练自己的数据集(以口罩检测数据集为例)
  4. 全网首发:linux任务栏分组的研究
  5. 深圳40K都招不到嵌入式开发人员?
  6. com.google.common.base.Preconditions.checkNotNull(Preconditions.java:877)
  7. png2theora.c: undefined reference to `png_sizeof
  8. 大群就是公共场所,不要有事就在大群说
  9. 《我的祖国》正确英译应该是《The Evercountry, Mine》
  10. 一个简单的获取时间的程序