计算机专业外文翻译_第1页
计算机专业外文翻译_第2页
计算机专业外文翻译_第3页
计算机专业外文翻译_第4页
计算机专业外文翻译_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、计算机专业外文翻译毕业设计英文翻译作者: 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 页毕业设计英文翻译作者: 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页毕业设计英文翻译作者: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 页毕业设计英文翻译作者: 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 页毕业设计英文翻译作者: 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 页毕业设计英文翻译作者: 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 页毕业设计英文翻译作者: 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 页毕业设计英文翻译作者: XXXDelphi 开发环境是基于Pasc

43、al 编程语言Object Pascal 的面向对象的一种扩展。大多数现代编程语言都支持面向对象编程( OPP。OPP®言基于三个基本概念:封装(通常通过类实现)继承、多态。1、 类与对象Delphi是基于OPP既念的,特别是在新类类型中。OPP的使用在可视开发环境中得到了增强,这是因为,对于每个在设计时定义的一个新窗体来说,Delphi 会自动定义一个新的类。另外,每个可视放置在一个窗体中的组件实际上是一个类型对象,而且该类可以在系统库中获得,或者被添加到系统库中。就像大多数开创现代 OPP®言(包括Java和C#)中一卞在Delphi中,一个类的类型变量为对象提供存储,

44、而只是在内存中为对象提供一个指针或引用。在使用对象之前,必须创建一个新的实例或将一个现有的实例分配给变量,以此来为其分配内存:varObj1, Obj2: TMyClass;begin / assign a newly created objectObj1 := TMyClass.Create; / assign to an existing objectObj2 := ExistingObject;对 Create 的调用会激活一个默认的构造器,该构造器对每个类都有效,除非某个类重新定义了它。为了在Delphi 中声明一个新的带有一些本地数据字段的和一些方法的类数据类型,可使用下面的语法:t

45、ypeTDate = classMonth, Day, Year: Integer;procedure SetValue (m, d, y: Integer);function LeapYear: Boolean;end;动态的创建组件第 8 页 共 13 页毕业设计英文翻译作者: XXX为了强调Delphi 组件与其他对象没有太多的区别的事实(同时说明Self 关键字日使用)的使用方法,笔者编写了一个 CreateComps范例。这个程序有一个不带 组件的窗体和一个用语其OnMouseDow事件的处理器,之所以选择它是以为它将鼠 标单机的位置作为一个参数接收(与OnClick 事件不同)。我

46、们需要这个信息,以便在那个位置创建有个按钮组件。下面是该方法的代码: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;这段代码的作用是在鼠标单击的位置创建按钮,正如在下图中看到的那样。在该代码中,特别要注意Self关键字的使用??同时作为Create方法的参数(以指定 组件的所有者)和Parent属性的值。CreateCompsr的示例输出,用于在运行时创建按钮组件封装的概念简单,可以把其想象为一个“黑盒子”。人们并不知道其内部什么:只能知道牌位与黑例子接口,或使用它而其内部结构。“怎样使用”这部分,称 为类的接口,它允许程序的其他部分访问和使用该类的对象。然而,使用对象时,对象 的大部分第9页共13页毕业设计英文翻译作者:XXX代码都是隐含的。用户很少能知道

48、对象胡哪些内部数据,而且一般没有办法可以直接访问数据。可以使用对象方法来访问数据,这与非法的访问不同。相对于信息隐含这样一个经典的编程概念来说,这就是面向对象的方法。然而,然而Delphi 中,通过属性会有其他等级的。对基于类的封装来说,Delphi 使用了三个访问标识符:PrivateProtected 和Public第四个地Published ,控制着运行时的类型信息(RTTI)和设计时信息,但是它提供了和Public 相同的程序访问性。这里是三个经典的访问标识符。a) Private 指令专门用于一个类字段和对象方法在声明类的单元外的类不能被访问。b) Protected 指令用于表示对

49、象方法和字段具有有限的可见性。Protected 元素只能被当前类和它的子类访问。更精确地说,只有同一个单元中的类、子类和任何代码可以访问 protected 成员。c) Public 指令专门用于表示那些可以被程序代码中的任意部分高谈阔论的数据和对象方法,当然也包括在定义它们的单元。一般情况下,一个类的字段应该是专用的,而对象方法则应该是公用的。然而如果某对象方法只需要在内部完成一些部分或实现属性,那么该方法也可以是专用或受难保护的。字段可以被声明为受难,这样就可以在子类中对它们进行处理,尽管这并不是一个好的OPP亍为。属性是一种非常能够有效的OOFM制,或者说非常适合实现封装的思想。从本质

50、上讲,书信就是用一个名称来完全隐藏它的实现细节,这使得程序员可以任意修改类,而还会影响使用它的代码。关于什么是属性,有一个较好的定义,亦即属性就是字段。从定义它们的类的用户角度来看,属性完全就像字段一样,因为拥护可以读取或编写它们的值。例如,可以用以下代码读取一个按钮的Caption 属性值,并将其赋给一个带有下写代码的编辑筐的text 属性:Edit1.Text:=Button1.Caption;这看起来就像读写字段一样。然而,属性可以直接与数据以及访问对象的方法对应起来,用于读写数值。当属性对象方法对应时,访问的数据可以是对象或其外部和某部第 10 页 共 13 页毕业设计英文翻译 作者:

51、XXX分内容,而且它们可以产生附加影响,如在改变了一个属性值后,提亲绘制一个控件。从技术上讲,一个属性就是对应数据或对象方法(使用一个read 或一个子句)的标识符。例如,下面是一个日期类的Month属性的定义:Property month: Integer read FMonth write SetMonth;为了访问Month属性的值,该程序语句要读取专用字段 FMonth的值,同时为其改变该属性值,它调用了对象方法SetMonth ( 当然,必须在类的内部定义它)不同的组合都是可能的(例如,还可以使用一个对象方法来读取属性值或直接修改write 指令中的一个字段),但使用对象方法修改一个

52、属性的值是很觉的。下面是属性的两种可替换定义,它们分别对应两个访问方法或在两个方向上直接对应着数据:property Month: Integer read GetMonth write SetMonth;property Month: Integer read FMonth write FMonth;通常,属性是公共的。而实际数据与访问对象方法是专用的(或受保护的),这意味着必须使用属性访问那些对象方法或数据,这种技术提供了封装的扩展简化版本。它是一个扩展的封装,不但可以改变数据的表示方法与访问函数,而且还可以添加与删除访问函数。而还会影响到调用代码。一个用户只重新编译使用属性的程序即可。 6TDate作为一个范例,我向前面讨论过的TDate 类的一个对象中添加用于访问年、月、日的属性。这些属性参与特定的字段对应着存储完整日期信息的单个TDate字段。这就是所有属性都有getter 和 setter 方法的原因:typeTDate = classpublicproperty Year: Integer read GetYear write SetYear;pro

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论