?? [7] classes and objects, c++ faq lite.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0057)http://www.sunistudio.com/cppfaq/classes-and-objects.html -->
<HTML><HEAD><TITLE>[7] Classes and objects, C++ FAQ Lite</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-cn>
<META content=classes-and-objects.html name=FILENAME>
<META content="[7] Classes and objects, C++ FAQ Lite" name=ABSTRACT>
<META content=cline@parashift.com name=OWNER>
<META content="Marshall Cline, cline@parashift.com" name=AUTHOR>
<META content="MSHTML 6.00.2462.0" name=GENERATOR>
<META content=FrontPage.Editor.Document name=ProgId><LINK rev=made
href="mailto:cline@parashift.com"><LINK
href="[7] Classes and objects, C++ FAQ Lite.files/cpp-faq.css" type=text/css
rel=stylesheet></HEAD>
<BODY>
<H1><A name=top></A>[7] 類和對象<BR><SMALL><SMALL>(Part of <A
href="http://www.sunistudio.com/cppfaq/index.html"><EM>C++ FAQ Lite</EM></A>, <A
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.2]">Copyright ©
1991-2001</A>, <A href="http://www.parashift.com/" target=OutsideTheFAQ>Marshall
Cline</A>, <A
href="mailto:cline@parashift.com">cline@parashift.com</A>)</SMALL></SMALL></H1>
<P>簡體中文版翻譯:<A href="http://www.sunistudio.com/nicrosoft">申旻</A>,<A
href="mailto:nicrosoft@sunistudio.com">nicrosoft@sunistudio.com</A>(<A
href="http://www.sunistudio.com/">東日制作室</A>,<A
href="http://www.sunistudio.com/asp/sunidoc.asp">東日文檔</A>)</P>
<HR>
<H3>FAQs in section [7]:</H3>
<UL>
<LI><A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.1]">[7.1]
類是什么</A><A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.1]">?</A>
<LI><A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.2]">[7.2]
對象是什么?</A>
<LI><A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.3]">[7.3]
什么樣的接口是“好”的?</A>
<LI><A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.4]">[7.4]
封裝是什么?</A>
<LI><A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.5]">[7.5]
C++是如何在安全性和可用性間取得平衡的?</A>
<LI><A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.6]">[7.6]
</A><A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.6]">我如何才能防止其它程序員查看我的類的私有部分而破壞封裝?</A>
<LI><A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.7]">[7.7]
封裝是一種安全裝置嗎?</A>
<LI><A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.8]">[7.8]
關鍵字 struct 和 class 有什么區別?</A> </LI></UL>
<P>
<HR>
<P><A name=[7.1]></A>
<DIV class=FaqTitle>
<H3>[7.1] 類是什么?</H3></DIV>
<P>面向對象軟件的基本組成物<BR><BR>類定義數據類型,就如同 C 中的結構。從計算機科學的角度來理解,類型由狀態集合和轉換這些狀態的操作集合組成。因為
<TT>int</TT> 既有狀態集合,也有象 <TT>i + j</TT> 或 <TT>i++</TT> 等這樣的操作,所以
<TT>int</TT> 是一種類型。同樣,類提供了一組操作集合(通常是 <TT>public:</TT>
)和一組描述類型實例所擁有的抽象值的數據集合。<BR><BR>可以將 <TT>int</TT> 看作為一個有 operator++
等成員函數的類。(<TT>int</TT> 實際并不是一個類,但是基本類似:一個類是一種類型,就如同 <TT>int</TT>
是一種類型)<BR><BR>注意: C 程序員可以將類看作為成員默認為私有的結構。但是,如果那是你對類的全部認識,那么你可能要經歷個人的典范轉變了。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[7.2]></A>
<DIV class=FaqTitle>
<H3>[7.2] 對象是什么?</H3></DIV>
<P>和語義有關的存儲區域<BR><BR>當我們聲明了<TT>int i</TT>,我們說:“<TT>i</TT> 是 <TT>int
</TT>類型的一個對象”。在 OO/C++ 中,“對象”通常意味著“類的一個實例”。因此,類定義多個對象(實例)的可能的行為。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[7.3]></A>
<DIV class=FaqTitle>
<H3>[7.3] 什么樣的接口是“好”的?</H3></DIV>
<P>提供了一個將“塊”狀的軟件簡化了的視圖,并且以“用戶”的詞匯表達的接口。(“塊”通常是一個或<A
href="http://www.sunistudio.com/cppfaq/friends.html#[14.2]">一組緊密相連的類</A>;“用戶”是指其它的開發者而不是最終客戶)
<P>
<UL>
<LI>“簡化了的視圖”指隱藏不必要的細節。這樣可以減少用戶的錯誤率。
<LI>“用戶的詞匯”指用戶不需要學習新的詞匯或概念,這樣可以降低用戶的學習曲線。 </LI></UL>
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[7.4]></A>
<DIV class=FaqTitle>
<H3>[7.4] 封裝是什么?</H3></DIV>
<P>防止未授權地訪問一些信息和功能。
<P>省錢的關鍵是從軟件“塊”的穩定部分中分離出可變的部分。封裝給這個“塊”安置了防火墻,它可以防止其它“塊”訪問可變的部分;其它“塊”僅僅能夠訪問穩定的部分。這樣做,當可變的部分改變后,可以防止其它“塊”被破壞。在面向對象軟件的概念中,“塊(chunk)”通常指一個或<A
href="http://www.sunistudio.com/cppfaq/friends.html#[14.2]">一組緊密相連的類</A>。
<P>“可變的部分”是實現的細節。如果“塊”是單個類,那么可變的部分通常用 <A
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.5]"><TT>private:</TT>
和/或 <TT>protected:</TT> 關鍵字</A>來封裝。如果“塊”是<A
href="http://www.sunistudio.com/cppfaq/friends.html#[14.2]">一組緊密相連的類</A>,封裝可被用來拒絕對組中全部類的訪問。<A
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html">繼承</A>也能被用來作為<A
href="http://www.sunistudio.com/cppfaq/abcs.html#[22.2]">封裝的一種形式</A>。
<P>“穩定的部分”是接口。好的接口提供了一個<A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.3]">以用戶的詞匯簡化了的視圖</A>,并且被<A
href="http://www.sunistudio.com/cppfaq/operator-overloading.html#[13.10]">從外到里的設計</A>。(此處的“用戶”是指其它開發者,而不是購買完整應用的最終用戶)。如果“塊”是單個類,接口僅僅是類的
public: 成員函數和<A
href="http://www.sunistudio.com/cppfaq/friends.html">友元</A>函數。如果“塊”是<A
href="http://www.sunistudio.com/cppfaq/friends.html#[14.2]">一組緊密相連的類</A>,那么接口可以包括模塊中的多個類。
<P>設計一個清晰的接口并且<A
href="http://www.sunistudio.com/cppfaq/abcs.html#[22.1]">將實現和接口分離</A>,只不過是允許用戶使用接口。而封裝實現可以強迫用戶使用接口。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[7.5]></A>
<DIV class=FaqTitle>
<H3>[7.5] C++是如何在安全性和可用性間取得平衡的?</H3></DIV>
<P>在 C 中,<A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.4]">封裝</A>是通過在編輯單元或模塊中,將對象聲明為靜態來完成的。這樣做防止了其他模塊訪問靜態區域。(順便說一句,現在這種做法是被遺棄的:不要在
C++中這樣做)
<P>不幸的是,由于沒有對一個模塊的靜態數據產生多個實例的直接支持,這種處理方法不支持數據的多個實例。在 C
中如果需要多個實例,那么程序員一般使用結構。但是很不幸,C 的結構不支持<A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.4]">封裝</A>。這增加了在安全性(信息隱藏)和可用性(多實例)之間取得平衡的難度。
<P>在 C++中,你可以利用類來同時獲得多實例和封裝性。類的 public: 部分包含了類的接口,它們通常由類的 public: 成員函數和它的<A
href="http://www.sunistudio.com/cppfaq/friends.html">友元</A>函數組成。類的 <A
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.5]"><TT>private:</TT>
和/或 <TT>protected:</TT></A> 部分包含了類的實現,而通常數據就在這里。
<P>最終的結果就象是“封裝了的結構”。這樣就易于在安全性(信息隱藏)和可用性(多實例)間取得平衡。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[7.6]></A>
<DIV class=FaqTitle>
<H3>[7.6] 我如何才能防止其它程序員查看我的類的私有部分而破壞封裝?</H3></DIV>
<P>不必這么做——封裝是對于代碼而言的,而不是對人。
<P>只要其它程序員寫的代碼不依賴于他們的所見,那么即使它們看了你的類的 <A
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.5]"><TT>private:</TT>
和/或 <TT>proteced:</TT></A>
部分,也不會破壞封裝。換句話說,封裝不會阻止人認識類的內部。封裝只是阻止他們寫出依賴類內部實現的代碼。你的公司不必為維護你眼睛看到的支付維護成本,但是必須為維護你的指尖寫出的代碼支付維護成本。正如你知道的,倘若他們寫的代碼依賴于接口而不是實現,就不會增加維護成本。
<P>此外,這很少成為一個問題。我想不會有故意試圖訪問類的私有部分的程序員。My recommendation in such cases would be
to change the programmer, not the code" [<A
href="mailto:kanze@gabi-soft.fr">James Kanze</A>; used with permission].
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[7.7]></A>
<DIV class=FaqTitle>
<H3>[7.7] 封裝是一種安全裝置嗎?</H3></DIV>
<P>不。
<P>封裝 != 安全。
<P>封裝要防止的是錯誤,而不是間諜。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[7.8]></A>
<DIV class=FaqTitle>
<H3>[7.8] 關鍵字 struct 和 class 有什么區別?</H3></DIV>
<P>struct 的成員默認是公有的,而類的成員默認是私有的。注意:你應該明白地聲明你的類成員為公有的、私有的、或者是保護的,而不是依賴于默認屬性
<P>struct 和 class 在其他方面是功能相當的。
<P>好,清晰的技術談論夠多了。從感情上講,大多數的開發者感到類和結構有很大的差別。感覺上結構僅僅象一堆缺乏封裝和功能的開放的內存位,而類就象活的并且可靠的社會成員,它有智能服務,有牢固的封裝屏障和一個良好定義的接口。既然大多數人都這么認為,那么只有在你的類有很少的方法并且有公有數據(這種事情在良好設計的系統中是存在的!)時,你也許應該使用
struct 關鍵字,否則,你應該使用 class 關鍵字。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Next section</A>
]</SMALL>
<HR>
<P><A name=bottom></A><A href="mailto:cline@parashift.com"><IMG height=26
alt=E-Mail src="[7] Classes and objects, C++ FAQ Lite.files/mbox.gif"
width=89> E-mail the author</A><BR>[ <A
href="http://www.sunistudio.com/cppfaq/index.html"><EM>C++ FAQ Lite</EM></A>
| <A
href="http://www.sunistudio.com/cppfaq/index.html#table-of-contents">Table of contents</A>
| <A
href="http://www.sunistudio.com/cppfaq/subject-index.html">Subject index</A>
| <A
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.1]">About the author</A>
| <A
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.2]">©</A>
| <A
href="http://www.sunistudio.com/cppfaq/on-line-availability.html#[2.2]">Download your own copy</A> ]<BR><SMALL>Revised
Apr 8, 2001</SMALL> </P></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -