灯下 登录
计算机科学 / SICP / section 中英对照

3.1 Assignment and Local State

We ordinarily view the world as populated by independent objects, each of which

has a state that changes over time. An object is said to “have state” if its

behavior is influenced by its history. A bank account, for example, has state

in that the answer to the question “Can I withdraw $100?” depends upon the

history of deposit and withdrawal transactions. We can characterize an

object’s state by one or more

state variables, which among them

maintain enough information about history to determine the object’s current

behavior. In a simple banking system, we could characterize the state of an

account by a current balance rather than by remembering the entire history of

account transactions.

我们通常将世界看作是由一个个独立的对象构成的,每个对象都有随时间变化的状态。如果一个对象的行为受其历史的影响,我们就说它"具有状态"。例如,银行账户就具有状态,因为"我能否取出 100 美元?"这一问题的答案取决于存款和取款交易的历史记录。我们可以用一个或多个状态变量 (state variables) 来刻画对象的状态——这些变量共同维护着足够的历史信息,以决定对象当前的行为。在一个简单的银行系统中,我们可以用当前余额来刻画账户的状态,而无需记录全部交易历史。

In a system composed of many objects, the objects are rarely completely

independent. Each may influence the states of others through interactions,

which serve to couple the state variables of one object to those of other

objects. Indeed, the view that a system is composed of separate objects is

most useful when the state variables of the system can be grouped into closely

coupled subsystems that are only loosely coupled to other subsystems.

在一个由许多对象构成的系统中,这些对象很少是完全独立的。每个对象都可能通过相互作用影响其他对象的状态,而这些相互作用将一个对象的状态变量与其他对象的状态变量耦合在一起。事实上,只有当系统的状态变量能够被归入若干紧密耦合的子系统、而这些子系统彼此之间只有松散耦合时,将系统视为独立对象集合的观点才最为有用。

This view of a system can be a powerful framework for organizing computational

models of the system. For such a model to be modular, it should be decomposed

into computational objects that model the actual objects in the system. Each

computational object must have its own

local state variables

describing the actual object’s state. Since the states of objects in the

system being modeled change over time, the state variables of the corresponding

computational objects must also change. If we choose to model the flow of time

in the system by the elapsed time in the computer, then we must have a way to

construct computational objects whose behaviors change as our programs run. In

particular, if we wish to model state variables by ordinary symbolic names in

the programming language, then the language must provide an

assignment operator

to enable us to change the value associated with a name.

这种对系统的看法,可以为构建系统的计算模型提供一个强有力的框架。为使这样的模型具有模块性,应当将其分解为若干计算对象,以对应系统中的实际对象。每个计算对象都必须有自己的局部状态变量 (local state variables),以描述对应实际对象的状态。由于被建模系统中对象的状态随时间变化,相应计算对象的状态变量也必须随之改变。如果我们选择用计算机中流逝的时间来模拟系统中时间的流逝,就必须有办法构造出其行为随程序运行而变化的计算对象。尤其是,如果我们希望在程序设计语言中用普通的符号名称来建模状态变量,那么语言必须提供赋值运算符 (assignment operator),以便我们能够改变与某个名称关联的值。