Delphi的常量定义(灌水篇之7)

ForestBen
.常数宣告 (使用标记 = )
2.1一般常数宣告
CONST
Min = 0;
Max = 100;
Center = ( Max - Min ) Div 2;
Blank = Chr(32);
NumChr = Ord('Z') - Ord('A') + 1;
ErrMsg = 'Out Of Rang';
ErrDtl = 'Out Of Rang' + ':Item 10';
Numeric = ['0'..'9'];
Alpha = ['A'..'Z','a'..'z'];
AlphNum = Alpha + Numeric;
2.1型态常数(Typed constant)宣告
CONST
MaxInt : Integer = 9999;
FixReal : Real = -0.12;
ListStr : String[4] = 'This';
AA : Pchar = 'abcedf';
Dim : Array[0..1,0..1,0..1] of Integer = (((0,1),(2,3)),((4,5),(6,7)));
{ Dim(0,0,0) = 0 Dim(0,0,1) = 1
Dim(0,1,0) = 2 Dim(0,1,1) = 3
Dim(1,0,0) = 4 Dim(1,0,1) = 5
Dim(1,1,0) = 6 Dim(1,1,1) = 7 }
--------------------------------
TYPE
Trec = record
fl1,fl2 : Integer;
end;
CONST
IntRec : Trec = ( fl1:1;fl2:2);
------------------------------------------
3.型态宣告 (使用标记 = ) : 当宣告一个变数时,必须指明其型态
3.1 例子(1)
TYPE
Trang = Integer;
TNumber = Integer;
TColor = ( Red , Green , Blue );
TCharVal = Ord('A')..Ord('Z');
TtestIndex = 1..100;
TtestValue = -99..99;
TtestList = Array[TtestIndex] of Ttestvalue;
ptestList = ^TtestList; =>指标型态
Tdate = Class
Year : Integer;
Month : 1..12;
Day : 1..31;
Procedure SetDate(D,M,Y:Integer);
Function ShowDate : String;
end;
TMeasureList = Array[1..50] of TMeasuredate;
Tname = String[80]; =>定长度
Tsex = (Male , Female); =>0 1
PpersonData = record
Name , FirstName : Tname;
Age : Integer;
Married : Boolean;
TFather,TChild,TSibling : PPersonData;
Case s: Tsex of =>0和1之间
Maie : ( Bearded : Boolean );
Female : (Pregnant : Boolean );
End;
TPersonBuf = Array[0..Size(TPsersonData) - 1] of Byte;
TPeople = File Of TPersonData; =>定type
3.2 简单型态
3.2.1序数型态
(1)整数型态:
基础型态(Fundmental)
Shortint -128..127 8-bit
Smallint -32768..32767 16-bit
Longint -2147483648..2147483647 32-bit
Byte 0..255 8-bit
Word 0.65535 16-bit
通用型态(Generic)
Integer -2147483648..2147483647 32-bit
Cardinal 0..2147483647 32-bit
若16bit
Integer -32768..32767 16-bit
Cardinal 0.65535 16-bit
(2)字元型态
基础型态(Fundmental)
AnsiChar ASCII 1-Byt
WideChar Unicode 2-Byt
通用型态(Generic)
Char ASCII 1-Byt
(3)列举型态(Enumerated Type)
==============================================
TColor = ( Red , Green , Blue );
==============================================
(4)逻辑型态(Boolean Type)
基础型态(Fundmental)
ByteBool 0..1 1-Byt
WordBool 0..1 2-Byt
LongBool 0..1 4-Byt
通用型态(Generic)
Boolean 1-Byt
==============================================
Married : Boolean;
False < True
Ord(False) = 0
Ord(True) = 1
Succ(False)=True
Pred(True)=False
==============================================
(5)子集型态(Subrang type)
==============================================
TtestIndex = 1..100;
==============================================
3.2.2实数型态
型态 有效位数 位元组大小
Real 11-12 6
Single 7-8 4
Douible 15-16 8
ExTended 10-20 10
Comp 19-20 8
Currency 19-20 8
===================================================
例子 Real 6byt 共48 bit
1-1 2-40 41-48
s f e
有效值 v
if ( e > 0 ) and ( e <= 255 ) then
v = (-1)**s * 2 ** (e - 129) * (1.f)
else if e = 0 then v = 0;
===================================================
3.3 字串型态
3.3.1 基础型态(Fundmental)
(1) ShortString 短字串 1-255 (又称pascal string)
(2) AnsiString 长字串 1-2G
(3) PChr Null-Terminated string
3.3.2 通用型态(Generic)
String 32位元时 等於 AnsiString
16位元时 等於 ShortString
===================================================
(1) 字串为一以零为基底的字元阵列(Zero-base character array)

-----------------------
TYPE
TStringBuf = Array[0..79] of Char;
const
StringBuf : TStringBuf = 'This is test';
-----------------------
(2) var s : ShortString
Length(s) = Ord(s[0])
(3) 长字串以4-byt指标,指到动态配置之记忆体,没有[0]之长度数值
一个动态记忆体配置之长字串会自动以一空字元(null Character)结束
(4) PChr 以字元阵列来储存,结尾以(null Character)结束,使用指标来管理
(5) PChr型态与String型态在指定叙述上是相容
例:
-----------------------------
var p:pchr;
begin
p := 'this is test';
end;
-----------------------------
Procedure PrintStr(Str:Pchr);
呼叫时可用
PrintStr('This is test');
-----------------------------
3.4 结构型态(Strictured type)
3.4.1 阵列型态(Array type)
TDim1 = Array[0..79] of Char;
TDim2 = Array[1..3,1..10] of Char;
VAR
Dim1 : Tdim1;
I : Integer;
begin
for I := Low(Dim1) to High(Dim1) do
Dim1 := #32;
end;
===================================================
3.4.2 记录型态(Record Type)
TYPE
TdateRec = Record
Year : Integer;
Month : 1..12;
Day : 1..31;
end;
何谓变动栏位 ?
3.4.3 集合型态(Set Type)
TYPE
Tmenber = ( Lee , White , Ston );
Tmenbers = Set Of Tmenber;
var Members : Tmenbers;
begin
Menbers := [White , Ston];
end;
集合型态不能超过256个可能值
3.4.4 档案型态(File Type)
(1)记录型态档案
TYPE
Temployee = record
name : string[10];
address : string[50];
end;
TemployeeFile = File Of Temployee;
var
EmployeeFile : TemployeeFile;
同 EmployeeFile : File Of Temployee;
(2)文件型态档案
TMyTextFile = TextFile;
var
MyTextFile : TMyTextFile;
or
MyTextFile : TextFile;
ReadText : String;
-----------------------------
AssignFile( MyTextFile , 'c:\MyFile.txt');
Reset( MyTextFile );
While Not Eof( MyTextFile ) do
begin
Readln( MyTextFile , ReadText );
end;
CloseFile( MyTextFile );
注:Rewrite(MyTextFile); 开启唯写
Writeln(MyTextFile , ReadText);
(3)其它型态档案
TBook = File Of Char;
TnumFile = File Of Integer;
3.4.5 类别型态(Class Type)
TDBText = class(TCustomLabel)
private
FDataLink: TFieldDataLink;
procedure DataChange(Sender: TObject);
function GetDataField: string;
function GetDataSource: TDataSource;
function GetField: TField;
function GetFieldText: string;
procedure SetDataField(const Value: string);
procedure SetDataSource(Value: TDataSource);
procedure CMGetDataLink(var Message: TMessage); message CM_GETDATALINK;
protected
function GetLabelText: string; override;
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure SetAutoSize(Value: Boolean); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Field: TField read GetField;
published
property Align;
property Alignment;
property AutoSize default False;
property Color;
property DataField: string read GetDataField write SetDataField;
property DataSource: TDataSource read GetDataSource write SetDataSource;
property DragCursor;
property DragMode;
property Enabled;
property Font;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property Transparent;
property ShowHint;
property Visible;
property WordWrap;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
end;
元件之可见性
注-1:同一unit中Private,Protect,Public,published皆同Public
不同unit时其特性不同
Private : 所属unit才有可见性
Protect : 只有继承时才有可见性
Public : 所有uses者皆有可见性
published : 所有uses者皆有可见性,且提供元件设计时之栏位可见性
注-2:若宣告没有注明4P,则表示
3.5 指标型态(Pointer type)
3.5.1 字元指标(character pointer)
在 system unit 中,有下列宣告
TYPE
PAnsiChar = ^AnsiChar;
PWideChar = ^WideChar;
PChar = PAnsiChar;
所以在2.0版中,PChar和PAnsiChar 是一样的
3.5.2 通用指标
TYPE
TMyPointer = Pointer;
通用指标可以被型态转换後来参考
3.6 程序型态(Procedure type)
3.6.1 全域程序指标
TYPE
TStrProc = Procedure( Const s: String );
TMyFunc = Function( X:Integer ) : String;
3.6.2 方法程序指标
TYPE
TNotifyEven = Procedure(Sender :Tobject) of Object;
3.6.3 程序数值
一个程序型态变数可以被指定程序数值
TYPE
TMainForm = Class(TForm)
Procedure ButtonClick(Sender:Tobject);
end;
VAR
MyForm : TMainForm;
MyFunc : TMathFunc;
Function ChgFunc(X:Integer):Integer;Far;
Begin
Result := X + X;
end;
MyFunc := ChgFunc;
X := MyFunc(X); {等於 X := ChgFunc(X)}
==========================================
一个程序变数未被指定数值时其数值是 nil,
但未被指定数值之程序或函数不能执行,
故安全之呼叫是
If Assigned(OnClick) Then OnClick(Self);
3.7 变动型态(Variant type)
(1) 变动型态和其它型态一起使用会作自动型态转换 =>使用arrey
Var
V1,V2,V3,V4,V5 : Variant;
I : Integer;
D:Double; 浮点
S:String;
Begin
V1 := 1;
V2 := 1234.5678;
V3 := 'This is test';
V4 := '1000';
V5 := V1 + V2 + V4; {实数 2235.5678}
I := V1; {I = 1 }
D := V2; {D = 1234.5678}
S := V3; {S = 'This is test'}
I := V4; {I = 1000}
S := V5; {S = '2235.5678'}
end;
虽然变动型态提供很大弹性,但它耗用更多记忆体,也比静态型态来得慢
(2)变动阵列
var
A : Variant; =>变动型态
I : Integer;
Begin
A := VarArrayCreate([0,4],VarOleStr);
For I := 0 to 4 Do A := 'AAAA';
VarArrayRedim( A , 9 ); =>加大
For I := 5 to 9 Do A := 'BBBB';
end;
※编辑: ForestBen (ForestBen) 于 2000-05-23 13:30:44 在 [202.117.91.39] 编辑本文


<marquee behavior=alternate>

终于漫长岁月 现已仿佛像流水</marquee>

<marquee behavior=alternate>
一花一世界,一叶一如来</marquee>