物件導向思想 – 關於對象(材料來自thinking in Java,這些東西是跟C#相通的,跟語言無關)

來源:互聯網
上載者:User

由於基於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#中的介面,而是廣義的介面,在這裡它其實是指方法,具體內容請見原書,我就偷懶不寫了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.