




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、計算機專業(yè)外文翻譯畢業(yè)設計英文翻譯作者: XXXThe Delphi Programming LanguageThe Delphi development environment is based on an object-oriented extension of thePascal programming language known as Object Pascal. Most modern programming languagessupport object-oriented programming (OOP). OOP languages are based on three fu
2、ndamentalconcepts: encapsulation (usually implemented with classes), inheritance, and polymorphism(or late binding).1Classes and ObjectsDelphi is based on OOP concepts, and in particular on the definition of new class types.The use of OOP is partially enforced by the visual development environment,
3、because forevery new form defined at design time, Delphi automatically definesa new class. In addition,every component visually placed on a form is an object of a classtype available in or added tothe system library.As in most other modern OOP languages (including Java and C#), inDelphi a class-type
4、variable doesn't provide the storage for the object, but is only apointer or reference to theobject in memory. Before you use the object, you must allocatememory for it by creating anew instance or by assigning an existing instance to the variable:varObj1, Obj2: TMyClass;begin / assign a newly c
5、reated objectObj1 := TMyClass.Create; / assign to an existing objectObj2 := ExistingObject;The call to Create invokes a default constructor available for everyclass, unless the classredefines it (as described later). To declare a new class data typein Delphi, with some localdata fields and some meth
6、ods, use the following syntax:typeTDate = classMonth, Day, Year: Integer;procedure SetValue (m, d, y: Integer);function LeapYear: Boolean;第 1 頁 共 13 頁畢業(yè)設計英文翻譯作者: XXXend;2Creating Components DynamicallyTo emphasize the fact that Delphi components aren't much different from other objects(and also
7、to demonstrate the use of the Self keyword), I've written the CreateComps example.This program has a form with no components and a handler for itsOnMouseDown event,which I've chosen because it receives as a parameter the position of the mouse click (unlikethe OnClick event). I need this info
8、rmation to create a button component in that position. Hereis the method's code:procedure TForm1.FormMouseDown (Sender: TObject;Button: TMouseButton; Shift: TShiftState; X, Y: Integer);Var Btn: TButton;beginBtn := TButton.Create (Self);Btn.Parent := Self;Btn.Left := X;Btn.Top := Y;Btn.Width := B
9、tn.Width + 50;Btn.Caption := Format ('Button at %d, %d', X, Y);end;The effect of this code is to create buttons at mouse-click positions, as you can see in thefollowing figure. In the code, notice in particular the use of theSelf keyword as both theparameter of the Create method (to specify
10、the component's owner) and the value of theParent property.(The output of theexample,which creates Button components at run time第2頁共13頁畢業(yè)設計英文翻譯作者:XXX3EncapsulationThe concept of encapsulation is often indicated by the idea of ablack box." You don'tknow about the internals: You only know
11、 how to interface with the black box or use itregardless of its internal structure. The "how to use" portion,called the class interface, allowsother parts of a program to access and use the objects of that class.However, when you usethe objects, most of their code is hidden. You seldom kno
12、w what internal data the object has,and you usually have no way to access the data directly. Of course,you are supposed to usemethods to access the data, which is shielded from unauthorizedaccess. This is theobject-oriented approach to a classical programming concept known as information hiding.Howe
13、ver, in Delphi there is the extra level of hiding, throughproperties,4PrivateProtectedand PublicFor class-based encapsulation, the Delphi language has three access specifiers: private,protected, and public. A fourth, published, controls run-time typeinformation (RTTI) anddesign-time information, but
14、 it gives the same programmaticaccessibility as public. Here arethe three classic access specifiers:, The private directive denotes fields and methods of a class thatare not accessible outsidethe unit that declares the class., The protected directive is used to indicate methods and fieldswith limite
15、d visibility. Onlythe current class and its inherited classes can access protected elements. More precisely,only the class, subclasses, and any code in the same unit as the class can access protectedmembers。, The public directive denotes fields and methods that are freely accessible from any otherpo
16、rtion of a program as well as in the unit in which they are defined.Generally, the fields of a class should be private and the methods public. However, thisis not always the case. Methods can be private or protected if they are needed only internallyto perform some partial computation or to implemen
17、t properties.Fields might be declared asprotected so that you can manipulate them in inherited classes, although this isn't considered agood OOP practice.5Encapsulating with Properties第 3 頁 共 13 頁畢業(yè)設計英文翻譯作者: XXXProperties are a very sound OOP mechanism, or a well-thought-out application of theid
18、ea of encapsulation. Essentially, you have a name that completely hides its implementationdetails. This allows you to modify the class extensively withoutaffecting the code using it. Agood definition of properties is that of virtual fields. From the perspective of the user of the class that defines
19、them, properties look exactly like fields, because you can generally read orwrite their value. For example, you can read the value of theCaption property of a button andassign it to the Text property of an edit box with the followingcode:Edit1.Text:= Button1.Caption;It looks like you are reading and
20、 writing fields. However,properties can be directlymapped to data, as well as to access methods, for reading andwriting the value. Whenproperties are mapped to methods, the data they access can be part of the object or outside ofit, and they can produce side effects, such as repainting a control aft
21、er you change one of itsvalues. Technically, a property is an identifier that is mapped todata or methods using a readand a write clause. For example, here is the definition of a Month property for a date class:property Month: Integer read FMonth write SetMonth;To access the value of the Month prope
22、rty, the program reads thevalue of the privatefield FMonth; to change the property value, it calls the methodSetMonth (which must bedefined inside the class, of course).Different combinations are possible (for example, you could also use a method to readthe value or directly change a field in the wr
23、ite directive), butthe use of a method to changethe value of a property is common. Here are two alternativedefinitions for the property,mapped to two access methods or mapped directly to data in both directions:property Month: Integer read GetMonth write SetMonth;property Month: Integer read FMonth
24、write FMonth;Often, the actual data and access methods are private (or protected), whereas the propertyis public. For this reason, you must use the property to have access to those methods or data, atechnique that provides both an extended and a simplified version of encapsulation. It is anextended
25、encapsulation because not only can you change the representation of the data and itsaccess functions, but you can also add or remove access functions without changing thecalling code. A user only needs to recompile the program using the property.第 4 頁 共 13 頁畢業(yè)設計英文翻譯作者: XXX6Properties for the TDate C
26、lassAs an example, I've added properties for accessing the year, the month, and the day to anobject of the TDate class discussed earlier. These properties are not mapped to specific fields,but they all map to the single fDate field storing the complete date information. This is whyall the proper
27、ties have both getter and setter methods:typeTDate = classpublicproperty Year: Integer read GetYear write SetYear;property Month: Integer read GetMonth write SetMonth;property Day: Integer read GetDay write SetDay;Each of these methods is easily implemented using functions available in the DateUtils
28、unit. Here is the code for two of them (the others are very similar):function TDate.GetYear: Integer;beginResult := YearOf (fDate);end;procedure TDate.SetYear(const Value: Integer);beginfDate := RecodeYear (fDate, Value);end;7Advanced Features of PropertiesProperties have several advanced features I
29、 Here is a short summary of these moreadvanced features:, The write directive of a property can be omitted, making it a read-only property. Thecompiler will issue an error if you try to change the property value.You can also omit theread directive and define a write-only property, but that approach
30、doesn't make much sense and is used infrequently.第 5 頁 共 13 頁畢業(yè)設計英文翻譯作者: XXX, The Delphi IDE gives special treatment to design-time properties, which are declared with the published access specifier and generally displayed in the Object Inspector for theselected component., An alternative is to
31、declare properties, often called run-time only properties, with the public access specifier. These properties can be used in program code., You can define array-based properties, which use the typicalnotation with squarebrackets to access an element of a list. The string list- basedproperties, such
32、as the Lines of a list box, are a typical example of this group., Properties have special directives, including stored and defaults, which control thecomponent streaming system.8Encapsulation and FormsOne of the key ideas of encapsulation is to reduce the number of global variables used bya program.
33、 A global variable can be accessed from every portion of a program. For this reason,a change in a global variable affects the whole program. On the other hand, when you changethe representation of a class's field, you only need to change the code of some methods of thatclass and nothing else. Th
34、erefore, we can say that informationhiding refers to encapsulatingchanges.Let me clarify this idea with an example. When you have a program with multiple forms,you can make some data available to every form by declaring it as a global variable in theinterface portion of the unit of one of the forms:
35、varForm1: TForm1;nClicks: Integer;This approach works, but the data is connected to the entire program rather than aspecific instance of the form. If you create two forms of the same type, they'll share the data.If you want every form of the same type to have its own copy of the data, the only s
36、olution isto add it to the form class:typeTForm1 = class(TForm)第 6 頁 共 13 頁畢業(yè)設計英文翻譯作者: XXXpublicnClicks: Integer;end;9Adding Properties to FormsThe previous class uses public data, so for the sake of encapsulation, you should insteadchange it to use private data and data-access functions. An evenbet
37、ter solution is to add aproperty to the form. Every time you want to make some information of a form available toother forms, you should use a property, for all the reasonsdiscussed in the section"Encapsulating with Properties." To do so, change the field declaration of the form (in the pr
38、evious code) by adding the keyword property in front of it, and then press Ctrl+Shift+C toactivate code completion. Delphi will automatically generate all the extra code you perties should also be used in the form classes to encapsulate the access to thecomponents of a form. For example, if
39、you have a main form with a status bar used to displaysome information (and with the SimplePanel property set to True) and you want to modify thetext from a secondary form, you might be tempted to writeForm1.StatusBar1.SimpleText := 'new text'This is a standard practice in Delphi, but it'
40、;s not a good one, because it doesn't provideany encapsulation of the form structure or components. If you havesimilar code in manyplaces throughout an application, and you later decide to modify theuser interface of the form(for example, replacing StatusBar with another control or activatingmul
41、tiple panels), you'llhave to fix the code in many places. The alternative is to use amethod or, even better, aproperty to hide the specific control. This property can be definedasproperty StatusText: string read GetText write SetText;with GetText and SetText methods that read from and write to t
42、heSimpleText propertyof the status bar (or the caption of one of its panels). In theprograms other forms, you canrefer to the form's StatusText property; and if the user interfacechanges, only the setter andgetter methods of the property are affected.第 7 頁 共 13 頁畢業(yè)設計英文翻譯作者: XXXDelphi 開發(fā)環(huán)境是基于Pasc
43、al 編程語言Object Pascal 的面向?qū)ο蟮囊环N擴展。大多數(shù)現(xiàn)代編程語言都支持面向?qū)ο缶幊? OPP。OPP®言基于三個基本概念:封裝(通常通過類實現(xiàn))繼承、多態(tài)。1、 類與對象Delphi是基于OPP既念的,特別是在新類類型中。OPP的使用在可視開發(fā)環(huán)境中得到了增強,這是因為,對于每個在設計時定義的一個新窗體來說,Delphi 會自動定義一個新的類。另外,每個可視放置在一個窗體中的組件實際上是一個類型對象,而且該類可以在系統(tǒng)庫中獲得,或者被添加到系統(tǒng)庫中。就像大多數(shù)開創(chuàng)現(xiàn)代 OPP®言(包括Java和C#)中一卞在Delphi中,一個類的類型變量為對象提供存儲,
44、而只是在內(nèi)存中為對象提供一個指針或引用。在使用對象之前,必須創(chuàng)建一個新的實例或?qū)⒁粋€現(xiàn)有的實例分配給變量,以此來為其分配內(nèi)存:varObj1, Obj2: TMyClass;begin / assign a newly created objectObj1 := TMyClass.Create; / assign to an existing objectObj2 := ExistingObject;對 Create 的調(diào)用會激活一個默認的構造器,該構造器對每個類都有效,除非某個類重新定義了它。為了在Delphi 中聲明一個新的帶有一些本地數(shù)據(jù)字段的和一些方法的類數(shù)據(jù)類型,可使用下面的語法:t
45、ypeTDate = classMonth, Day, Year: Integer;procedure SetValue (m, d, y: Integer);function LeapYear: Boolean;end;動態(tài)的創(chuàng)建組件第 8 頁 共 13 頁畢業(yè)設計英文翻譯作者: XXX為了強調(diào)Delphi 組件與其他對象沒有太多的區(qū)別的事實(同時說明Self 關鍵字日使用)的使用方法,筆者編寫了一個 CreateComps范例。這個程序有一個不帶 組件的窗體和一個用語其OnMouseDow事件的處理器,之所以選擇它是以為它將鼠 標單機的位置作為一個參數(shù)接收(與OnClick 事件不同)。我
46、們需要這個信息,以便在那個位置創(chuàng)建有個按鈕組件。下面是該方法的代碼:procedure TForm1.FormMouseDown (Sender: TObject;Button: TMouseButton; Shift: TShiftState; X, Y: Integer); varBtn: TButton;beginBtn := TButton.Create (Self);Btn.Parent := Self;Btn.Left := X;Btn.Top := Y;Btn.Width := Btn.Width + 50;Btn.Caption := Format ('Button a
47、t %d, %d', X, Y);end;這段代碼的作用是在鼠標單擊的位置創(chuàng)建按鈕,正如在下圖中看到的那樣。在該代碼中,特別要注意Self關鍵字的使用??同時作為Create方法的參數(shù)(以指定 組件的所有者)和Parent屬性的值。CreateCompsr的示例輸出,用于在運行時創(chuàng)建按鈕組件封裝的概念簡單,可以把其想象為一個“黑盒子”。人們并不知道其內(nèi)部什么:只能知道牌位與黑例子接口,或使用它而其內(nèi)部結(jié)構?!霸鯓邮褂谩边@部分,稱 為類的接口,它允許程序的其他部分訪問和使用該類的對象。然而,使用對象時,對象 的大部分第9頁共13頁畢業(yè)設計英文翻譯作者:XXX代碼都是隱含的。用戶很少能知道
48、對象胡哪些內(nèi)部數(shù)據(jù),而且一般沒有辦法可以直接訪問數(shù)據(jù)。可以使用對象方法來訪問數(shù)據(jù),這與非法的訪問不同。相對于信息隱含這樣一個經(jīng)典的編程概念來說,這就是面向?qū)ο蟮姆椒?。然而,然而Delphi 中,通過屬性會有其他等級的。對基于類的封裝來說,Delphi 使用了三個訪問標識符:PrivateProtected 和Public第四個地Published ,控制著運行時的類型信息(RTTI)和設計時信息,但是它提供了和Public 相同的程序訪問性。這里是三個經(jīng)典的訪問標識符。a) Private 指令專門用于一個類字段和對象方法在聲明類的單元外的類不能被訪問。b) Protected 指令用于表示對
49、象方法和字段具有有限的可見性。Protected 元素只能被當前類和它的子類訪問。更精確地說,只有同一個單元中的類、子類和任何代碼可以訪問 protected 成員。c) Public 指令專門用于表示那些可以被程序代碼中的任意部分高談闊論的數(shù)據(jù)和對象方法,當然也包括在定義它們的單元。一般情況下,一個類的字段應該是專用的,而對象方法則應該是公用的。然而如果某對象方法只需要在內(nèi)部完成一些部分或?qū)崿F(xiàn)屬性,那么該方法也可以是專用或受難保護的。字段可以被聲明為受難,這樣就可以在子類中對它們進行處理,盡管這并不是一個好的OPP亍為。屬性是一種非常能夠有效的OOFM制,或者說非常適合實現(xiàn)封裝的思想。從本質(zhì)
50、上講,書信就是用一個名稱來完全隱藏它的實現(xiàn)細節(jié),這使得程序員可以任意修改類,而還會影響使用它的代碼。關于什么是屬性,有一個較好的定義,亦即屬性就是字段。從定義它們的類的用戶角度來看,屬性完全就像字段一樣,因為擁護可以讀取或編寫它們的值。例如,可以用以下代碼讀取一個按鈕的Caption 屬性值,并將其賦給一個帶有下寫代碼的編輯筐的text 屬性:Edit1.Text:=Button1.Caption;這看起來就像讀寫字段一樣。然而,屬性可以直接與數(shù)據(jù)以及訪問對象的方法對應起來,用于讀寫數(shù)值。當屬性對象方法對應時,訪問的數(shù)據(jù)可以是對象或其外部和某部第 10 頁 共 13 頁畢業(yè)設計英文翻譯 作者:
51、XXX分內(nèi)容,而且它們可以產(chǎn)生附加影響,如在改變了一個屬性值后,提親繪制一個控件。從技術上講,一個屬性就是對應數(shù)據(jù)或?qū)ο蠓椒ǎㄊ褂靡粋€read 或一個子句)的標識符。例如,下面是一個日期類的Month屬性的定義:Property month: Integer read FMonth write SetMonth;為了訪問Month屬性的值,該程序語句要讀取專用字段 FMonth的值,同時為其改變該屬性值,它調(diào)用了對象方法SetMonth ( 當然,必須在類的內(nèi)部定義它)不同的組合都是可能的(例如,還可以使用一個對象方法來讀取屬性值或直接修改write 指令中的一個字段),但使用對象方法修改一個
52、屬性的值是很覺的。下面是屬性的兩種可替換定義,它們分別對應兩個訪問方法或在兩個方向上直接對應著數(shù)據(jù):property Month: Integer read GetMonth write SetMonth;property Month: Integer read FMonth write FMonth;通常,屬性是公共的。而實際數(shù)據(jù)與訪問對象方法是專用的(或受保護的),這意味著必須使用屬性訪問那些對象方法或數(shù)據(jù),這種技術提供了封裝的擴展簡化版本。它是一個擴展的封裝,不但可以改變數(shù)據(jù)的表示方法與訪問函數(shù),而且還可以添加與刪除訪問函數(shù)。而還會影響到調(diào)用代碼。一個用戶只重新編譯使用屬性的程序即可。 6TDate作為一個范例,我向前面討論過的TDate 類的一個對象中添加用于訪問年、月、日的屬性。這些屬性參與特定的字段對應著存儲完整日期信息的單個TDate字段。這就是所有屬性都有getter 和 setter 方法的原因:typeTDate = classpublicproperty Year: Integer read GetYear write SetYear;pro
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 班會春節(jié)文化課件
- 廣東省建筑施工企業(yè)安全生產(chǎn)管理人員安全生產(chǎn)(安全管理)測試題及答案(附解析)
- 2025年2月新能源汽車習題庫及參考答案解析
- 玩具行業(yè)人才激勵計劃考核試卷
- 初中語文教學設計與指導
- 稀土金屬礦選礦廠生產(chǎn)自動化與信息集成考核試卷
- 眼科疾病診療與視力保護考核試卷
- 曼漢教育費用分析
- 發(fā)動機結(jié)構與維修技術考核試卷
- 肉類產(chǎn)品陳列與貨架管理技巧考核試卷
- 標準化基礎知識培訓課件
- 第4章我們生活的大地知識點清單-2024-2025學年浙教版七年級下冊科學
- 軍事通信基礎知識
- 建筑工地挖掘機吊裝施工方案
- 8.2 法治政府 課件-高中政治統(tǒng)編版必修三政治與法治
- 糖尿病合并痛風
- 中西文化鑒賞知到智慧樹章節(jié)測試課后答案2024年秋鄭州大學
- 《天津市新型職業(yè)農(nóng)民培育問題研究》
- 車險理賠重大案管理辦法
- 牙科市場細分領域分析-洞察分析
- 第16課《經(jīng)濟危機與資本主義國家的應對》中職高一下學期高教版(2023)世界歷史全一冊
評論
0/150
提交評論