📄 pascal.txt
字号:
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[I] := 'AAAA';
VarArrayRedim( A , 9 ); =>加大
For I := 5 to 9 Do A[I] := 'BBBB';
end;
4.变数宣告 (使用标记 : ) : 当宣告一个变数时,必须指明其型态
4.1 例子
X,Y,Z : Double;
I,J,K : Integer;
Dig :0..9;
C : Color;
Done,Error : Boolean; =>通常为一个byts
Operater : ( Plus , Munus , Times );
H1,H2 : Set Of Color;
Today : Date;
MyDim : Array[1..10,1..5] Of Double;
4.2 全域变数 : 在程序和函数之外宣告之变数
4.3 区域变数 : 在程序和函数之内宣告之变
4.3 变数初始化值 : MyInt : Integer = 123; {注意不可为区域变数指定初值}
4.4 限定词 :
4.4.1阵列索引 : MyDim[I][J] 同MyDim[I,J]
4.4.2栏位指示词 : MyRec.MyField {在一个with叙述中可以省略栏位指示 参考
4.4.3物件指示词 : form1.Button1.Caption
4.4.4指标和动态变数: p1^,@p1
4.5 变数型态转换
{例-1}
TYPE
TbyteRec = record
Fl1 , Fl2: Byte;
end;
VAR
W : Word;
B : Byte;
Begin
W := 256 * 10 + 128;
B := TbyteRec(W).Fl1; { B = 10}
*** word ─────>换成byte型态
B := TbyteRec(W).Fl2; { B = 128}
end;
{例-2}
With Sender as Mdbedit do =>代上一层型态
Text := 'This is test';
{例-3}
Mdbedit( Sender ).Text := 'This is test';
5.运算式语法
5.1 运算元及运算优先次序
(1)@,not 单运算子运算元 =>@变数住置
(2)*,/,div,mod,and,shl,shr,as 乘除
(3)+,-,or,xor 加减
(4)=,<>,<,>,<=,>=,in,is 关系
5.2 () 有优先运算评量, 由内外之运算优先
5.3 相同优先次序,则由左至右之运算优先
5.4 捷径评量(short-Circuit)
while (I <= Length(S)) and
(S[I] <> ' ') do
Inc(I)
捷径评量严格执行由左至右之运算,一旦运算结果己可知後便停止,可使得
一些原本不合法之运算架构变成可以运算
5.5 运算元分类
5.5.1 数学运算元 : +,-,*,/
div 整数相除之商
mod 整数相除之余数
{ I mod J = I - ( I div J) * J }
5.5.2 逻辑运算元 : not , And , Or , Xor , Shl , Shr
5.5.3 字串运算元 : +
S := 'aaaaa' + 'bbbb'; {s = aaaaabbbb}
5.5.4 集合运算元 : + , - , *
+ :连集 -:差集 *:交集
5.5.5 关系运算元:=,<>,<,>,<=,>=,in =>in 判断有效值
In : 成员
type
Month = ( Jan , Feb , Mar , Apr , May , Jun , Jul , Aug , Sep , Oct , Nov , Dec );
Spring = set of Feb..Apr..Mar;
Mstr = array[ 0 .. 11 ] of string[ 3 ];
const
CMstr : Mstr = ( 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' );
var
M : Month;
SSpring : Spring;
begin
SSpring := [ Feb..Apr ];
writeln( 'The Spring Month is : ' );
for M := Jan to Dec do
if M in SSpring then
writeln( CMstr[ Ord( M ) ] );
end;
5.5.6 类别运算元:is , As
if ( Sender is Mbutten ) and
( Mbutten( Sender ).tag <> 0 ) then ...;
with Sender As Mdbedit do
text := 'aaaaaa';
5.5.7 位置运算元:@
5.5.8 指标运算元:^
var
M , N : integer;
P1 , P2 : ^integer; => 指标
begin
M := 6;
P1 := @M; =>@位置
Label1.Caption := 'P1^ = ' + IntToStr( P1^ );
P2 := P1;
N := P2^;
Label2.Caption := 'N = ' + IntToStr( N );
end;
6.叙述语法
6.1 goto
label aa;
var i : integer;
begin
.
.
if (i = 0) then goto aa;
aa:begin
.
.
end;
end;
6.2 if
if ( x > 10 ) and {注意关系运算必须()}
( y > 5 ) then
z := x + y {注意 没;}
else z := 2 * ( x + y );
6.3 case
var
s : string;
r : integer;
begin
if s <> '' then
begin
case s[1] of =>S之第一个BYTE
'1' : r := 1; =>由小到大
'2' : r := 2;
'3' : r := 3;
else r := 4;
end;
end;
6.4 while
While ( i > 0 ) do
begin
x = X + I;
I = I - 1;
end;
while True do
begin
if ( i = 0 ) then break; =>break 中断
x = X + I;
I = I - 1;
end;
6.5 Repeat => 先做再检查
repeat
k:= i mod j;
i := j;
j := k;
until j = 0;
6.6 for
for i := 1 to 10 do
begin
if i = 5 then continue;
x := X + I;
end;
7.程式区块
7.1 单元结构
unit <单元名称>
interface {界面部份}
uses < list of units>;
{public declarations}
Implementation {实作部份}
uses < list of units>;
{Private declarations}
{implementation of procedure and function}
end.
7.2 单元叫用
uses < unitname list>;
unitname 会自动加上延伸档名 .dcu
其它单元己编释过,故不是 include source file 方式
若使用单元有使用到相同变数,必须采限定识别
unit.IdName
7.3 间接单元参考
program prog;
uses unit1;
const a = b;
begin
end.
unit unit1;
interface
uses unit2;
const b = c;
implementation
end.
unit unit2;
interface
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -