早期装订和晚期装订之间有什么区别?

时间:2020-03-05 18:39:31  来源:igfitidea点击:

早期和晚期绑定之间有什么区别?

解决方案

回答

直接来自http://word.mvps.org/fAQs/InterDev/EarlyvsLateBinding.htm

There are two ways to use Automation (or OLE Automation) to
  programmatically control another application.
  
  Late binding uses CreateObject to create and instance of the
  application object, which you can then control. For example, to create
  a new instance of Excel using late binding:

 Dim oXL As Object
 Set oXL = CreateObject("Excel.Application")

  
  On the other hand, to manipulate an existing instance of Excel (if
  Excel is already open) you would use GetObject (regardless whether
  you're using early or late binding):

 Dim oXL As Object
 Set oXL = GetObject(, "Excel.Application")

  
  To use early binding, you first need to set a reference in your
  project to the application you want to manipulate. In the VB Editor of
  any Office application, or in VB itself, you do this by selecting
  Tools + References, and selecting the application you want from the
  list (e.g. “Microsoft Excel 8.0 Object Library”).
  
  To create a new instance of Excel using early binding:

 Dim oXL As Excel.Application
 Set oXL = New Excel.Application

  
  In either case, incidentally, you can first try to get an existing
  instance of Excel, and if that returns an error, you can create a new
  instance in your error handler.

回答

在编译语言中,区别是明显的。

Java:

//early binding:
public create_a_foo(*args) {
 return new Foo(args)
}
my_foo = create_a_foo();

//late binding:
public create_something(Class klass, *args) {
  klass.new_instance(args)
}
my_foo = create_something(Foo);

在第一个示例中,编译器可以在编译时完成各种巧妙的工作。第二,我们只希望希望使用该方法的人做到负责任。 (当然,较新的JVM支持Class <?extended Foo> klass结构,这可以大大降低这种风险。)

另一个好处是,IDE可以热链接到类定义,因为它是在方法中声明的。对create_something(Foo)的调用可能与方法定义相去甚远,如果我们正在查看方法定义,则可能很高兴看到实现。

后期绑定的主要优点在于,它使控制反转等操作以及多态性和鸭子类型的某些其他用法(如果语言支持)更加容易。

回答

在解释语言中,差异稍微有些细微。

红宝石:

# early binding:
def create_a_foo(*args)
  Foo.new(*args)
end
my_foo = create_a_foo

# late binding:
def create_something(klass, *args)
  klass.new(*args)
end
my_foo = create_something(Foo)

因为Ruby通常不会被编译,所以没有编译器可以完成漂亮的前期工作。 JRuby的增长意味着这些天编译了更多的Ruby,从而使其行为更像Java。

IDE的问题仍然存在:像Eclipse这样的平台可以在我们对类定义进行硬编码时查找类定义,但是如果将其交给调用者则无法查找。

控制反转在Ruby中并不是很流行,可能是因为它具有极高的运行时灵活性,但是Rails充分利用了后期绑定来减少启动应用程序所需的配置量。

回答

简短的答案是,早期(或者静态)绑定是指编译时绑定,而后期(或者动态)绑定是指运行时绑定(例如,当我们使用反射时)。