⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch03.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//-->                                                                </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css">	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">	<TITLE>Teach Yourself Borland Delphi 4 in 21 Days -- Ch 3 -- Classes and Object-Oriented Programming</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"></H1><H1><BR>Teach Yourself Borland Delphi 4 in 21 Days</H1></CENTER><CENTER><P><A HREF="../ch02/ch02.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch04/ch04.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><CENTER><H1>- 3 -</H1></CENTER><CENTER><H1>Classes and Object-Oriented Programming</H1></CENTER><UL>	<LI><A HREF="#Heading1">Sets</A>	<UL>		<LI><A HREF="#Heading2">Styles := Styles - [fsItalic];</A>		<LI><A HREF="#Heading3">Styles := [fsBold, fsItalic];</A>	</UL>	<LI><A HREF="#Heading4">Casting</A>	<LI><A HREF="#Heading5">Pointers</A>	<UL>		<LI><A HREF="#Heading6">Local Versus Dynamic Memory Usage</A>		<LI><A HREF="#Heading7">Dynamic Allocation and Pointers</A>		<LI><A HREF="#Heading8">Dereferencing a Pointer</A>	</UL>	<LI><A HREF="#Heading9">What's a Class?</A>	<LI><A HREF="#Heading10">Anatomy of a Class</A>	<LI><A HREF="#Heading11">Class Access Levels</A>	<UL>		<LI><A HREF="#Heading12">Constructors</A>		<LI><A HREF="#Heading13">Destructors</A>		<LI><A HREF="#Heading14">Data Fields</A>		<LI><A HREF="#Heading15">Methods</A>		<LI><A HREF="#Heading16">About Self</A>		<LI><A HREF="#Heading17">A Class Example</A>	</UL>	<LI><A HREF="#Heading18">Inheritance</A>	<LI><A HREF="#Heading19">Overriding Methods</A>	<UL>		<LI><A HREF="#Heading20">Class Keywords: is and as</A>	</UL>	<LI><A HREF="#Heading21">Summary</A>	<LI><A HREF="#Heading22">Workshop</A>	<UL>		<LI><A HREF="#Heading23">Q&amp;A</A>		<LI><A HREF="#Heading24">Quiz</A>		<LI><A HREF="#Heading25">Exercises</A>	</UL></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>Today you get to the good stuff. In this chapter you will learn about classes.Classes are the heart of Object Pascal and a major part of object-oriented programming.Classes are also the heart of the Visual Component Library (VCL), which you willuse when you start writing real Windows applications. (The VCL is discussed in detailon Day 5, &quot;The Visual Component Model.&quot;) Today you will find out what aclass is and how it's expected to be used. Along the way you will learn the meaningof Object Pascal buzzwords like <I>inheritance</I>, <I>object</I>, and <I>data</I><I>abstraction</I>. Before you get to that, however, I want to cover a few more aspectsof Object Pascal that I haven't yet covered.</P><P><H2><A NAME="Heading1"></A>Sets</H2><P>Sets are used frequently throughout Delphi, so you need to know what sets areand how they work.</P><P>A <I>set</I> is a collection of values of one type.</P><P>That description doesn't say too much, does it? An example that comes to mindis the Style property of a VCL font object. This property can include one or moreof the following values:</P><UL>	<LI>fsBold	<P>	<LI>fsItalic	<P>	<LI>fsUnderline<BR>	<BR>		<LI>fsStrikeout</UL><P>A font can have any combination of these styles or none of them at all. A setof font styles, then, might have none of these values, it could have all of them,or it could have any combination.</P><P>So how do you use a set? Let me use the Style property to illustrate. Typically,you turn the individual Style values for the font on or off at design time. Sometimes,however, you need to set the font's Style property at runtime. For example, let'ssay that you want to add the bold and italic attributes to the font style. One wayis to declare a variable of type TFontStyles and then add the fsBold and fsItalicstyles to the set. Here's how it looks:</P><P><PRE>var  Styles : TFontStyles;begin  Styles := Styles + [fsBold, fsItalic];end;</PRE><P>This code adds the elements fsBold and fsItalic to the Styles set. The elementsare enclosed in brackets to indicate that you are adding elements to the set. Thebrackets, when used in this way, are called a <I>set constructor</I>. Notice thatthis code doesn't actually change a font's style; it just creates a set and addstwo elements to it. To change a font's style, you have to assign this newly createdset to the Font.Style property of some component:</P><P><PRE>Memo.Font.Style = Styles;</PRE><P>Now, let's say that you want the font to be bold but not italic. In that case,you have to remove the italic style from the set:</P><P><H3><A NAME="Heading2"></A>Styles := Styles - [fsItalic];</H3><P>The style now contains only the fsBold value because the fsItalic value has beenremoved.</P><P>Often you want to know whether a particular item is in a set. Let's say you wantto know whether the font is currently set to bold. You can find out whether the fsBoldelement is in the set by using the in keyword:</P><P><PRE>if fsBold in Styles then  DoSomething;</PRE><P>Sometimes you need to make sure you are starting with an empty set. You can cleara set of its contents by assigning an empty set to a set variable. This is done withan empty set constructor--for example,</P><P><PRE>{ start with an empty set } Styles := [];{ now add the bold and italic styles } Styles := Styles + [fsBold, fsItalic];</PRE><P>In this example the font style is cleared of all contents, and then the bold anditalic styles are added. This same thing can be accomplished in a slightly differentway by just assigning directly to a set:</P><P><H3><A NAME="Heading3"></A>Styles := [fsBold, fsItalic];</H3><P>You don't specifically have to create a TFontStyles variable to change a font'sstyle. You can just work with the property directly--for example,</P><P><PRE>Memo.Font.Style := [];Memo.Font.Style := Memo.Font.Style + [fsBold, fsItalic];</PRE><P>A set is declared using the set keyword. The TFontStyles property is declaredin the VCL source file GRAPHICS.PAS like this:</P><P><PRE>TFontStyle = (fsBold, fsItalic, fsUnderline, fsStrikeOut);</PRE><PRE>TFontStyles = set of TFontStyle;</PRE><P>The first line here declares an enumeration type called TFontStyle. (An <I>enumeration</I>is a list of possible values.) The second line creates the TFontStyles set as a setof TFontStyle values.</P><P>Sets are used often in VCL and in Delphi programming. Many component propertiesare defined as sets. You'll get the hang of sets quickly as you work with Delphi.</P><P><H2><A NAME="Heading4"></A>Casting</H2><PRE><I><strong>New Term:</strong>  Cast</I> means to tell the compiler to treat one data type as if it were a different type. Another term for cast is <I>typecast</I>.</PRE><P>Here's an example of a Char data type typecast to an Integer:</P><P><PRE>procedure TForm1.Button1Click(Sender: TObject);var  AChar : Char;  AnInteger : Integer;begin  AChar := `A';  AnInteger := Integer(AChar);  Label1.Caption := IntToStr(AnInteger);end;</PRE><P>In this example, the cast Integer(AChar) tells the compiler to convert the valueof AChar to an Integer data type. The cast is necessary because you can't assignthe value of a Char data type to an Integer type. If you attempt to make the assignmentwithout the cast, the compiler will issue an error that reads Incompatible types:`Integer' and `Char'.</P><P>By the way, when the preceding code executes, the label will display the text65 (65 is the integer value of the character A).</P><P>It is not always possible to cast one data type to another. Take this code, forexample:</P><P><PRE>procedure TForm1.Button1Click(Sender: TObject);var  Pi : Double;  AnInteger : Integer;begin  Pi := 3.14;  AnInteger := Integer(Pi);  Label1.Caption := IntToStr(AnInteger);end;</PRE><PRE>In this case, I am trying to cast a Double to an Integer. This is not a valid cast, so the compiler will issue an error that reads Invalid typecast. To convert a floating-point value to an integer value, use the Trunc, Floor, or Ceil functions. These functions do just as their names indicate, so I don't need to explain further. See the Delphi help for more information on these functions.</PRE><P>Pointers can be cast from one type to another using the as operator. (Pointersare discussed in the next section.) I'll discuss the as operator later in the section&quot;Class Keywords: is and as.&quot;</P><P><H2><A NAME="Heading5"></A>Pointers</H2><P>Pointers are one of the most confusing aspects of the Object Pascal language.So what is a pointer? It's a variable that holds the address of another variable.There, that wasn't so bad, was it? I wish it were that simple! Because a pointerholds the address of another variable, it is said to &quot;point to&quot; the secondvariable. This is called <I>indirection</I> because the pointer does not have a directassociation with the actual data, but rather an indirect association.</P><P><strong>New Term:</strong> A <I>pointer</I> is a variable that holds the address of anothervariable.</P><P>Let's look at an example. Let's say you have a record, and you need to pass theaddress of that record to a procedure requiring a pointer. You take the address ofa record instance using the @ operator. Here's how it looks:</P><P><PRE>var  MLRecord : TMailingListRecord;APtr : Pointer;begin  { Fill MLRecord with data. }   APtr := @MLRecord;  SomeFunction(APtr);end;</PRE><PRE>The APtr variable (which is of type Pointer) is used to hold the memory address of the MLRecord record. This type of pointer is called an <I>untyped pointer</I> because the Pointer data type simply holds a memory address. Another type of pointer is a pointer that is declared as a pointer to a specific type of object. For example, let's say that you create a new type, a pointer to TMailingListRecord record. The declaration would look like this:</PRE><PRE>type  PMailingListRecord = ^TMailingListRecord;  TMailingListRecord = record    FirstName : string;    LastName : string;    Address : string;    City : string;    State : string;    Zip : Integer;  end;</PRE><P>The type PMailingListRecord is declared as a pointer to a TMailingListRecord.You will often see records and their corresponding pointers declared in this way.You might be wondering what the point is (no pun intended). Let's go on to the nextsection and I'll show you one way pointers are used.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> I almost never use long strings in records as I have done here with	the TMailingListRecord. I usually use an array of Char rather than a long string.	The reason for this is that long strings are dynamically allocated and are not a	fixed size. Fixed-size fields are important if you are writing records to disk. I	used long strings in the case of TMailingListRecord because I didn't want to muddy	the waters with a discussion on fixed-length records at this point in the book. 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -