第二電腦網導讀:是面向過程語言所不能比擬的,本文所有程序設計例程均由DELPHI中的OBJECT PASCAL語言進行描述。首先要說明的是,本文不是和大家討論如何用可視化開發平臺的IDE去設計程序的界面,也不用大篇幅的和大家討論各個控件VCL的應用,不討論如何用開發平臺去完成一個MIDAS/ADO/DCOM數據庫的開發過程,本文只和大家簡單,膚淺的討論面向對象語言的技術,和大家討論面向對象語言內在的規則,...
<iframe id="google_ads_frame1" style="LEFT: 0px; POSITION: absolute; TOP: 0px" name="google_ads_frame" marginwidth="0" marginheight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9414907233156792&output=html&h=250&slotname=5594755314&w=300&lmt=1267524486&flash=10.1.102.64&url=http%3A%2F%2Fwww.002pc.com%2Fmaster%2FCollege%2FLanguage%2FDelphi%2F12980_5.html&dt=1292297143640&shv=r20101117&jsv=r20101209&saldr=1&correlator=1292297143703&frm=0&adk=2114289378&ga_vid=2056410259.1292297144&ga_sid=1292297144&ga_hid=1170765305&ga_fc=0&u_tz=480&u_his=0&u_java=1&u_h=768&u_w=1024&u_ah=734&u_aw=1024&u_cd=32&u_nplug=0&u_nmime=0&biw=771&bih=458&eid=30143103&ref=http%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3DDelphi%25B3%25CC%25D0%25F2%25C9%25E8%25BC%25C6%25BC%25BC%25CA%25F5%25CC%25D6%25C2%25DB%26pn%3D80&fu=0&ifi=1&dtd=156&xpc=1l1lkerKiP&p=http%3A//www.002pc.com" frameborder="0" width="300" scrolling="no" height="250" allowTransparency="allowTransparency"></iframe><iframe style="VISIBILITY: hidden; POSITION: absolute" src="http://pagead2.googlesyndication.com/pagead/s/iframes_api_loader.html" width="1" height="1"></iframe>
正文:inherited create;
name:='gaohongyan';
age:=23;
school:='東北電力學院-電子商務系-2001級大專12班';
teacher:='zhaolifei';
end;
procedure tghyother.display;
begin
writeln(name);
writeln(age);
writeln(school);
writeln(teacher);
end;
end.
當執行完這個程序后,屏幕上出現這個結果:
gaohongyan
23
東北電力學院-電子商務系-2001級大專12班
zhaolifei
我們結合上面的例子不難看出在unit1中聲明的tghy類,在unit2中的tghyother是繼承自tghy類的,是tghy類的一個"孩子",它們是父子關系,而在tghy類中的name,age成員是聲明在protected保護成員中的,根據保護成員的特性,我們就可以在tghyother類的實例newobject中訪問tghy中的成員內容了,如果把tghy類中的name,age聲明在private私有成員中,那么是不可能被tghyother類的實例newobject所訪問到的。
請切記,每天下班后把你認為對孩子重要事情和他交流。
如果要想讓自己的派生類訪問基類中的成員,聲明在protected段中。
1.0.5.3 公有成員public
每個人每天都會發生一些讓自己感到快樂的事情,你會告訴你的家人,你的朋友,你愿意把這個快樂和大家一起分享,如果是這樣的話,那就是public。
公有成員Public是把要公開的信息發表出去的地方,你想把任何的消息讓任何人知道,就通過public發布出去。通過把向外界提供服務功能放在此部分中,實現向外界提供服務。
舉個例子:
新建一個工程,工程中包括一個工程文件,及一個單元文件。
工程文件內容:
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils,
Unit1 in 'Unit1.pas',
Unit2 in 'Unit2.pas';
var
newobject:tghy;
begin
newobject:=tghy.create;
newobject.display;
newobject.free;
end.
單元文件1內容:
unit Unit1;
interface
uses Dialogs;
type
tghy=class
public
constructor create;
procedure display; virtual;
end;
implementation
constructor tghy.create;
begin
end;
procedure tghy.display;
begin
writeln('this is public');
end;
end.
程序運行后,屏幕上出現結果:
this is public
通過上面的這個例子,我們知道在工程文件中聲明一個tghy類的實例newobject能訪問tghy類的public中的部分。通常一個類的方法就聲明在public中,來為其類的實例進行任何的數據操作。
請切記,把你每天快樂的事情告訴給你的家人,朋友。
把你要發布的信息放到public中去,讓大家都知道。
1.0.5.4 發行成員published
如果您十分的熟悉DELPHI的開發環境的話,那么屬性編輯器一定是您最常用的工具了,里面顯示了當前控件的屬性,顯示了當前控件的狀態,里面顯示的狀態的內容就是聲明在published里面的,我們可以參考一下delphi的源碼:
TControlScrollBar = class(TPersistent)
published
property ButtonSize: Integer read FButtonSize write SetButtonSize default 0;
property Color: TColor read FColor write SetColor default clBtnHighlight;
property Increment: TScrollBarInc read FIncrement write FIncrement stored IsIncrementStored default 8;
property Margin: Word read FMargin write FMargin default 0;
property ParentColor: Boolean read FParentColor write SetParentColor default True;
property Position: Integer read FPosition write SetPosition default 0;
property Range: Integer read FRange write SetRange stored IsRangeStored default 0;
property Smooth: Boolean read FSmooth write FSmooth default False;
property Size: Integer read FSize write SetSize default 0;
property Style: TScrollBarStyle read FStyle write SetStyle default ssRegular;
property ThumbSize: Integer read FThumbSize write SetThumbSize default 0;
property Tracking: Boolean read FTracking write FTracking default False;