Libraries · 库
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| library/ˈlaɪbrəri/ | 库 | kù |
| from scratch/frɒm skrætʃ/ | 从头开始 | cóng tóu kāi shǐ |
| API/ˌeɪ piː ˈaɪ/ | 应用程序接口 | yìng yòng chéng xù jiē kǒu |
| documents/ˈdɒkjuːmənts/ | 记录 | jì lù |
| functionality/ˌfʌŋkʃəˈnælɪti/ | 功能 | gōng néng |
| abstraction/əbˈstrækʃn/ | 抽象 | chōu xiàng |
Reusing code others have written
- A library 库 is a collection of procedures you can reuse across programs.
- Rather than write everything from scratch 从头开始, you use code others have already written and tested.
- That saves time and reduces bugs.
- Almost every real program leans on libraries.
重用别人写的代码
- 一个库(library)是一组你能跨程序重用的过程。
- 与其从头开始(from scratch)写一切,你用别人已经写好并测试过的代码。
- 那节省时间并减少缺陷。
- 几乎每个真实程序都依赖库。
A library is: · 一个库是:
It lets you reuse code instead of writing from scratch. · 它让你重用代码,而非从头开始写。
Reading the API
- To use a library well, you read its API 应用程序接口 — the application program interface.
- The API documents 记录 how to use the library: which procedures it offers, what arguments they take, what they return.
- You do not need to see the library's inside code.
- The API is the contract between you and the library.
阅读 API
- 要用好一个库,你阅读它的 API(应用程序接口)。
- API 记录(documents)如何使用库:它提供哪些过程、它们接收什么实参、返回什么。
- 你不需要看库内部的代码。
- API 是你和库之间的契约。
Which library for the job? · 哪个库适合这个任务?
Pick the library that matches your task: a maths library for numbers, a graphics library for drawing, a random library for random values. · 挑与你的任务匹配的库:数字用数学库,绘图用图形库,随机值用随机库。
A library's API tells you: · 一个库的 API 告诉你:
The API documents usage; you never need the inside code. · API 记录用法;你从不需要内部代码。
A well-chosen library saves time and reduces bugs because its code is already tested. · 一个选得好的库节省时间并减少缺陷,因为它的代码已经测试过。
You reuse tested code instead of writing and debugging your own. · 你重用已测试的代码,而非写并调试你自己的。
Adding functionality fast
- Libraries add functionality 功能 to your program quickly.
- A maths library gives square roots; a graphics library draws shapes; a random library gives random numbers.
- Pick an appropriate library for the part you need.
- A well-chosen library saves time and reduces bugs, because the code is already tested.
快速添加功能
- 库快速给你的程序添加功能(functionality)。
- 一个数学库给平方根;一个图形库画形状;一个随机库给随机数。
- 为你需要的部分挑一个合适的库。
- 一个选得好的库节省时间并减少缺陷,因为代码已经测试过。
Calling sqrt(9) and getting 3 without knowing the method inside is an example of: · 调用 sqrt(9) 得到 3,而不知道内部的方法,是什么的例子:
The library hides its implementation behind a simple call. · 库把它的实现藏在一个简单的调用后面。
To draw shapes on the screen, you would choose a: · 要在屏幕上画形状,你会选一个:
Match the library to the kind of job you need. · 把库与你需要的任务种类匹配。
r ← sqrt(2) stores about 1.414, using a ______ library. · r ← sqrt(2) 存储约 1.414,用一个______库。
The maths library provides the tested square-root procedure. · 数学库提供已测试的平方根过程。
Libraries and abstraction
- Like procedures, libraries support abstraction 抽象 by hiding complex implementation.
- You call
sqrt(9)and get 3 without knowing the method used inside.
Square root of 2. Instead of writing your own algorithm, call a maths library: r ← sqrt(2) stores about 1.414. The API tells you sqrt takes one number and returns its square root. The whole task becomes one line.
库与抽象
- 像过程一样,库通过隐藏复杂的实现支持抽象(abstraction)。
- 你调用
sqrt(9)得到 3,而不知道内部用的方法。
2 的平方根。 与其写你自己的算法,调用一个数学库:r ← sqrt(2) 存储约 1.414。API 告诉你 sqrt 接收一个数字并返回它的平方根。整个任务变成一行。
A library is reusable, tested code, so you don't build everything from scratch. Read its API to learn which procedures it offers and what they return — you never see the inside. Libraries add functionality fast and provide abstraction: sqrt(9) gives 3 without you knowing how.
一个库是可重用、已测试的代码,所以你不用从头开始建一切。阅读它的 API 来了解它提供哪些过程、返回什么——你从不看内部。库快速添加功能并提供抽象:sqrt(9) 给出 3,而你不知道怎么做的。