在VBScript中重载构造函数 [英] Overload constructors in VBScript

查看:116
本文介绍了在VBScript中重载构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一种方法来扩展VBScript中的类,但是有什么方法传递参数或重载构造函数?我目前使用一个Init函数来初始化属性,但是希望能够在创建对象时这样做。

这是我的示例类:

  
类测试
私有strText

公共属性获取文本
Text = strText
End属性

公共属性Let Text(strIn)
strText = strIn
结束属性

Private Sub Class_Initialize()
Init
End Sub

Private Sub Class_Terminate()

End Sub

私人函数Init
strText =开始文本
结束函数
结束类

我创建它

  
Set objTest = New Test


$ b b

但是想要这样做

  
Set objTest = New Test(strInitText)

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

解决方案

只是稍微修改svinto的方法...

 类测试
私有m_s
公共默认函数初始化
m_s = s
设置Init = Me
结束函数
公共函数Hello()
Hello = m_s
结束函数
结束类

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

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




虽然如果你真的想要你可以这样做...

 类测试
私有m_s
私有m_i

公共默认函数Init(参数)
选择案例UBound(parameters)
情况0
设置Init = InitOneParam(参数(0))
情况1
设置Init = InitTwoParam(参数(0),参数(1))
case
设置Init = Me
结束选择
结束函数

私有函数InitOneParam(parameter1)
如果TypeName(parameter1)=String b $ b m_s = parameter1
Else
m_i = parameter1
结束如果
设置InitOneParam = Me
结束函数

私有函数InitTwoParam参数1,参数2)
m_s = parameter1
m_i = parameter2
设置InitTwoParam = Me
结束函数
结束类
/ pre>

其中给出了构造函数...

 
测试(字符串)
测试(整数)
测试(字符串,整数)


b $ b

你可以这样调用:

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

一个痛苦。


I found a way to extend classes in VBScript, but are there any ways to pass in parameters or overload the constructor? I am currently using an Init function to initialize the properties, but would like to be able to do this when I create the object.
This is my sample class:


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

And I create it


Set objTest = New Test

But would like to do something like this


Set objTest = New Test(strInitText)

Is this possible, or does the object have to be created and initialized in two setps?

解决方案

Just to alter slightly on svinto's method...

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

Is how I do it. Sadly no overloading though.

[edit] Though if you really wanted to you could do something like this...

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

Which gives the constructors...

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

which you can call as:

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

Bit of a pain though.

这篇关于在VBScript中重载构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆