Originale-mail to me for new edition

 

Class methods

 

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.

 

Topic groups

 

See also

Class references: Overview

Self

 

 

译文

 

类方法

 

类方法就是作用于类而不是作用于对象的方法(除构造器外)。类方法的定义必需以保留字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的值。

 

主题组

 

相关主题

类引用:概述

Self