inherited Create(...);
出现在方法定义中时,它调用继承的Create方法。
When inherited has no identifier after it, it refers to the inherited method with the same name as the enclosing method. In this case, inherited takes no explicit parameters, but passes to the inherited method the same parameters with which the enclosing method was called. For example,
当inherited后面没有标志符时,它指的是和当前方法同名的继承下来的方法。在这种情况下,inherited没有明确指定参数,但把当前使用的参数传给继承下来的方法。比如,
inherited;
经常出现在构造函数的实现中,它把相同的参数传给继承下来的构造函数。
- 101 -
Classes and objects
Self(Self变量)
在实现方法时,标志符Self引用方法所属的对象。比如,下面是Classes单元中TCollection的Add方法的实现:
function TCollection.Add: TCollectionItem;
begin
Result := FItemClass.Create(Self);
end;
Add方法调用FItemClass的Create方法,而FItemClass所属的类总是TCollectionItem的派生类,TCollectionItem.Create有一个TCollection类型的单一参数,
这以下面的代码表示:
var MyCollection: TCollection;
...
MyCollection.Add // MyCollection
Self在很多方面都有用,比如,一个在类中声明的成员(标志符)可能在方法中被重新声明,这种情况下,你可以使用Self.Identifier来访问原来的成员。
关于类方法中的Self,请参考Class methods。
Method binding(方法绑定)
Method binding: Overview(概述)
方法分为静态方法(默认)、虚方法和动态方法。虚方法和动态方法能被覆盖,它们可是是抽象的。当某个类类型的变量存储的是它的派生类时,它们的意义开始发挥作用,它们决定了调用方法时哪种实现被执行。
Static methods(静态方法)
方法默认是静态的。当调用一个静态方法时,类或对象被声明的类型决定了哪种实现被执行(编译时决定)。在下面的例子中,Draw方法是静态的。
type
TFigure = class
procedure Draw;
end;
TRectangle = class(TFigure)
procedure Draw;
end;
给定上面的声明,下面的代码演示了静态方法执行时的结果。在第
用的是一个TRectangle类型的对象,但却执行
TFigure。
var
Figure: TFigure;
Rectangle: TRectangle;
- 102 - 所以,Add把此时TCollection的实例传给它,TCollectionItem.Create方法 2个Figure.Draw中,变量TFigure中的Draw方法,因为Figure变量声明的类型是Figure引被传给
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-23665-74.html
这是国家给自己找面子呢