个人认为用New关键字根本不算OO思想,用类创建类实例才符合, 没必要什么都仿C++,Pascal应有自己的特色。 指针在Pascal中根本没什么意义。你试试 TA = class public A1:integer; A2:integer; end;procedure Test(const a:TA); begin a.A1:=10; a.A2:=20; end; WriteLn('a1:',ta1.A1,' a2:',ta1.A2);
查了一下帮助: procedure New(var P: Pointer);DescriptionThe New procedure creates a new dynamic variable and sets a pointer variable to point to it. P is a variable of any pointer type. The size of the allocated memory block corresponds to the size of the type that P points to. The newly created variable can be referenced as P^. If there isn't enough memory available to allocate the dynamic variable, an EOutOfMemory exception is raised.When an application is finished using a dynamic variable created with New, it should dispose of the memory allocated for the variable using the Dispose standard procedure. type PListEntry = ^TListEntry; TListEntry = record Next: PListEntry; Text: string; Count: Integer; end; var List, P: PListEntry; begin ... New(P); P^.Next := List; P^.Text := 'Hello world'; P^.Count := 1; List := P; ... end;
做了一个类: TA=class function sum(x:integer;y:integer):integer; end; 调用: procedure TForm1.Button1Click(Sender: TObject); var p1:^TA; begin new(p1); showmessage(inttostr(p1.sum(2,3))); if assigned(p1) then dispose(p1); end; 调用成功!
在C++里,类就是类。而在PASCAL里,类是一个指针。所以如果你要在PASCAL里NEW一个类指针,那么就应该是建立一个指针的指针。如: C++ class a { ... } a pa; pa = new a();
GetMem () / FreeMem () //不过到了Delphi. NET时代可能borland就
太建议你用它了,因为从安全角度考虑的。
如果生成对象而分配空间,只有Create ()了,
每创建一个对象,当需要实例化的时候,对象自己
(其实就是系统自己搞的)会无声无息的为它准备
好了Create () / Destroy () / Free ().
没必要什么都仿C++,Pascal应有自己的特色。
指针在Pascal中根本没什么意义。你试试
TA = class
public
A1:integer;
A2:integer;
end;procedure Test(const a:TA);
begin
a.A1:=10;
a.A2:=20;
end; WriteLn('a1:',ta1.A1,' a2:',ta1.A2);
它用了New了吗?Create是怎么分配内存,初始化对象的。有高手谈谈就好了!:)
procedure New(var P: Pointer);DescriptionThe New procedure creates a new dynamic variable and sets a pointer variable to point to it. P is a variable of any pointer type. The size of the allocated memory block corresponds to the size of the type that P points to. The newly created variable can be referenced as P^. If there isn't enough memory available to allocate the dynamic variable, an EOutOfMemory exception is raised.When an application is finished using a dynamic variable created with New, it should dispose of the memory allocated for the variable using the Dispose standard procedure.
type PListEntry = ^TListEntry;
TListEntry = record
Next: PListEntry;
Text: string;
Count: Integer;
end;
var
List, P: PListEntry;
begin
...
New(P);
P^.Next := List;
P^.Text := 'Hello world';
P^.Count := 1;
List := P;
...
end;
TA=class
function sum(x:integer;y:integer):integer;
end;
调用:
procedure TForm1.Button1Click(Sender: TObject);
var p1:^TA;
begin
new(p1);
showmessage(inttostr(p1.sum(2,3)));
if assigned(p1) then
dispose(p1);
end;
调用成功!
C++
class a
{
...
}
a pa;
pa = new a();
Pascal
Ta = class
end;
pa : ^a;
new(pa)
a* pa
写太快了 :)
那么有没有谁能够通过以下方式建立起一个和VCL兼容的对象呢TSomeObj=objectend;
这种方式彻底不要VCLTSomeObj也不再是从TObject继承的了。