日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用Delphi命名空间

發(fā)布時間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Delphi命名空间 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Delphi XE5教程8:使用Delphi命名空間?

// Project file declarations...//項目文件聲明…program MyCompany.ProjectX.ProgramY;// Unit source file declaration...//單元源文件聲明unit MyCompany.ProjectX.ProgramY.MyUnit1;

內(nèi)容源自Delphi XE5 UPDATE 2官方幫助《Delphi Reference》,本人水平有限,歡迎各位高人修正相關(guān)錯誤!

也歡迎各位加入到Delphi學(xué)習(xí)資料漢化中來,有興趣者可QQ:34484690@qq.com

Using Namespaces with Delphi

使用?Delphi?命名空間

Important: RAD Studio now supports a unit scope name or prefix in addition to the namespace or unit name. In order for a name to be considered fully qualified, the unit scope name must be included. For more details, see Unit Scope Names.

重要提示:RAD Studio現(xiàn)在支持為命名空間或單元名稱添加單元空間名稱或前綴。為了讓其被認(rèn)為是完全合格的名稱,單元空間名稱必須包括在內(nèi)。有關(guān)詳細(xì)信息,請參見單元空間名稱。

In Delphi, a unit is the basic container for types. In Delphi, a namespace is a container of Delphi units.

在Delphi中,一個單元是基本容器類型。在Delphi中,一個命名空間是Delphi單元的容器。

Unlike traditional Delphi units, namespaces can be nested to form a containment hierarchy. Nested namespaces provide a way to organize identifiers and types, and are used to disambiguate types with the same name. Since they are a container for Delphi units, namespaces may also be used to differentiate between units of the same name, that reside in different packages.

不同于傳統(tǒng)的Delphi單元,命名空間可以嵌套,從而形成一個層次結(jié)構(gòu)。嵌套的命名空間提供了一種組織標(biāo)識符和類型,以及用于對具有相同名稱的類型消除歧義。因為它們是Delphi單元的一個容器,命名空間也可以使用具有相同名稱的單元,用其駐留在不同的包中進(jìn)行區(qū)分。

For example, the class MyClass in MyNameSpace, is different from the class MyClass in YourNamespace.

例如,MyNameSpace命名空間中的MyClass,不同于YourNameSpace中的MyClass。

This topic describes the following:

本主題介紹了以下內(nèi)容:

?Project default namespaces, and namespace declaration.

項目的默認(rèn)命名空間,以及命名空間聲明。

?Namespace search scope.

命名空間的搜索范圍。

?Using namespaces in Delphi units.

?在Delphi單元中使用命名空間。

1 Declaring Namespaces

1?聲明命名空間

In RAD Studio, a project file (program, library, or package) implicitly introduces its own namespace, called the project default namespace. A unit may be a member of the project default namespace, or it may explicitly declare itself to be a member of a different namespace. In either case, a unit declares its namespace membership in its unit header. For example, consider the following explicit namespace declaration:

在RAD Studio中,項目文件(程序,庫或包)隱式地引入了自己的名字空間,項目調(diào)用默認(rèn)的命名空間。單元在它的單元標(biāo)題中聲明了它是哪個命名空間的成員。例如,下面命名空間的聲明:

unit MyCompany.MyWidgets.MyUnit;

First, notice that the parts of namespaces are separated by dots. Namespaces do not introduce new symbols for the identifiers between the dots; the dots are part of the unit name. The source file name for this example is MyCompany.MyWidgets.MyUnit.pas, and the compiled output file name is MyCompany.MyWidgets.MyUnit.dcu.

首先,請注意命名空間是使用.分隔各個部分。命名空間不在.之間引入新的標(biāo)識符;.作為單元名稱的一部分。這個例子的源文件的名稱是MyCompany.MyWidgets.MyUnit.pas,并且編譯輸出的文件是MyCompany.MyWidgets.MyUnit.dcu。

Second, notice that the dots imply the conceptual nesting, or containment, of one namespace within another. The example above declares the unit MyUnit to be a member of the MyWidgets namespace, which itself is contained in the MyCompany namespace. Again, it should be noted that this containment is for documentation purposes only.

其次,請注意.意味著命名空間中的另一個嵌套概念,或容器。上面的例子中聲明MyUnit單元是MyWidgets命名空間的一個成員,它本身是包含在MyCompany命名空間中的。再次,應(yīng)該注意的是,這只是容納文檔的目的。

A project default namespace declares a namespace for all of the units in the project. Consider the following declarations:

為項目中的所有units聲明一個默認(rèn)的命名空間,請考慮以下聲明:

Program MyCompany.Programs.MyProgram;Library MyCompany.Libs.MyLibrary;Package MyCompany.Packages.MyPackage;

These statements establish the project default namespace for the program, library, and package, respectively. The namespace is determined by removing the rightmost identifier (and dot) from the declaration.

這些語句為程序,庫和包分別建立了默認(rèn)的命名空間,命名空間從聲明的最右邊的標(biāo)識符(以及.)確定。

A unit that omits an explicit namespace is called a generic unit. A generic unit automatically becomes a member of the project default namespace. Given the preceding program declaration, the following unit declaration would cause the compiler to treat MyUnit as a member of the MyCompany.Programs namespace.

一個沒有顯式聲明的命名空間中的單元稱為常規(guī)單元。一個常規(guī)單元自動變?yōu)轫椖康哪J(rèn)命名空間的成員。在程序前面給出聲明,下列單元聲明會導(dǎo)致編譯器把MyUnit作為MyCompany.Programs命名空間的成員。

unit MyUnit;

The project default namespace does not affect the name of the Delphi source file for a generic unit. In the preceding example, the Delphi source file name would be MyUnit.pas. The same rule applies for the dcu file name. The resulting dcu file in the current example would be MyUnit.dcu.

項目的默認(rèn)命名空間不影響對一個常規(guī)單元的Delphi源文件的名稱。在前面的例子中,Delphi源文件的名稱將是MyUnit.pas。同樣的規(guī)則也適用于該DCU文件名。在當(dāng)前的例子中所得到的DCU文件將MyUnit.dcu。

Namespace strings are not case-sensitive. The compiler considers two namespaces that differ only in case to be equivalent. However, the compiler does preserve the case of a namespace, and will use the preserved casing in output file names, error messages, and RTTI unit identifiers. RTTI for class and type names will include the full namespace specification.

命名空間的字符串是不區(qū)分大小寫的。編譯器認(rèn)為兩個只有大小寫不同的命名空間是等價的。但是,編譯器會在這種情況下保留命名空間,并且將在輸出文件名,錯誤信息,和RTTI單元標(biāo)識符中保存。RTTI的類和類型名稱將包含完整命名空間。

1?????????Searching Namespaces

2???????搜索命名空間

A unit must declare the other units on which it depends. The compiler must search these units for identifiers. For units in explicit namespaces the search scope is already known, but for generic units, the compiler must establish a namespace search scope.

單元必須聲明它所依賴的其它單元。編譯器必須通過標(biāo)識符搜索這些單元。對于明確命名空間的單元,搜索空間是已知的,但對于常規(guī)單元,編譯器必須建立一個命名空間搜索范圍。

Consider the following unit and uses declarations:

考慮下面的單元并使用聲明:

unit MyCompany.ProjectX.ProgramY.MyUnit1;uses MyCompany.Libs.Unit2, Unit3, Unit4;

These declarations establish MyUnit1 as a member of the MyCompany.ProjectX.ProgramY namespace. MyUnit1 depends on three other units: MyCompany.Libs.Unit2, and the generic units, Unit3, and Unit4. The compiler can resolve identifier names in Unit2, since the uses clause specified the fully qualified unit name. To resolve identifier names in Unit3 and Unit4, the compiler must establish a namespace search order.

這個聲明建立MyUnit1作為MyCompany.ProjectX.ProgramY命名空間的一個成員。MyUnit1依賴于3個其它單元:MyCompany.Libs.Unit2和常規(guī)單元Unit3、Unit4。編譯器可以解析Unit2,因為uses子句指定了完全限制的單元名稱。要解析Unit3、Unit4的單元名稱,編譯器必須建立一個命名空間搜索順序。

1.1????Namespace search order

2.1?命名空間搜索順序

Search locations can come from three possible sources: compiler options, the project default namespace, and the current unit's namespace.

搜索位置可以來自三個可能的來源:編譯器選項,項目默認(rèn)的命名空間,和當(dāng)前單元的命名空間。

The compiler resolves identifier names in the following order:

編譯器將按以下順序解析標(biāo)識符名稱:

  • The current unit namespace (if any)
  • 當(dāng)前單元命名空間(如果有的話)

  • The project default namespace (if any)
  • 項目的默認(rèn)命名空間(如果有的話)

  • Namespaces specified by compiler options.
  • 由編譯器選項指定的命名空間。

    2.2 A namespace search example

    2.2?一個命名空間搜索例子

    The following example project and unit files demonstrate the namespace search order:

    以下示例項目和單元文件證明命名空間搜索順序:

    Given this program example, the compiler would search namespaces in the following order:

    基于這個程序的例子,編譯器會按如下順序搜索命名空間:

    1. MyCompany.ProjectX.ProgramY

    2. MyCompany.ProjectX

    3. Namespaces specified by compiler options. 由編譯器選項指定的命名空間。

    Note that if the current unit is generic (i.e. it does not have an explicit namespace declaration in its unit statement), then resolution begins with the project default namespace.

    請注意,如果當(dāng)前單元是常規(guī)的(即它沒有一個明確的命名空間聲明在其單元語句中),那么開始解析項目默認(rèn)命名空間。

    2?????????Using Namespaces

    3???????使用命名空間

    Delphi's?uses?clause brings a module into the context of the current unit. The?uses?clause must either refer to a module by its fully qualified name (i.e. including the full namespace specification), or by its generic name, thereby relying on the namespace resolution mechanisms to locate the unit.

    Delphi的uses子句帶來一個模塊到當(dāng)前單元的上下文。 uses子句必須引用它的全限定名(即包括完整的命名空間規(guī)范),或由它的常規(guī)名稱,從而依靠命名空間解析機制來定位單元要指定的模塊。

    3.1 Fully qualified unit names

    3.1?完全合規(guī)的單元名稱

    The following example demonstrates the uses clause with namespaces:

    下面示例演示了使用uses子句和命名空間:

    unit MyCompany.Libs.MyUnit1;uses MyCompany.Libs.Unit2, // Fully qualified name.完全合規(guī)的單元名稱UnitX; // Generic name.常規(guī)名稱

    Once a module has been brought into context, source code can refer to identifiers within that module either by the unqualified name, or by the fully qualified name (if necessary, to disambiguate identifiers with the same name in different units). The following Writeln statements are equivalent:

    一旦模塊被引入上下文,源代碼根據(jù)非限定名稱,或者完全合規(guī)的單元名稱去識別模塊內(nèi)的標(biāo)識符(如果有必要,要對不同單元內(nèi)的相同名稱去消除岐義)。下面Writeln語句是等價的:

    uses MyCompany.Libs.Unit2;beginWriteln(MyCompany.Libs.Unit2.SomeString);Writeln(SomeString);end.

    A fully qualified identifier must include the full namespace specification. In the preceding example, it would be an error to refer to SomeString using only a portion of the namespace:

    一個完全合格的標(biāo)識符必須包括完整的命名空間規(guī)范。在前面的例子中,使用命名空間中一部分去引用SomeString將會造成錯誤:

    Writeln(Unit2.SomeString); // ERROR錯誤!Writeln(Libs.Unit2.SomeString); // ERROR錯誤!Writeln(MyCompany.Libs.Unit2.SomeString); // Correct正確.Writeln(SomeString); // Correct正確.

    It is also an error to refer to only a portion of a namespace in the uses clause. There is no mechanism to import all units and symbols in a namespace. The following code does not import all units and symbols in the MyCompany namespace:

    在uses子句中只使用命名空間部分也是一個錯誤。沒有任何機制命名空間中導(dǎo)入所有單元和符號。下面的代碼不會在MyCompany的命名空間中導(dǎo)入所有單元和符號:

    uses MyCompany; // ERROR錯誤!


    This restriction also applies to the with-do statement. The following will produce a compiler error:

    這些限制也可以應(yīng)用于with-do語句。下面過程也將產(chǎn)生一個編譯錯誤:

    with MyCompany.Libs do??? // ERROR錯誤!

    3.2 Multi-unit Namespaces

    3.2?多單元命名空間

    Multiple units can belong to the same namespace, if the unit declarations refer to the same namespace. For example, one can create two files, unit1.pas and unit2.pas, with the following unit declarations:

    多個units單元可以同屬于相同的命名空間,如果unit單元聲明引入相同的命名空間。例如,共同創(chuàng)建兩個文件,unit1.pas 和 unit2.pas,會有以下的unit聲明:

    // in file 'unit1.pas'unit MyCompany.ProjectX.ProgramY.Unit1;// in file 'unit2.pas'unit MyCompany.ProjectX.ProgramY.Unit2;

    In this example, the namespace MyCompany.ProjectX.ProgramY logically contains all of the interface symbols from unit1.pas and unit2.pas.

    在這個例子中,命名空間MyCompany.ProjectX.ProgramY邏輯上包含所有的unit1.pas和unit2.pas接口符號。

    Symbol names in a namespace must be unique, across all units in the namespace. In the example above, it is an error for Unit1 and Unit2 to both define a global interface symbol named mySymbol.

    在所有命名空間中的單元的命名空間中的名稱必須是唯一的。在上面的例子中,它為unit1和unit2定義一個名為mySymbol一個全局性的接口符號是錯誤的。

    The individual units aggregated in a namespace are not available to source code unless the individual units are explicitly used in the file's uses clause. In other words, if a source file uses only the namespace, then fully qualified identifier expressions referring to a symbol in a unit in that namespace must use the namespace name, not just the name of the unit that defines that symbol.

    除非文件uses子句中顯式的使用獨立單元,聚集在一個命名空間中的個別單位不提供源代碼。換句話說,如果一個源文件使用唯一的命名空間,則完全限定標(biāo)識符表達(dá)式在unit單元中引用一個符號,必須使用命名空間的名稱。而不是只使用單元符號。

    A uses clause may refer to a namespace as well as individual units within that namespace. In this case, a fully qualified expression referring to a symbol from a specific unit listed in the uses clause may be referred to using the actual unit name or the fully-qualified name (including namespace and unit name) for the qualifier. The two forms of reference are identical and refer to the same symbol.

    Uses子句可以引用一個命名空間,以及在命名空間中的獨立單元。在這種情況下,一個完全合規(guī)的標(biāo)識符指的是從uses子句中列出的特定單元標(biāo)識符。可以是實際使用的單元名稱或完全限定名(包括命名空間和單元名稱)的限定符。這兩種形式的參考是相同的,并且指代相同的符號。

    Note: Explicitly using a unit in the uses clause will only work when you are compiling from source or dcu files. If the namespace units are compiled into an assembly and the assembly is referenced by the project instead of the individual units, then the source code that explicitly refers to a unit in the namespace will fail.

    注:顯式在uses子句使用單元將只能在你要編譯的源代碼或DCU文件工作。如果命名空間單元被編譯成一個組件和組件的項目,而不是個別單元引用,那么源代碼在命名空間中顯示引用一個單元將失敗。

    總結(jié)

    以上是生活随笔為你收集整理的使用Delphi命名空间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。