在TObject类定义中有若干型如如下的函数:class function classname:shortstring;
class function .....
等等...请问在函数前加class是什么意思?有何用处?
为什么过程前没有class ?过程procedure和属性property有什么不同之处?
class function .....
等等...请问在函数前加class是什么意思?有何用处?
为什么过程前没有class ?过程procedure和属性property有什么不同之处?
解决方案 »
- 请问一个问题,滚动条如何设置为平滑一点?
- 急啊自动弹出窗体问题(时间倒计时来控制)
- 如何保证多数据库表间的数据一致性
- 如何在程序中给FTP(serverU)新增加一个帐户?
- ListView的简单问题(在线等待)
- 使用string型的变量,怎样用在tmemorystream.read(s, length(s))里面?
- 几个windowsAPI的强烈问题,想必大家都遇到过
- 请大家帮我看段insert into的程序是什么问题?
- 关于界面设置的问题,帮帮忙~~
- 我的COM问题!!!请大家帮忙
- paradox数据库的问题
- ++++++++++++++=谁有access的解密软件,能否给我一个,++++++++++
在一个方法前加上关键字class,使得方法向其他通常的过程和函数一样调用而不需要生成一个包含这个方法的类的实例,这个功能是从C++的static函数借鉴来的。要小心,不要让一个类方法依赖于任何实例信息,否则编译时将出错。
这么说可能不太清楚看object里的定义吧类方法类方法就是作用于类而不是作用于对象的方法(除构造器外)。类方法的定义必需以保留字class开始。例如,type TFigure = class public class function Supports(Operation: string): Boolean; virtual; class procedure GetInfo(var Info: TFigureInfo); virtual; ... end;类方法的定义声明也必需以保留字class开始。例如,class procedure TFigure.GetInfo(var Info: TFigureInfo);begin ...end;在类方法的定义声明中,标识符Self表示方法被调用处的类(可以是方法定义所在的类的后裔类)。如果在类C中调用类方法,那么Self就是类C的类型。因此,这时不能用Self对域、属性和一般的对象方法进行访问,但可以用它调用构造器和其他的类方法。类方法可以通过类引用或对象引用被调用。当类方法通过对象引用被调用时,对象的类成为Self的值。
比如: caption := TButton.ClassName;
type TMyClass=class
class procedure HeyGuys;
end;
implementationclass procedure TMyClass.HeyGuys;
begin
ShowMessage('Hey Guys!');
end;当你使用该成员函数的时候,你有两种方法来调用1。直接调用静态方法 TMyClass.HeyGuys;
2。调用实例化后对象的该方法(实际上还是调用的TMyClass.HeyGuys)。
var myClass:TMyClass;
begin
myClass:=TMyClass.Create;
myClass.HeyGuys;
FreeAndNil(myClass);
end;
"类函数,通过类名直接调用,不用通过对象调用"
TO thh820630(空吻)
"是类函数 只能由类调用 不能由对象调用"类函数也可以由对象调用。
TO myling(阿德)
能否将类方法中的self再解释更清楚一下呢?
C++中不能在类方法中访问this,而
OP中却可以访问self,但不能访问属性、非类方法等。
如在类方法中使用integer(self);的表达式。
我感到有些困惑,请不吝执教。
他们都是类的成员,所不同的是procedure是类的方法,property 是类的属性,我想属性和方法你还能分辨清楚吧!好了,我觉得你应该多看看一些object pascal的基础书籍!
类函数,
在一个方法前加上关键字class,使得方法向其他通常的过程和函数一样调用而不需要生成一个包含这个方法的类的实例,这个功能是从C++的static函数借鉴来的。
-------------------------------------------------------------
要小心,不要让一个类方法依赖于任何实例信息,否则编译时将出错。
-------------------------------------------------------------
我觉得这句话,最关键。也是Class方法的特点。
所以integer(self),实际上就跟c++中(int)this一样的。将指针转换为整形。
with TfrmPassword.Create(nil) do
try
Result := ShowModal;
finally
Release; //注意此处必须为release不能为free!<-----此处为何不能用free?
end;
请解释一下?
TO myling(阿德):
"在类方法的定义声明中,标识符Self表示方法被调用处的类(可以是方法定义所在的类的后裔类)"如何理解?
如果定义类以及类函数:
TA = class
class function f1: integer;
end;class function TA.f1: integer;
begin
result := Integer(self);
end;
定义变量t1: TA;
调用TA.f1时得到的结果并不等于(PInteger(integer(t1))^),后者是类的VMT
type TMyClass=class
private
field1:string;
field2:integer;
public
class procedure foo;
end;
class procedure TMyCLass.foo;
var field3:integer;
begin
field1:='test';//编译错误
field2:=2;//编译错误
field3:=3;//这里可以通过编译,因为这个变量的生存期在foo里面,而不是存在类的实例中。
end;
在这里面,是不允许通过编译的,因为这个类可能根本就没有被实例化,哪里来的field1,field2呢?或者由多个实例,你知道你访问的是哪个实例的字段呢?所以干脆不允许静态方法访问成员字段。
Destroys an object and frees its associated memory, if necessary.Delphi syntax:procedure Free;C++ syntax:__fastcall Free();DescriptionUse Free to destroy an object. Free automatically calls the destructor if the object reference is not nil. Any object instantiated at runtime that does not have an owner should be destroyed by a call to Free so that it can be properly disposed of and its memory released. Unlike Destroy, Free is successful even if the object is nil; so if the object was never initialized, Free won抰 result in an error.When you call Free for a component, it calls Free for all components that it owns梩hat is, all components in its component list. Since a form owns all the controls and other components that are created on it in design mode, those components are automatically freed when the form is freed. By default, all forms are owned by the Application object; when the application terminates, it frees the Application object, which frees all forms. For objects that are not components, or for components created with a nil owner, be sure to call Free after you are finished with them; otherwise the allocated memory will not be usable until after the application terminates.Warning: Never explicitly free a component within one of its own event handlers or the event handler of a component it owns or contains. For example, don抰 free a button, or the form that owns the button, in its OnClick event handler.To free a form, call its Release method, which destroys the form and releases the memory allocated for it after all its event handlers and those of the components it contains are through executing.
不过都让Eastunfail(恶鱼杀手)说完了 :-)
看这个: TA = class
class procedure f1;
class function f2: string;
end;
Tb=class (Ta)
end;class procedure TA.f1;
begin
showmessage(f2);
end;class function TA.f2: string;
begin
Result:=Self.ClassName;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ta.f1; //ta
tb.f1; //tb
end;
在定义类方法的时候,使用保留字class对过程或函数进行说明。在定义类方法的时候,标识符Self将代表类方法被调用的类。不可以使用Self访问类的字段、属性和普通方法,但是可以通过Self调用构造函数和其他类方法。
Eastunfail(恶鱼杀手) 和 阿德都说完了,
现在只有学习的份了
比如说:
type
A = Class(TObject)
private
class function Func():Res;
protet
public
函数Func不需要A类的实例就可以调用
调用方法为: A.Func;
我所说的"调用TA.f1时得到的结果并不等于(PInteger(integer(t1))^),后者是类的VMT"有误,经过验证后,两者是相等的。谢谢纠正。
zhxfzhxf1(zhxfzhxf1)互相学习:-)
最经典的代表就是CREATE 函数.
但是在类函数中不可以引用类的一些方法属性