delphi 的抽象方法,该如何处理
delphi 的抽象方法
希望可以举个小列说明 谢谢大侠
------解决方案--------------------
Delphi--抽象方法、重载、覆盖。继承实例http://download.****.net/download/miaojun_25/1014042这个资源好像不错
------解决方案--------------------
可以看看TStrings类的GetCount,它就是一个抽象方法,它在类TStrings中的很多其它方法中都被调用到。
而子类TStringList覆盖了GetCount,其他用到了GetCount的方法都会调子类的GetCount。
如果TStrings类没有GetCount,那么凡是用到了GetCount的方法都要被子类所覆盖!!!
另一方面,抽象方法也在父类级别为所有子类提供了对外的“接口”。
比如TStrings类的Insert,如下代码:
var
s: TStrings;
begin
s := TStringList.Create;
s.Insert(0, 'abc');//如果TStrings没有Insert,这里是连编译都通不过的!!!
s.Free;
end;
------解决方案--------------------
关于class of,其实可以把他看作一个数据类型,就像Integer一样,只不过他是类的类型。声明一个此类型的变量,可以将类赋值给变量。
procedure TForm1.FormCreate(Sender: TObject);
type
TStringListClass = class of TStringList;
var
c: TStringListClass;
o: TStringList;
begin
c := TStringList;
o := c.Create;
o.Add('abc');
o.Free;
end;
------解决方案--------------------
delphi7里头的一段帮助:
To see how class-reference types are used, look at the declaration of the constructor for TCollection (in the Classes unit):
为了了解类引用类型是如何使用的,看看TCollection的构造器的声明(在Classes单元):
type TCollectionItemClass = class of TCollectionItem;
...
constructor Create(ItemClass: TCollectionItemClass);
This declaration says that to create a TCollection instance object, you must pass to the constructor the name of a class descending from TCollectionItem.
这个声明是说要想创建一个TCollection的实例对象,你必须把派生于TCollectionItem的一个类的名字传递给那个构造器。
Class-reference types are useful when you want to invoke a class method or virtual constructor on a class or object whose actual type is unknown at compile time.
类引用类型是有用的,当你想调用一个“类方法”或当你想调用在编译时期实际类型未知的一个类或对象的虚构造器的时候。
- Delphi(Pascal) code
delphi 面向对象的抽象方法的作用;如果不定义抽象方法会怎样呢? 还请说下 class of .. 类的类 的用法
希望可以举个小列说明 谢谢大侠
------解决方案--------------------
Delphi--抽象方法、重载、覆盖。继承实例http://download.****.net/download/miaojun_25/1014042这个资源好像不错
------解决方案--------------------
可以看看TStrings类的GetCount,它就是一个抽象方法,它在类TStrings中的很多其它方法中都被调用到。
而子类TStringList覆盖了GetCount,其他用到了GetCount的方法都会调子类的GetCount。
如果TStrings类没有GetCount,那么凡是用到了GetCount的方法都要被子类所覆盖!!!
另一方面,抽象方法也在父类级别为所有子类提供了对外的“接口”。
比如TStrings类的Insert,如下代码:
var
s: TStrings;
begin
s := TStringList.Create;
s.Insert(0, 'abc');//如果TStrings没有Insert,这里是连编译都通不过的!!!
s.Free;
end;
------解决方案--------------------
关于class of,其实可以把他看作一个数据类型,就像Integer一样,只不过他是类的类型。声明一个此类型的变量,可以将类赋值给变量。
procedure TForm1.FormCreate(Sender: TObject);
type
TStringListClass = class of TStringList;
var
c: TStringListClass;
o: TStringList;
begin
c := TStringList;
o := c.Create;
o.Add('abc');
o.Free;
end;
------解决方案--------------------
delphi7里头的一段帮助:
To see how class-reference types are used, look at the declaration of the constructor for TCollection (in the Classes unit):
为了了解类引用类型是如何使用的,看看TCollection的构造器的声明(在Classes单元):
type TCollectionItemClass = class of TCollectionItem;
...
constructor Create(ItemClass: TCollectionItemClass);
This declaration says that to create a TCollection instance object, you must pass to the constructor the name of a class descending from TCollectionItem.
这个声明是说要想创建一个TCollection的实例对象,你必须把派生于TCollectionItem的一个类的名字传递给那个构造器。
Class-reference types are useful when you want to invoke a class method or virtual constructor on a class or object whose actual type is unknown at compile time.
类引用类型是有用的,当你想调用一个“类方法”或当你想调用在编译时期实际类型未知的一个类或对象的虚构造器的时候。