Deep BlueVBScriptWMIPHPC语言JavaScriptWindows API路由器Windows函数Python

VBS中Property Set和Property Let的区别

说好不玩VBS来着,但是今天有人问我,简单的写一下吧。

对于这个问题,《VBScript Programmers Reference》第215页说的很清楚:

Functionally, Property Let and Property Set procedures do the same thing. However, the Property Set procedure has two differences:

  • It makes it clearer that the property is an object-based property (any technique that makes the intent of the code more explicit is preferable over any other equally correct technique).
  • Code outside of your class must use the Set Object.Property = Object syntax in order to write to the property (also a good thing, because this is the typical way of doing things).

从功能上说,这两者的作用是一样的。但是Property Set有两点不同:第一,它说明了这个属性是一个与对象有关的属性;第二,在类的外面给属性赋值的时候必须使用Set关键字。

文件说明太抽象了,举个例子:

'Property Set
Class File
    Private m_fso
    Public Property Set fso(para_fso)
        m_fso = pata_fso
    End Property
End Class

Dim fso
Set fso = CreateObject("scripting.filesystemobject")
Set objFile = New File
'必须加上Set,否则报错
Set objfile.fso = fso
'Property Let
Class File
    Private m_fso
    Public Property Let fso(para_fso)
        m_fso = pata_fso
    End Property
End Class

Dim fso
Set fso = CreateObject("scripting.filesystemobject")
Set objFile = New File
'不能加上Set,否则报错
objfile.fso = fso

应该说明白了吧,继续Pythoning。


http://ken.gw.to/