【问题标题】:Overload constructors in VBScriptVBScript 中的重载构造函数
【发布时间】:2016-12-28 20:06:44
【问题描述】:

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

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)

这可能吗,还是必须在两个 setps 中创建和初始化对象?

【问题讨论】:

    标签: vbscript class


    【解决方案1】:

    只是稍微改变一下 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))
    

    虽然有点痛。

    【讨论】:

    • 现在是 2011 年,我都在谷歌上搜索过,并且很喜欢学习它。我喜欢重构旧的 VBScript,就像我喜欢让旧的 486 运行一样。我没有办法。
    • 您必须将(New Test) 括在括号中吗?你不能简单地做,New Test(Array())? =[
    • 我无法快速测试它,但据我所知,括号是必需的。没有类构造函数,因此New ClassName(arguments) 在 VBScript 中不是有效的语法。括号让解析器清楚地知道您正在将参数应用于New ClassName 的结果。将参数应用于对象会调用在类定义中标记为默认值的函数。
    • 太棒了。有一些小错误:内部函数中缺少“set”、end function 而不是 end public、end select 而不是 end case。
    • 如果显式调用Init(例如Set o = New Test.Init("hello world")),则不需要括号。不幸的是,对于默认函数技巧,这不起作用,所以是的——在这种情况下需要括号。
    【解决方案2】:

    您可以通过让 Init 函数返回对象本身来解决它...

    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
    

    【讨论】:

    • 如果我在 QTP 库中执行所有这些操作并将 Set o= 语句放入方法中,为什么我会收到 Set o = (New Test).Init("hello world") 的错误但 Set o = New Test : Set o=o.Init("hello world") 不会出现错误?
    【解决方案3】:

    您必须分两步完成。 VB 脚本不支持重载,因此您不能使用新参数修改默认构造函数。 Vb6 也是如此

    【讨论】:

      【解决方案4】:

      确实有点老套,但是当我在调用中需要可变参数时,我的一个参数会作为数组传入,即

      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)):
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 2015-03-30
        • 2012-09-24
        • 1970-01-01
        相关资源
        最近更新 更多