class function或class procedure是什么意思
類(lèi)函數(shù)\類(lèi)過(guò)程.?? 它們是直接操作在類(lèi)上面(沒(méi)有實(shí)例化的對(duì)象)???
下面是Delphi??? Help??? 的描述???
????
?? A class method is a method (other than a constructor) that operates on classes instead of objects. The definition of a class method must begin with the reserved word?
?? class. For example,???
?? type???
??? TFigure = class???
??? public???
???? class function Supports(Operation: string): Boolean; virtual;???
???? class procedure GetInfo(var Info: TFigureInfo); virtual;???
???? ...???
??? end;???
?? The defining declaration of a class method must also begin with class. For example,???
?? class procedure TFigure.GetInfo(var Info: TFigureInfo);???
?? begin???
??? ...???
?? end;???
?? In the defining declaration of a class method, the identifier Self represents the class where the method is called (which could be a descendant of the class in which it is defined). If the method is called in the class C, then Self is of the type class of C. Thus you cannot use Self to access fields, properties, and normal (object) methods, but you can use it to call constructors and other class methods.???
?? A class method can be called through a class reference or an object reference. When it is called through an object reference, the class of the object becomes the value of Self.
類(lèi)方法(Class methods)是一類(lèi)特殊的方法,它們?cè)诼暶鲿r(shí)要以 class 開(kāi)頭:
??? type
???? TFigure = class
?? public
?? ...
class procedure GetInfo(var Info: TFigureInfo); virtual;
???? ...
??? end;
實(shí)現(xiàn)時(shí)也以 class 開(kāi)頭:
class procedure TFigure.GetInfo(var Info: TFigureInfo);
begin
...
end;
乍一看好象平時(shí)沒(méi)有遇到過(guò)這個(gè)東東,好象這個(gè)東東也沒(méi)有什么大作用,其實(shí)不然。比如我們有時(shí)為輸入密碼或其他常用數(shù)據(jù)專(zhuān)門(mén)做一個(gè) Form,但由于其代碼都在 Form 定義的 unit 里面,所以在使用時(shí)僅僅需要幾行代碼,比如:
??? with TfrmPassword.Create(nil) do
??? try
ShowModal;
finally
Free;
end;
??? 雖然這樣的代碼已經(jīng)很簡(jiǎn)潔,但如果寫(xiě)多了還是很討厭的。利用類(lèi)方法可以使其更簡(jiǎn)潔:
??? TfrmPassword = class(TForm)
?? ...
??? public {Public declarations }
??? class function Execute: TModalResult;
??? end;
?? ...
class function TfrmPassword.Execute: TModalResult;
begin
with TfrmPassword.Create(nil) do
??? try
????? Result :=ShowModal;
??? finally
??? Release; //注意此處必須為release不能為free!
??? end;
??? end;
然后只用一行 TfrmPassword.Execute; 即可直接完成調(diào)用!
轉(zhuǎn)載于:https://www.cnblogs.com/lzj1981/archive/2013/04/28/3049419.html
總結(jié)
以上是生活随笔為你收集整理的class function或class procedure是什么意思的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微信网名是什么
- 下一篇: C# 在用户控件中添加自定义事件