計(jì)算機(jī)專(zhuān)業(yè)外文翻譯_第1頁(yè)
計(jì)算機(jī)專(zhuān)業(yè)外文翻譯_第2頁(yè)
計(jì)算機(jī)專(zhuān)業(yè)外文翻譯_第3頁(yè)
計(jì)算機(jī)專(zhuān)業(yè)外文翻譯_第4頁(yè)
計(jì)算機(jī)專(zhuān)業(yè)外文翻譯_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余20頁(yè)可下載查看

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、計(jì)算機(jī)專(zhuān)業(yè)外文翻譯畢業(yè)設(shè)計(jì)英文翻譯作者: 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 頁(yè) 共 13 頁(yè)畢業(yè)設(shè)計(jì)英文翻譯作者: 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頁(yè)共13頁(yè)畢業(yè)設(shè)計(jì)英文翻譯作者: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 頁(yè) 共 13 頁(yè)畢業(yè)設(shè)計(jì)英文翻譯作者: 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 頁(yè) 共 13 頁(yè)畢業(yè)設(shè)計(jì)英文翻譯作者: 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 頁(yè) 共 13 頁(yè)畢業(yè)設(shè)計(jì)英文翻譯作者: 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 頁(yè) 共 13 頁(yè)畢業(yè)設(shè)計(jì)英文翻譯作者: 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 頁(yè) 共 13 頁(yè)畢業(yè)設(shè)計(jì)英文翻譯作者: XXXDelphi 開(kāi)發(fā)環(huán)境是基于Pasc

43、al 編程語(yǔ)言O(shè)bject Pascal 的面向?qū)ο蟮囊环N擴(kuò)展。大多數(shù)現(xiàn)代編程語(yǔ)言都支持面向?qū)ο缶幊? OPP。OPP®言基于三個(gè)基本概念:封裝(通常通過(guò)類(lèi)實(shí)現(xiàn))繼承、多態(tài)。1、 類(lèi)與對(duì)象Delphi是基于OPP既念的,特別是在新類(lèi)類(lèi)型中。OPP的使用在可視開(kāi)發(fā)環(huán)境中得到了增強(qiáng),這是因?yàn)?,?duì)于每個(gè)在設(shè)計(jì)時(shí)定義的一個(gè)新窗體來(lái)說(shuō),Delphi 會(huì)自動(dòng)定義一個(gè)新的類(lèi)。另外,每個(gè)可視放置在一個(gè)窗體中的組件實(shí)際上是一個(gè)類(lèi)型對(duì)象,而且該類(lèi)可以在系統(tǒng)庫(kù)中獲得,或者被添加到系統(tǒng)庫(kù)中。就像大多數(shù)開(kāi)創(chuàng)現(xiàn)代 OPP®言(包括Java和C#)中一卞在Delphi中,一個(gè)類(lèi)的類(lèi)型變量為對(duì)象提供存儲(chǔ),

44、而只是在內(nèi)存中為對(duì)象提供一個(gè)指針或引用。在使用對(duì)象之前,必須創(chuàng)建一個(gè)新的實(shí)例或?qū)⒁粋€(gè)現(xiàn)有的實(shí)例分配給變量,以此來(lái)為其分配內(nèi)存:varObj1, Obj2: TMyClass;begin / assign a newly created objectObj1 := TMyClass.Create; / assign to an existing objectObj2 := ExistingObject;對(duì) Create 的調(diào)用會(huì)激活一個(gè)默認(rèn)的構(gòu)造器,該構(gòu)造器對(duì)每個(gè)類(lèi)都有效,除非某個(gè)類(lèi)重新定義了它。為了在Delphi 中聲明一個(gè)新的帶有一些本地?cái)?shù)據(jù)字段的和一些方法的類(lèi)數(shù)據(jù)類(lèi)型,可使用下面的語(yǔ)法:t

45、ypeTDate = classMonth, Day, Year: Integer;procedure SetValue (m, d, y: Integer);function LeapYear: Boolean;end;動(dòng)態(tài)的創(chuàng)建組件第 8 頁(yè) 共 13 頁(yè)畢業(yè)設(shè)計(jì)英文翻譯作者: XXX為了強(qiáng)調(diào)Delphi 組件與其他對(duì)象沒(méi)有太多的區(qū)別的事實(shí)(同時(shí)說(shuō)明Self 關(guān)鍵字日使用)的使用方法,筆者編寫(xiě)了一個(gè) CreateComps范例。這個(gè)程序有一個(gè)不帶 組件的窗體和一個(gè)用語(yǔ)其OnMouseDow事件的處理器,之所以選擇它是以為它將鼠 標(biāo)單機(jī)的位置作為一個(gè)參數(shù)接收(與OnClick 事件不同)。我

46、們需要這個(gè)信息,以便在那個(gè)位置創(chuàng)建有個(gè)按鈕組件。下面是該方法的代碼: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;這段代碼的作用是在鼠標(biāo)單擊的位置創(chuàng)建按鈕,正如在下圖中看到的那樣。在該代碼中,特別要注意Self關(guān)鍵字的使用??同時(shí)作為Create方法的參數(shù)(以指定 組件的所有者)和Parent屬性的值。CreateCompsr的示例輸出,用于在運(yùn)行時(shí)創(chuàng)建按鈕組件封裝的概念簡(jiǎn)單,可以把其想象為一個(gè)“黑盒子”。人們并不知道其內(nèi)部什么:只能知道牌位與黑例子接口,或使用它而其內(nèi)部結(jié)構(gòu)。“怎樣使用”這部分,稱 為類(lèi)的接口,它允許程序的其他部分訪問(wèn)和使用該類(lèi)的對(duì)象。然而,使用對(duì)象時(shí),對(duì)象 的大部分第9頁(yè)共13頁(yè)畢業(yè)設(shè)計(jì)英文翻譯作者:XXX代碼都是隱含的。用戶很少能知道

48、對(duì)象胡哪些內(nèi)部數(shù)據(jù),而且一般沒(méi)有辦法可以直接訪問(wèn)數(shù)據(jù)。可以使用對(duì)象方法來(lái)訪問(wèn)數(shù)據(jù),這與非法的訪問(wèn)不同。相對(duì)于信息隱含這樣一個(gè)經(jīng)典的編程概念來(lái)說(shuō),這就是面向?qū)ο蟮姆椒?。然而,然而Delphi 中,通過(guò)屬性會(huì)有其他等級(jí)的。對(duì)基于類(lèi)的封裝來(lái)說(shuō),Delphi 使用了三個(gè)訪問(wèn)標(biāo)識(shí)符:PrivateProtected 和Public第四個(gè)地Published ,控制著運(yùn)行時(shí)的類(lèi)型信息(RTTI)和設(shè)計(jì)時(shí)信息,但是它提供了和Public 相同的程序訪問(wèn)性。這里是三個(gè)經(jīng)典的訪問(wèn)標(biāo)識(shí)符。a) Private 指令專(zhuān)門(mén)用于一個(gè)類(lèi)字段和對(duì)象方法在聲明類(lèi)的單元外的類(lèi)不能被訪問(wèn)。b) Protected 指令用于表示對(duì)

49、象方法和字段具有有限的可見(jiàn)性。Protected 元素只能被當(dāng)前類(lèi)和它的子類(lèi)訪問(wèn)。更精確地說(shuō),只有同一個(gè)單元中的類(lèi)、子類(lèi)和任何代碼可以訪問(wèn) protected 成員。c) Public 指令專(zhuān)門(mén)用于表示那些可以被程序代碼中的任意部分高談闊論的數(shù)據(jù)和對(duì)象方法,當(dāng)然也包括在定義它們的單元。一般情況下,一個(gè)類(lèi)的字段應(yīng)該是專(zhuān)用的,而對(duì)象方法則應(yīng)該是公用的。然而如果某對(duì)象方法只需要在內(nèi)部完成一些部分或?qū)崿F(xiàn)屬性,那么該方法也可以是專(zhuān)用或受難保護(hù)的。字段可以被聲明為受難,這樣就可以在子類(lèi)中對(duì)它們進(jìn)行處理,盡管這并不是一個(gè)好的OPP亍為。屬性是一種非常能夠有效的OOFM制,或者說(shuō)非常適合實(shí)現(xiàn)封裝的思想。從本質(zhì)

50、上講,書(shū)信就是用一個(gè)名稱來(lái)完全隱藏它的實(shí)現(xiàn)細(xì)節(jié),這使得程序員可以任意修改類(lèi),而還會(huì)影響使用它的代碼。關(guān)于什么是屬性,有一個(gè)較好的定義,亦即屬性就是字段。從定義它們的類(lèi)的用戶角度來(lái)看,屬性完全就像字段一樣,因?yàn)閾碜o(hù)可以讀取或編寫(xiě)它們的值。例如,可以用以下代碼讀取一個(gè)按鈕的Caption 屬性值,并將其賦給一個(gè)帶有下寫(xiě)代碼的編輯筐的text 屬性:Edit1.Text:=Button1.Caption;這看起來(lái)就像讀寫(xiě)字段一樣。然而,屬性可以直接與數(shù)據(jù)以及訪問(wèn)對(duì)象的方法對(duì)應(yīng)起來(lái),用于讀寫(xiě)數(shù)值。當(dāng)屬性對(duì)象方法對(duì)應(yīng)時(shí),訪問(wèn)的數(shù)據(jù)可以是對(duì)象或其外部和某部第 10 頁(yè) 共 13 頁(yè)畢業(yè)設(shè)計(jì)英文翻譯 作者:

51、XXX分內(nèi)容,而且它們可以產(chǎn)生附加影響,如在改變了一個(gè)屬性值后,提親繪制一個(gè)控件。從技術(shù)上講,一個(gè)屬性就是對(duì)應(yīng)數(shù)據(jù)或?qū)ο蠓椒ǎㄊ褂靡粋€(gè)read 或一個(gè)子句)的標(biāo)識(shí)符。例如,下面是一個(gè)日期類(lèi)的Month屬性的定義:Property month: Integer read FMonth write SetMonth;為了訪問(wèn)Month屬性的值,該程序語(yǔ)句要讀取專(zhuān)用字段 FMonth的值,同時(shí)為其改變?cè)搶傩灾?,它調(diào)用了對(duì)象方法SetMonth ( 當(dāng)然,必須在類(lèi)的內(nèi)部定義它)不同的組合都是可能的(例如,還可以使用一個(gè)對(duì)象方法來(lái)讀取屬性值或直接修改write 指令中的一個(gè)字段),但使用對(duì)象方法修改一個(gè)

52、屬性的值是很覺(jué)的。下面是屬性的兩種可替換定義,它們分別對(duì)應(yīng)兩個(gè)訪問(wèn)方法或在兩個(gè)方向上直接對(duì)應(yīng)著數(shù)據(jù):property Month: Integer read GetMonth write SetMonth;property Month: Integer read FMonth write FMonth;通常,屬性是公共的。而實(shí)際數(shù)據(jù)與訪問(wèn)對(duì)象方法是專(zhuān)用的(或受保護(hù)的),這意味著必須使用屬性訪問(wèn)那些對(duì)象方法或數(shù)據(jù),這種技術(shù)提供了封裝的擴(kuò)展簡(jiǎn)化版本。它是一個(gè)擴(kuò)展的封裝,不但可以改變數(shù)據(jù)的表示方法與訪問(wèn)函數(shù),而且還可以添加與刪除訪問(wèn)函數(shù)。而還會(huì)影響到調(diào)用代碼。一個(gè)用戶只重新編譯使用屬性的程序即可。 6TDate作為一個(gè)范例,我向前面討論過(guò)的TDate 類(lèi)的一個(gè)對(duì)象中添加用于訪問(wèn)年、月、日的屬性。這些屬性參與特定的字段對(duì)應(yīng)著存儲(chǔ)完整日期信息的單個(gè)TDate字段。這就是所有屬性都有g(shù)etter 和 setter 方法的原因:typeTDate = classpublicproperty Year: Integer read GetYear write SetYear;pro

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論