Skip to content

Commit 380c0ba

Browse files
committed
对象清理
1 parent eb1d959 commit 380c0ba

File tree

2 files changed

+49
-58
lines changed

2 files changed

+49
-58
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* [对象创建](book/03-Objects-Everywhere.md#对象创建)
4747
* [代码注释](book/03-Objects-Everywhere.md#代码注释)
4848
* [对象清理](book/03-Objects-Everywhere.md#对象清理)
49+
* [创建新类](book/03-Objects-Everywhere.md#创建新类)
4950
* [程序编写](book/03-Objects-Everywhere.md#程序编写)
5051
* [小试牛刀](book/03-Objects-Everywhere.md#小试牛刀)
5152
* [编码风格](book/03-Objects-Everywhere.md#编码风格)

book/03-Objects-Everywhere.md

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -149,73 +149,63 @@ continues across lines */
149149
<!-- You Never Need to Destroy an Object -->
150150
## 对象清理
151151

152-
In some programming languages, managing storage lifetime requires
153-
significant effort. How long does a variable last? If you are supposed to
154-
destroy it, when should you? Confusion over storage lifetime can lead
155-
to many bugs, and this section shows how Java simplifies the issue by
156-
releasing storage for you.
157-
Scoping
158-
Most procedural languages have the concept of scope. This determines
159-
both the visibility and lifetime of the names defined within that scope.
160-
In C, C++, and Java, scope is determined by the placement of curly
161-
braces {}. Here is a fragment of Java code demonstrating scope:
152+
在一些编程语言中,管理存储的生命周期需要大量的工作。一个变量需要存续多久?如果我们想销毁它,应该什么时候去做呢?存储生命周期的混乱会导致许多错误,本小结将会向你介绍 Java 是如何通过释放存储来简化这个问题的。
153+
154+
<!-- Scoping -->
155+
### 作用域
156+
157+
大多数程序语言都有作用域的概念。这将确定在该范围内定义的名称的可见性和生存周期。在 C、C++ 和 Java 中,作用域是由大括号 `{}` 的位置决定的。下面是 Java 代码作用域的一个示例:
158+
159+
```JAVA
162160
{
163-
int x = 12;
164-
// Only x available
161+
int x = 12;
162+
// x 变量可用
165163
{
166-
int q = 96;
167-
// Both x & q available
164+
int q = 96;
165+
// x 和 q 变量皆可用
168166
}
169-
// Only x available
170-
// q is "out of scope"
167+
// x 变量可用
168+
// 变量 q 不在作用域内
171169
}
172-
A variable defined within a scope is available only until the end of that
173-
scope.
174-
Indentation makes Java code easier to read. Since Java is a free-form
175-
language, the extra spaces, tabs, and carriage returns do not affect the
176-
resulting program.
177-
You cannot do the following, even though it is legal in C and C++:
170+
```
171+
172+
Java 的变量只有在其作用域内才可用。缩进使得 Java 代码更易于阅读。由于 Java 是一种自由形式的语言,额外的空格、制表符和回车并不会影响程序的生成结果。在 Java 中,你不能执行以下操作,即使这在 C 和 C++ 中是合法的:
173+
174+
```JAVA
178175
{
179-
int x = 12;
176+
int x = 12;
180177
{
181-
int x = 96; // Illegal
178+
int x = 96; // Illegal
182179
}
183180
}
184-
The Java compiler will announce that the variable x has already been
185-
defined. Thus the C and C++ ability to “hide” a variable in a larger
186-
scope is not allowed, because the Java designers thought it led to
187-
confusing programs.
188-
Scope of Objects
189-
Java objects do not have the same lifetimes as primitives. When you
190-
create a Java object using new, it persists past the end of the scope.
191-
Thus, if you say:
181+
```
182+
183+
在上例中, Java 便编译器会在提示变量 x 已经被定义过了。因此,在 C 和 C++ 中可以于更大作用域中“隐藏”变量的能力在 Java 中是不被允许的。 因为 Java 的设计者认为这样的定义会混淆编程。
184+
185+
<!-- Scope of Objects -->
186+
### 对象作用域
187+
188+
Java 对象与基本类型具有不同的生命周期。当我们使用 `new` 关键字来创建 Java 对象时,它的存续将会超出作用域的末尾。因此,下面这段代码示例
189+
190+
```JAVA
192191
{
193-
String s = new String("a string");
194-
} // End of scope
195-
the reference s vanishes at the end of the scope. However, the
196-
String object that s points to is still occupying memory. In this bit
197-
of code, there is no way to access the object after the end of the scope,
198-
because the only reference to it is out of scope. In later chapters you’ll
199-
see how the reference to the object can be passed around and
200-
duplicated during the course of a program.
201-
Because objects created with new exist as long as you need them, a
202-
whole slew of C++ programming problems vanish in Java. In C++ you
203-
must not only make sure that the objects stay around as long as
204-
necessary, you must also destroy the objects when you’re done with
205-
them.
206-
This brings up a question. If Java leaves the objects lying around, what
207-
keeps them from filling up memory and halting your program, which
208-
is exactly the kind of problem that occurs in C++? In Java, a bit of
209-
magic happens: the garbage collector looks at all the objects created
210-
with new and finds those that are no longer referenced. It then
211-
releases the memory for those objects, so the memory can be used for
212-
new objects. This means you don’t worry about reclaiming memory
213-
yourself. You simply create objects, and when you no longer need
214-
them, they go away by themselves. This prevents an important class of
215-
programming problem: the so-called “memory leak,” when a
216-
programmer forgets to release memory.
217-
Creating New Data
218-
Types: class
192+
String s = new String("a string");
193+
}
194+
// 作用域结束
195+
```
196+
上例中,变量 s 的范围在标注的地方结束了。但是,引用的字符串对象依然还在占用内存。在这段代码中,变量 s 的唯一引用超出了作用域,因此它无法在作用域外被访问。在后面的章节中,你将学习到如何在编程过程中传递和复制对象的引用。
197+
198+
只要你还需要它们,这些用 `new` 关键字创建出来的对象就不会消失。因此,这个在 C++ 编程中大量存在的问题在 Java 中消失了。在 C++ 中你不仅要确保对象在你操作的范围内存在,而且还必须在完成后销毁对象。
199+
200+
随之而来的问题:如果 Java 并没有清理周围的这些对象,那么它是如何避免 C++ 中出现的内存泄漏和程序终止的问题呢?答案是:Java 的垃圾收集器会查找所有 `new` 出来的对象并判断哪些不再可达。然后释放那些对象所占用的内存。这意味着我们不必再需要自己操作回收内存了。我们只需要简单的创建对象,当这些对象不再被需要时,它们能自行被垃圾收集器释放。
201+
。这意味着您不必担心回收内存你自己。您只需创建对象,当您不再需要时他们,他们自己消失。这可以防止重要的一类编程问题:所谓的“内存泄漏”时程序员忘记释放内存。
202+
203+
这就有效地防止了一类重要的编程问题:因程序员忘记释放内存而造成的“内存泄漏”。
204+
205+
<!-- Creating New Data Types: class -->
206+
## 创建新类
207+
208+
219209
If everything is an object, what determines how a particular class of
220210
object looks and behaves? Put another way, what establishes the type
221211
of an object? You might expect a keyword called “type,” and that would

0 commit comments

Comments
 (0)