小新的技术天地

Make It Works !

博客园 首页 新随笔 联系 订阅 管理

由于基于C#的面向对象思想方法的好书还比较少,所以开始看《Thinking in Java 3rd》,了解一些面向对象的思想方法,并记下一些精彩的段落(语句),第一次是关于类 - 对象的一些思考(当然是原书中的思考)
中文译文是网上的一个网友翻译的,就我现在看到的部分来看,翻译得挺好的,我就直接看他的中文版了,有疑惑的地方再看原文

一、面向对象的五个特征:

1.Everything is an object. Think of an object as a fancy variable; it stores data, but you can “make requests” to that object, asking it to perform operations on itself. In theory, you can take any conceptual component in the problem you’re trying to solve (dogs, buildings, services, etc.) and represent it as an object in your program.

万物皆对象。将对象想象成一种特殊的变量;它储存数据,而且还可以让你提要求,命令它进行某些操作。从理论上讲,你可以把待解决的问题中的概念性组建(狗,建筑,服务等)都表示成程序里的对象。

2.A program is a bunch of objects telling each other what to do by sending messages. To make a request of an object, you “send a message” to that object. More concretely, you can think of a message as a request to call a method that belongs to a particular object.

程序就是一组相互之间传递消息,告诉对方该干些什么的对象。你只要向那个对象发一个消息,就能向它提出请求。更确切的说,你可以这样认为,消息是调用专属某个对象的方法的请求。

3.Each object has its own memory made up of other objects. Put another way, you create a new kind of object by making a package containing existing objects. Thus, you can build complexity into a program while hiding it behind the simplicity of objects.

每个对象都利用别的对象来组建它自己的记忆。换言之,你通过将已有的对象达成一个包,来创建新的对象。由此,你可以将程序的复杂性,隐藏在对象的简单性之下。

4.Every object has a type. Using the parlance, each object is an instance of a class, in which “class” is synonymous with “type.” The most important distinguishing characteristic of a class is “What messages can you send to it?”

对象都有类型。说这句话的意思是,任何对象都是某个类的实例(an instance of a class)。而这里的类(class)都是类型(type)的意思。用于区分类的最突出的特定就是“你能传给它什么消息?”

5.All objects of a particular type can receive the same messages. This is actually a loaded statement, as you will see later. Because an object of type “circle” is also an object of type “shape,” a circle is guaranteed to accept shape messages. This means you can write code that talks to shapes and automatically handle anything that fits the description of a shape. This substitutability is one of the powerful concepts in OOP.


所有属于同一类型的对象都能接受相同的消息。以后你就会知道这是定义而不是特点。一个“circle”型的对象也是“shape”型的对象,所以保证circle能接受shape的消息。也就是说,你写给shape的代码能自动交给由任何符合shape描述的东西处理。这种互换性(substitutability)是OOP最强大的功能之一。

 更为简洁的定义是:
对象有状态,行为和标识。

这就是说,对象可以由内部数据(这给了它状态),有了方法(因而产生了行为),以及每个对象都能与其他对象区分开了--具体而言,每个对象在内存里都有唯一的地址。

二、对象有接口


三、对象会提供服务
While you’re trying to develop or understand a program design, one of the best ways to think about objects is as “service providers.”

当你开发一个程序或者分析一个程序的设计时,理解对象的最佳方式是把它们当作“服务的提供者”。

In a good object-oriented design, each object does one thing well, but doesn’t try to do too much.

在一个良好设计的面向对象的设计中,每个对象都应该只作一件事,并且作好一件事,而不是去作太多的事情。

四、隐藏实现

So the first reason for access control is to keep client programmers’ hands off portions they shouldn’t touch—parts that are necessary for the internal operation of the data type but not part of the interface that users need in order to solve their particular problems.

所以要控制访问权的首要原因就是,禁止那些客户成功员去碰他们不该动的东西 - 就是那些数据类型内部运作所必需的东西。只允许他们接触解决问题所必需的接口。

The second reason for access control is to allow the library designer to change the internal workings of the class without worrying about how it will affect the client programmer.

第二个原因是允许类库设计人员能在不打搅客户程序员的情况下修改类的内部工作方式。

(这点在语言中其实就是有访问修饰符实现的,比如说public,private等)

未完待续...

注:第二点中提到的接口不是指C#中的接口,而是广义的接口,在这里它其实是指方法,具体内容请见原书,我就偷懒不写了。


posted on 2004-11-20 14:29  小新0574  阅读(2272)  评论(8编辑  收藏  举报