oracle早期绑定和晚期绑定,早期绑定和后期绑定 - Visual Basic | Microsoft Docs

早期绑定和后期绑定 (Visual Basic)Early and Late Binding (Visual Basic)

07/20/2015

本文内容

Visual Basic 编译器将 binding 在对象被分配给对象变量时执行调用的进程。The Visual Basic compiler performs a process called binding when an object is assigned to an object variable. 如果对象被分配给声明为特定对象类型的变量,就是 早期绑定 对象。An object is early bound when it is assigned to a variable declared to be of a specific object type. 借助早期绑定对象,编译器可以在应用程序执行前分配内存并执行其他优化。Early bound objects allow the compiler to allocate memory and perform other optimizations before an application executes. 例如,下面的代码片段将变量声明为类型 FileStream:For example, the following code fragment declares a variable to be of type FileStream:

' Create a variable to hold a new object.

Dim FS As System.IO.FileStream

' Assign a new object to the variable.

FS = New System.IO.FileStream("C:\tmp.txt",

System.IO.FileMode.Open)

由于 FileStream 是特定对象类型,因此分配给 FS 的实例就是早期绑定对象。Because FileStream is a specific object type, the instance assigned to FS is early bound.

相反,如果对象被分配给声明为 Object 类型的变量,就是 晚期绑定 对象。By contrast, an object is late bound when it is assigned to a variable declared to be of type Object. 虽然这种类型的对象可保留对任何对象的引用,但却没有早期绑定对象的诸多优点。Objects of this type can hold references to any object, but lack many of the advantages of early-bound objects. 例如,下面的代码片段将对象变量声明为保留 CreateObject 函数返回的对象:For example, the following code fragment declares an object variable to hold an object returned by the CreateObject function:

' To use this example, you must have Microsoft Excel installed on your computer.

' Compile with Option Strict Off to allow late binding.

Sub TestLateBinding()

Dim xlApp As Object

Dim xlBook As Object

Dim xlSheet As Object

xlApp = CreateObject("Excel.Application")

' Late bind an instance of an Excel workbook.

xlBook = xlApp.Workbooks.Add

' Late bind an instance of an Excel worksheet.

xlSheet = xlBook.Worksheets(1)

xlSheet.Activate()

' Show the application.

xlSheet.Application.Visible = True

' Place some text in the second row of the sheet.

xlSheet.Cells(2, 2) = "This is column B row 2"

End Sub

早期绑定的优点Advantages of Early Binding

应尽量使用早期绑定对象,因为这样编译器可以执行重要优化,从而大大提升应用程序的工作效率。You should use early-bound objects whenever possible, because they allow the compiler to make important optimizations that yield more efficient applications. 早期绑定对象的速度远超晚期绑定对象,并明确指出在使用的对象类型,使得代码更易于阅读和维护。Early-bound objects are significantly faster than late-bound objects and make your code easier to read and maintain by stating exactly what kind of objects are being used. 早期绑定的另一个优点是,它启用了一些有用的功能,如自动代码完成和动态帮助,因为 Visual Studio 集成开发环境 (IDE) 可以准确确定在编辑代码时所使用的对象的类型。Another advantage to early binding is that it enables useful features such as automatic code completion and Dynamic Help because the Visual Studio integrated development environment (IDE) can determine exactly what type of object you are working with as you edit the code. 早期绑定降低了运行时错误的数量和严重性,因为它允许编译器在编译程序时报告错误。Early binding reduces the number and severity of run-time errors because it allows the compiler to report errors when a program is compiled.

备注

晚期绑定只能用于访问声明为 Public 的类型成员。Late binding can only be used to access type members that are declared as Public. 访问声明为 Friend 或 Protected Friend 的成员会导致生成运行时错误。Accessing members declared as Friend or Protected Friend results in a run-time error.

请参阅See also

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值