2009-01-19 92 views
15

我找到了一种在VBScript中扩展类的方法,但是有什么方法可以传入参数或重载构造函数吗?我目前使用Init函数初始化属性,但希望能够在创建对象时执行此操作。
这是我的示例类:在VBScript中重载构造函数

Class Test 
    Private strText 

    Public Property Get Text 
     Text = strText 
    End Property 

    Public Property Let Text(strIn) 
     strText = strIn 
    End Property 

    Private Sub Class_Initialize() 
     Init 
    End Sub 

    Private Sub Class_Terminate() 

    End Sub 

    Private Function Init 
     strText = "Start Text" 
    End Function  
End Class 

我创造它

Set objTest = New Test 

但希望做这样的事情

Set objTest = New Test(strInitText) 

这是可能的,或者确实对象必须要在两个设置中创建和初始化?

回答

22

只是对svinto的方法稍微改变...

Class Test 
    Private m_s 
    Public Default Function Init(s) 
    m_s = s 
    Set Init = Me 
    End Function 
    Public Function Hello() 
    Hello = m_s 
    End Function 
End Class 

Dim o : Set o = (New Test)("hello world") 

是我怎么做。可悲的是没有超载。

[编辑] 但如果你真的想你可以做这样的事情...

Class Test 
    Private m_s 
    Private m_i 

    Public Default Function Init(parameters) 
     Select Case UBound(parameters) 
      Case 0 
       Set Init = InitOneParam(parameters(0)) 
      Case 1 
       Set Init = InitTwoParam(parameters(0), parameters(1)) 
      Else Case 
       Set Init = Me 
     End Select 
    End Function 

    Private Function InitOneParam(parameter1) 
     If TypeName(parameter1) = "String" Then 
      m_s = parameter1 
     Else 
      m_i = parameter1 
     End If 
     Set InitOneParam = Me 
    End Function 

    Private Function InitTwoParam(parameter1, parameter2) 
     m_s = parameter1 
     m_i = parameter2 
     Set InitTwoParam = Me 
    End Function 
End Class 

这给建设者...

Test() 
Test(string) 
Test(integer) 
Test(string, integer) 

,你可以为拨打:

Dim o : Set o = (New Test)(Array()) 
Dim o : Set o = (New Test)(Array("Hello World")) 
Dim o : Set o = (New Test)(Array(1024)) 
Dim o : Set o = (New Test)(Array("Hello World", 1024)) 

虽然有点痛。

+2

这是2011年,我都为此搜索并享受它的学习。我喜欢重构旧的VBScript,就像我喜欢使旧的486运行一样。我不知道的方式。 – Chris 2011-02-22 18:04:26

2

你必须分两步做。 VB脚本不支持重载,所以你不能用新参数修改默认构造函数。这同样适用于VB6

6

你可以让你的初始化函数返回的对象本身解决它......

Class Test 
    Private m_s 
    Public Function Init(s) 
    m_s = s 
    Set Init = Me 
    End Function 
    Public Function Hello() 
    Hello = m_s 
    End Function 
End Class 

Dim o 
Set o = (New Test).Init("hello world") 
Echo o.Hello 
+0

为什么我得到`集合O =错误(新测试).Init(“hello world”)`但不适用于`Set o = New Test:Set o = o.Init(“hello world”)`如果我在QTP库中完成了所有这些操作,并将Set o =`语句转换成方法? – TheBlastOne 2016-04-28 12:00:42

1

有点hackish的,肯定的,但是当我需要可变参数的电话,我的参数我传递一个数组,一个即

Rem printf done poorly 
sub printf(fmt, args) 
    dim fp, vap: 
    dim outs: 
    dim fini: 
     fini = 0: 
     vap = 0: 
    while (not fini) 
    fp = index(fmt,"%"): 
    if (not(isNull(fp))) then 
     ' do something with %f, %s 
     select case(fp) 
     case 'c': 
      outs = outs & charparse(args(vap)): 
     case 's': 
      outs = outs & args(vap): 
     ' and so on. Quite incomplete but you get the idea. 
     end select 
     vap = vap + 1 
    end if 
    wend 
end sub 

printf("%s %d\n",array("Hello World", 42)):