我在这个 StackOverFlow 问题上找到了这段代码,即 Dictionary of Objects/Classes in VBScript。
我的问题是为什么这个“短”类和这个“长”类做同样的事情?为什么还要费心编写长班?短类版本可以与同一类中的其他方法一起使用吗?谢谢!
短班
Class employeeclass
Public first, last, salary
End Class
长班
Class employeeclass
Private m_first
Private m_last
Private m_salary
Public Property Get first
first = m_first
End Property
Public Property Let first(value)
m_first = value
End Property
Public Property Get last
last = m_last
End Property
Public Property Let last(value)
m_last = value
End Property
Public Property Get salary
salary = m_salary
End Property
Public Property Let salary(value)
m_salary = value
End Property
End Class
然而,短类的完整脚本,只需将短类替换为长类即可获得相同的结果。
Class employeeclass
Public first, last, salary
End Class
Dim employeedict: Set employeedict = CreateObject("Scripting.Dictionary")
Dim employee: Set employee = new employeeclass
With employee
.first = "John"
.last = "Doe"
.salary = 50000
End With
employeedict.Add "1", employee
Set employee = new employeeclass
With employee
.first = "Mary"
.last = "Jane"
.salary = 50000
End With
employeedict.Add "3", employee
Dim employeedetails: Set employeedetails = employeedict.Item("1")
WScript.Echo "Name: " & employeedetails.first & " " & employeedetails.last & " $" & employeedetails.salary
WScript.Echo employeedict.Item("3").first & " " & employeedict.Item("3").last & " makes $" & employeedict.Item("3").salary