简体   繁体   English

Delphi tstream:创建时的奇怪行为

[英]Delphi tstream: strange behaviour on create

I am new of Delphi. 我是Delphi的新手。 In the documentation of TStrem class, i read it is an abstract class. 在TStrem类的文档中,我读到它是一个抽象类。 So i think the compiler goes in error when i try to create it with 所以我认为当我尝试使用时,编译器会出错

stream := TStream.Create();

Why not? 为什么不?

The Delphi language doesn't really have any formal concept of abstract class. Delphi语言实际上没有任何抽象类的正式概念。

It is true that you can define a class to be abstract: 确实,您可以将类定义为抽象类:

type
  TMyClass = class abstract
  end;

But you can perfectly well instantiate this class. 但是你可以很好地实例化这个类。 In fact class abstract in Delphi is a feature used only by the long abandoned Delphi .net compiler. 事实上,Delphi中的class abstract是一个仅被长期放弃的Delphi .net编译器使用的特性。

A more useful definition of an abstract class is one that contains abstract methods. 抽象类的更有用的定义是包含abstract方法的定义。 If you attempt to instantiate such a class, then a compiler warning will be emitted. 如果您尝试实例化此类,则会发出编译器警告。 Those warnings can be promoted to errors by way of a compiler option, if you wish. 如果您愿意,可以通过编译器选项将这些警告提升为错误。

When the documentation refers to TStream as being abstract it in fact means that it is "conceptually" abstract. 当文档将TStream称为抽象​​时,它实际上意味着它在概念上是抽象的。 In fact it does not even have any abstract methods, so by my definition above it is not abstract. 实际上它甚至没有任何abstract方法,因此根据我上面的定义它并不是抽象的。

I'm really not sure why TStream does not contain abstract methods. 我真的不确定为什么TStream不包含抽象方法。 I would suggest that the GetSize , SetSize , Read , Write and Seek should really be declared abstract . 我建议将GetSizeSetSizeReadWriteSeek真正声明为abstract I suspect that if the class were being designed today then they would be declared abstract and likely they are not for historical reasons. 我怀疑,如果这个课程今天被设计,那么他们将被宣布为abstract并且可能不是出于历史原因。

Instantiating TStream is a very common mistake made by programmers less experienced in the Delphi RTL. 实例化TStream是Delphi RTL经验不足的程序员常犯的错误。 Once the mistake has been made a couple of times, the lesson is usually learnt. 一旦犯了几次错误,通常会吸取教训。 Unfortunately the system provides no easy way for this mistake to be flagged up. 不幸的是,系统没有提供标记这个错误的简单方法。 Each and every new programmer just has to learn the hard way. 每个新程序员都必须学习艰难的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM