问题描述
我需要编写一些Delphi代码,但是我没有使用Delphi的经验。我见过人们写一些代码,称为 unit1
或 unit2
并使用其中的代码将其导入。因此,我可以将单元视为Java或C#中的类吗?
I need to write some Delphi code, but I have no prior experience with Delphi. I've seen people writing some code, known as unit1
or unit2
and import it using the code inside them. So, can I see the unit as a class in Java or C#?
推荐答案
否。单元是Delphi中的源代码文件。本质上,您可以将其视为一个范围与当前文件完全相同的命名空间。
No. A unit is a source code file in Delphi. You can essentially think of it as a namespace whose scope is exactly the same as the current file.
在一个单元内,您可以使用类型定义语法来定义类。看起来像这样:
Within a unit, you can define classes, with type definition syntax. It looks like this:
type
TMyClass = class(TParentClass)
private
//private members go here
protected
//protected members go here
public
//public members go here
end;
任何方法都在类型声明下声明,而不是内联,这使代码更易于阅读,因为您
Any methods are declared below the type declaration, not inline, which makes the code easier to read because you can see the composition of a class at a glance instead of having to wade through its implementation.
此外,每个单元都有两个主要部分,称为界面和实施。可以在任一部分中放置类型声明,但是在界面中,实现代码无效。这允许使用类似于Java或C#的公共和私有类的语言概念:在 interface 中声明的任何类型对使用此单元的其他单元( public)可见,而在中声明的任何类型>实施仅在同一单元内可见。
Furthermore, each unit has two main sections, called interface and implementation. A type declaration can be placed in either section, but implementing code is not valid in interface. This allows for a language concept similar to Java's or C#'s public and private classes: any type declared in interface is visible to other units that use this unit ("public"), while any type declared in implementation is visible only within the same unit.
这篇关于delphi中的单位与其他语言中的类是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!