2009-01-19 185 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)):