<<DEITEL - Python How to Program 2002>>
Objectives
目标:
• To understand the software-engineering concepts of “encapsulation” and “data hiding.”
为了理解封装和数据隐藏的软件工程概念
• To understand the notions of data abstraction and abstract data types (ADTs).
为了理解数据抽象和抽象数据类型ADT的概念
• To create Python ADTs, namely classes.
为了建立python ADT,也即“类”
• To understand how to create, use and destroy objects of a class.
为了理解怎样去建立、使用和销毁一个类的诸多对象
• To control access to object attributes and methods.
为了控制对象属性和方法的访问
• To begin to appreciate the value of object orientation.
开始感受面向对象编程的价值
My object all sublime I shall achieve in time.
W. S. Gilbert
Is it a world to hide virtues in?
William Shakespeare, Twelfth Night
Your public servants serve you right.
Adlai Stevenson
Classes struggle, some classes triumph, others are eliminated.
Mao Zedong
This above all: to thine own self be true.
William Shakespeare, Hamlet
Object-oriented programming (OOP) encapsulates (i.e., wraps) data (attributes) and functions (behaviors) into components called classes.
OOP封装(即包裹)了数据(属性)和函数(行为),完事之后成为“class”
The data and functions of a class are intimately tied together. A class is like a blueprint.
一个类中的数据和函数紧紧地拥抱在一起。一个类相当于一个"blueprint”
排比句:
Using a blueprint, a builder can build a house.
Using a class, a programmer can create an object
(also called an instance).
One blueprint can be reused many times to make many houses.
One class can be reused many times to make many objects of the same class.
Classes have a property called information hiding. This means that, although objects may know how to communicate with one another across well-defined interfaces, one object normally should not be allowed to know how another object is implemented—implementation details are hidden within the objects themselves.
Surely it is possible to drive a car effectively without knowing the details of how engines, transmissions and exhaust systems work internally.
We will see why information hiding is crucial to good software engineering.
对比句:
In C and other procedural programming languages, programming tends to be actionoriented; in Python, programming can be object-oriented. In procedural programming, the unit of programming is the function. In object-oriented programming, the unit of programming is the class from which objects eventually are instantiated (i.e., created).
过程式语言tends to be actionoriented,the unit of programming is the function。
面向对象语言tends to be object-oriented,the unit of programming is the class。用这一个个class实例化对象(也就是created)
继续对比:
Procedural programmers concentrate on writing functions. Groups of actions that perform some task are formed into functions, and functions are grouped to form programs.
过程式语言集中精力在函数上。行为组--这个行为组用来执行某项任务are formed into ”函数"。
Data certainly is important in procedural programming, but the view is that data exists primarily in support of the actions that functions perform. The verbs in a system specification—a document that describes the services an application should provide—help the procedural programmer determine the set of functions that will work together to implement
the system.
数据在过程式编程中无疑是重要的,但是关键在你怎么看待数据。这里的数据存在主要是用来支持函数欲表现的行为。
Object-oriented programmers concentrate on creating their own user-defined types,called classes. Classes are also referred to as programmer-defined types. Each class contains data and the set of functions that manipulate the data. The data components of a class are called attributes (or data members). The functional components of a class are called methods (or member functions, in other object-oriented languages). The focus of attention in object-oriented programming is on classes rather than on functions. The nouns in a system specification help the object-oriented programmer determine the set of classes that will be used to create the objects that will work together to implement the system.
过程式语言使用的技术是“动词”,面对对象则是“名词”
Software Engineering Observation 7.1
A central theme of this book is “reuse, reuse, reuse.” We will carefully discuss a number of techniques for “polishing” classes to encourage reuse. We focus on “crafting valuable classes” and creating valuable “software assets.”