7.1.3 类的方法
与记录一样,在定义方法代码时,您需要使用类名作为前缀,并使用点符号来指明该方法属于哪个类(本例中为 TDate 类),如以下代码所示:
procedure TDate.SetValue(M, D, Y: Integer);
begin
FMonth := M;
FDay := D;
FYear := Y;
end;
function TDate.LeapYear: Boolean;
begin
// 在SysUtils.pas中调用IsLeapYear
Result := IsLeapYear(FYear);
end;
与大多数其他面向对象的编程语言不同,Object Pascal对方法的定义显式区分了过程和函数的不同,这取决于是否存在返回值。C++ 中的情况并非如此,在 C++ 中,单独定义的方法实现会是这样的:
// C++ method
void Date::setValue(int m, int d, int y)
{
month = m;
day = d;
year = y;
}