Delphiキーワードの詳細2
20272 ワード
in
//In . .
type
TCol = (cA,cB,cC);
TCols = set of TCol;
var
Cols: TCols;
begin
Cols := [cA,cB];
if cA in Cols then
ShowMessage('cA in Cols')
else
ShowMessage('cA not in Cols');
end;
//In , .
Uses
Unit1 in 'Unit1.pas';
//In For , .
var
s: string;
sl: TStringList;
begin
...
for s In sl do
begin
ShowMessage(s);
end;
end;
index
//Index , (Get,Set) .
type
TForm1 = class(TForm)
private
function GetInfo(const Index: Integer): Longint;
procedure SetInfo(const Index: Integer; const Value: Longint);
public
property iLeft:Longint index 0 read GetInfo write SetInfo;
property iTop:Longint index 1 read GetInfo write SetInfo;
property iWidth:Longint index 2 read GetInfo write SetInfo;
property iHeight:Longint index 3 read GetInfo write SetInfo;
end;
function TForm1.GetInfo(const Index: Integer): Longint;
begin
case Index of
0: result := self.Left;
1: Result := self.Top;
2: result := self.Width;
3: result := self.Height;
end;
end;
//Index , :
property Selected[Index: Integer]: Boolean read GetSelected write SetSelected;
inherited
//Inherited .
type
TDemo = class(TComponent)
public
constructor Create(AOwner: TComponent); override;
end;
constructor TDemo.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
// , .
inherited Create(AOwner);
// :
Inherited;
initialization
//initialization ,
// , .
//initialization OLE .
initialization
ActiveX.OleInitialize(nil);
finalization
ActiveX.OleUninitialize;
inline
//InLine Asm assembler ,
// . .
function IntToStr(Value: Integer): string;
asm
InLine;
PUSH ESI
MOV ESI, ESP
SUB ESP, 16
xor ECX, ECX
PUSH EDX
xor EDX, EDX
CALL CvtInt
MOV EDX, ESI
POP EAX
CALL System.@LStrFromPCharLen
ADD ESP, 16
POP ESI
end;
interface
//Interface , :
//Unit...Interface...implementation...end.
// , Interface .
// Interface , , .
Interface
uses frmAbout;
var
FAbout: TFormAbout;
begin
FAbout := TFormAbout.Create(Self);
FAbout.Show;
end;
// Interface .
//Interface .
type
IMalloc = interface(IInterface)
['{00000002-0000-0000-C000-000000000046}']
function Alloc(Size: Integer): Pointer; stdcall;
function Realloc(P: Pointer; Size: Integer): Pointer; stdcall;
procedure Free(P: Pointer); stdcall;
function GetSize(P: Pointer): Integer; stdcall;
function DidAlloc(P: Pointer): Integer; stdcall;
procedure HeapMinimize; stdcall;
end;
is
//Is , , "As" .
var
Comp: TComponent;
begin
...
if Comp Is TEdit then
(Comp as TEdit).Text := 'Edit';
end;
label
//label , Goto , .
var
a,b: Integer;
label
X,Y;
begin
if a > b then
goto X
else
goto Y;
X:
WriteLn('a>b');
Y:
WriteLn('b>a');
end;
library
//Library . DLL , .
library Editors;
uses EdInit, EdInOut, EdFormat, EdPrint;
exports
InitEditors,
doneEditors name done,
InsertText name Insert,
DeleteSelection name Delete,
FormatSelection,
PrintSelection name Print,
SetErrorHandler;
begin
InitLibrary;
end.
message
//Message ,
// Message , , .
procedure Refresh(var Msg: TMessageRecordtype); message ID_REFRESH;
procedure Refresh(var Msg: TMessageRecordtype);
begin
if Chr(Msg.Code) = #13 then
...
else
inherited;
end;
// , Message , .
mod
//Mod , . Mod , .
var
a,b,c: Integer;
begin
a := 20; b := 3;
c := a mod b; {2}
end;
name
//Name ,
// , Name , .
// , , Name .
function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer): Integer;
stdcall; external 'user32.dll' name 'MessageBoxA';
near
//Near , .
// dll . .
function Add(a,b: Integer): Integer; near;
// Demo.exe, , :
function Add(a,b: Integer): Integer; stdcall; external 'Demo.exe';
nil
//Nil , .
while Node <> nil do
begin
ListBox1.Items.Add(Node.Text);
Node := Node.GetNext;
end;
nodefault
//NoDefault , .
type
TClassA = class
private
fValue: Integer;
published
property Value: Integer read fValue write fValue default 0;
end;
TClassB = class(TClassA)
published
property Value:Integer read fValue write fValue nodefault;
end;
// , TClassA Value 0,
//TClassB TClassA, , NoDefault
not
//Not , . :
if a > b then
// :
if not(a < b) then
//Not Boolean
procedure Button1Click(Sender: TObject);
begin
StatusBar1.Visible := not StatusBar1.Visible;
end;
object
//Object , , .Object Object .
// .
type
ODemoA = object
end;
ODemoB = object(ODemoA)
end;
//Object , :
type
TMyFun = function(i: Integer): Integer of Object;
TMyProc = procedure(s: string) of object;
// object , .
of
//Of .Of Case, Class, Array, File, Set, Object .
//Case :
case Tag Of
0: Result := 'a';
1: Result := 'b';
end;
//Class :
type
TDemo = class of TComponent;
//Array :
var
MyInt: array of Integer;
//File :
var
MyFile: file of Byte;
//Set :
type
TCol = (cA,cB,cC);
TCols = set of TCol;
//Object :
type
MyFun = function(I: Integer): Integer of Object;
on
//On , , .
try
i := StrToInt(s);
except
on E: exception do
ShowMessage(E.Message);
end;
or
// 、
if (a>0) or (b>0) then
// 、
var
a,b,c: Integer;
begin
c := (a or b);
end;
// Or , Or ,
// Or, Or
:
if a>0 or b>0 then
// :
if a>(0 or b)>0 then
//
if (a>0) or (b>0) then
// , ,
// a>b>c , Delphi
// Or , .
// , Or .
out
//Out , ,
// Out .
//Out var , Out , var .
procedure X(out i: Integer; out s: string);
begin
i := i * 2;
s := s + 'abc';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
s: string;
begin
i := 20;
s := 'xxx';
X(i,s);
end;
overload
//Overload , ,
// , , .
function X(i: Integer): string; overload;
function X(s: string): string; overload;
// , , overload ,
// .
type
TDemo = class(TComponent)
public
procedure CreateWnd(AOwner: TWinControl); overload;
end;
// , :
procedure CreateWnd; { }
procedure CreateWnd(AOwner: TWinControl); { }
// CreateWnd .
// , .
override
//Override Virtual Dynamic .
// , .
procedure Create(AOwner: TComponent); override;
//Override , .
type
TClassA = class
procedure X; virtual;
end;
TClassB = class(TClassA)
procedure X; override;
end;
// , :
procedure X; { }
// :
procedure X; { , }
// Virtual Dynamic ,
// , Reintroduce .
package
//Package .
// BPL , Delphi , .
package DATAX;
requires
rtl,
clx;
contains
MyUnit in 'C:/MyProject/MyUnit.pas';
end.
packed
//Packed , .
type
TPerson = packed Record
PName: string[32];
PAge: Integer;
end;
MyArray: packed array of PChar;
pascal
//Pascal ,
// Pascal , ,
// . .
function X(i: Integer): Integer; Pascal;
begin
Result := i * 2;
end;
private
//Private , Private .
procedure
//Procedure
procedure X(i: Integer);
//
type
TProc = procedure(i: Integer) of object;
// , , , .
program
//Program . exe ,
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' ;
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
property
//Property , ,
// published , .
type
TDemo = class
Private
fValue: Integr;
Published
property Value: Integer read fValue write fValue;
end;
// , published Property
type
TOnTextChange=procedure (Sender: TObject) of object;
TDemo = class
private
fEvent: TOnTexChange;
published
property OntextChange: TOnTextChange read fEvent write fEvent;
end;
protected
//Protected , Protected .
public
//Public , Public .
published
//Published .
// Published RTTI
// Published .
raise
//Raise ,
// , , Raise .
function GetString(i: Integer): string;
begin
if i < 0 then
raise exception.Create('Integer Cannot smaller than 0');
Result := IntToStr(i);
end;
// ,
try
i := StrToInt(s);
except
on E: exception do
raise exception.Create(E.Message);
end;
read
//Read .
private
fValue: Integer;
published
property Value: Integer read fValue;
// Value fValue .
readonly
//ReadOnly .
property ReadOnly;
// ReadOnly True , , .
record
//Record ,
// , .
type
TPerson = record
PName: string[32];
PAge: Integer;
end;
register
//Register , . .
function Add(a,b: Integer): Integer; Register; Register
// IDE .
procedure Register;
begin
RegisterComponents('Sample', [TDemo]);
end;
reintroduce
//Reintroduce , ,
// , , Reintroduce .
// Virtual Dynamic , Override .
type
TClassA = class
procedure X;
end;
TClassB = class(TClassA)
procedure X; reintroduce;
end;
TClassC = class(TClassB)
procedure X(i: Integer); reintroduce;
end;
repeat
//repeat repeat ,
// , .repeat Until .
i := 0;
repeat
sum := sum + i;
Inc(i);
until(i >= 100);
requires
//Requires Package . Requires , .
package DATAX;
requires
rtl,
clx;
end.
resourcestring
//ResourceString , .
ResourceString
CreateError = 'Cannot create file %s';
OpenError = 'Cannot open file %s';
LineTooLong = 'Line too long';
ProductName = 'Borland Rocks';
SomeResourceString = SomeTrueConstant;
safecall
//Safecall , COM .
// , Safecall COM .
procedure X(s: WideString); safecall;
// :
procedure X(s: PAnsiString);
set
//Set , , in .
type
TCol = (cA,cB,cC);
TCols = set of TCol;
//
var
Cols: Tcols;
begin
Cols := Cols + [cA,cB];
end;
shl
//SHL , 2
var
x: Integer;
begin
X := 2 shl 3; {16}
end;
shr
//SHL , 2
var
x: Integer;
begin
X := 16 shr 2; {4}
end;
stdcall
//Stdcall , .
//Stdcall .
, :
Library Demo;
function X(i: Integer): Integer; stdcall;
begin
Result := i * 2;
end;
exports
X;
begin
end.
// :
function X(i: Integer): Integer; stdcall; external 'Demo.dll';
// , Stdcall , , .
stored
//Stored , True, .
property Value: string read fValue write fValue stored True;
string
//String , .
var
Str: string;
then
//Then If , If , Then .
var
a,b: Integer;
begin
if a > b then
WriteLn('a')
else
WriteLn('b');
end;
threadvar
//Threadvar ,
// Threadvar , .
threadvar S: AnsiString;
S := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
S := '';
//S := ''; S .
to
//To For , .
for i := 10 to 100 do
ListBox1.Items.Add(IntToStr(i));
// For , To , DownTo .
try
//try , , try , .
try
i := StrToInt(s);
except
ShowMessage('Error');
end;
type
//Type , Type , .
type
TDemo = class
end;
//type .
type
TCol = (cA,cB,cC);
TInt = Integer;
unit
//Unit , Unit...Interface...implementation...end.
Unit Unit1;
Interface
uses Classes;
implementation
end.
// Unit .
until
//Until repeat ,
// , .Until repeat .
i := 0;
repeat
sum := sum + i;
Inc(i);
until(i >= 100);
uses
//Uses , .
//Uses .
Interface
uses Classes;
Implemention
uses frmAbout;
var
//var , var .
var
i: Integer;
s: string;
//var
function X(var i: Integer): Integer;
// i , , .
varargs
//varArgs , Cdecl , .
function printf(Format: PChar): Integer; cdecl; varargs;
// C++ Printf , .
virtual
//Virtual ,
// , ( Dynamic).
procedure X(i: Integer); virtual;
while
//While While , , .
i := 0;
while i < 100 do
begin
sum := sum + i;
Inc(i);
end;
with
//With , , .
with Form1.Memo1.Lines do
begin
Clear;
Append('abc');
Append('def');
SaveToFile('C:/demo.txt');
end;
// With , :
Form1.Memo1.Lines.Clear;
Form1.Memo1.Lines.Append('abc');
Form1.Memo1.Lines.Append('def');
Form1.Memo1.Lines.SaveToFile('C:/demo.txt');
write
//Write .
private
fValue: Integer;
published
property Value: Integer write fValue;
// Value fValue .
writeonly
//writeonly .
property writeonly;
// writeonly True , , .
xor
//Xor , , False, True.
var
a,b: Integer;
begin
a := 2; b := 3;
if a xor b then
WriteLn('a xor b')
else
WriteLn('a not xor b');
end;
//Xor
WriteLn(IntToStr(3 xor 5)); {6}
原稿ソースhttp://mydelphi.5d6d.com/thread-252-1-1.html