简体   繁体   English

“var”对字段声明有什么区别?

[英]What difference does “var” have on field declarations?

What's the difference between the following declarations of S and F ? SF的以下声明之间有什么区别?

type
  TMyClass = class(TObject)
  private
    S: String;
  private var
    F: String;
  end;

The reason I'm asking is because I could use S the same way as F (without adding the var keyword). 我问的原因是因为我可以像F一样使用S (不添加var关键字)。

Are they treated differently? 他们的待遇不同吗?

There is no difference at all. 完全没有区别。 According to the documentation : 根据文件

The var keyword is optional. var关键字是可选的。 However, if it is not used, then all field declarations must occur before any property or method declarations. 但是,如果未使用它,则所有字段声明必须在任何属性或方法声明之前发生。 After any property or method declarations, the var may be used to introduce any additional field declarations. 在任何属性或方法声明之后,var可用于引入任何其他字段声明。

They are both Fields with the same visibility. 它们都是具有相同可见性的Fields。

The var keyword is needed sometimes, when you declare fields after other declarations (like constants, inner types or even after methods and properties). 当您在其他声明之后声明字段(如常量,内部类型甚至在方法和属性之后)时,有时需要var关键字。

For example: 例如:

type
  TMyClass = class
  private
    FSomeField: string; //<--- this is a field, here you don't need the var clause
    const
      SOME_CONSTANT = 1;
      OTHER_CONSTANT = 2;  
    var //<---- here you need the var clause to start declaring fields
      FSomeOtherField: string; 
  end;

The var field is needed in the second case ( FSomeOtherField ) to instruct the compiler now come a series of fields declarations, but in the first case it is not needed for historical reasons, because in the first Delphi versions you can just declare Fields and there was not support for nested types or constants. 在第二种情况下需要var字段( FSomeOtherField )来指示编译器现在进行一系列字段声明,但在第一种情况下由于历史原因不需要它,因为在第一个Delphi版本中你可以只声明Fields和那里不支持嵌套类型或常量。

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

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