Function QTP,VBScript,公共函数

Function QTP,VBScript,公共函数,function,vbscript,qtp,Function,Vbscript,Qtp,我只是想知道“公共职能”和“职能”有什么区别 如果有人能帮忙,我们将不胜感激 谢谢只有在一个类中使用公共和私有的问题才有意义。在VBScript类中,默认情况下函数是公共的,因此两者之间没有区别。使用Private会使函数从类外部无法访问。公共与私有可访问性的概念最好通过使用类来解释: Option Explicit Class cPubPrivDemo Public m_n1 Dim m_n2 Private m_n3 Private Sub Class_In

我只是想知道“公共职能”和“职能”有什么区别

如果有人能帮忙,我们将不胜感激


谢谢

只有在一个类中使用公共和私有的问题才有意义。在VBScript类中,默认情况下函数是公共的,因此两者之间没有区别。使用Private会使函数从类外部无法访问。

公共与私有可访问性的概念最好通过使用类来解释:

Option Explicit

Class cPubPrivDemo
  Public   m_n1
  Dim      m_n2
  Private  m_n3
  Private Sub Class_Initialize()
    m_n1 = 1
    m_n2 = 2
    m_n3 = 3
  End Sub
  Sub s1()
    WScript.Echo "s1 (Public by default) called"
  End Sub
  Public Sub s2()
    WScript.Echo "s2 (Public by keyword) called"
  End Sub
  Private Sub s3()
    WScript.Echo "s3 (Private by keyword) called"
  End Sub
  Public Sub s4()
    WScript.Echo "(public) s4 can call (private) s3 from inside the class"
    s3
  End Sub
End Class

Dim oPPD : Set oPPD = New cPubPrivDemo

WScript.Echo "Can access public member variables of oPPD:", oPPD.m_n1, oPPD.m_n2

WScript.Echo "No access to oPPD's private parts:"
Dim n3
On Error Resume Next
n_3 = oPPD.m_n3
WScript.Echo Err.Description
On Error GoTo 0

WScript.Echo "Can call public subs:"
oPPD.s1
oPPD.s2

WScript.Echo "Can't call private sub .s3:"
On Error Resume Next
oPPD.s3
WScript.Echo Err.Description
On Error GoTo 0

WScript.Echo "private sub s3 can be called from inside the class:"
oPPD.s4
从脚本的输出:

Can access public member variables of oPPD: 1 2
No access to oPPD's private parts:
Object doesn't support this property or method
Can call public subs:
s1 (Public by default) called
s2 (Public by keyword) called
Can't call private sub .s3:
Object doesn't support this property or method
private sub s3 can be called from inside the class:
(public) s4 can call (private) s3 from inside the class
s3 (Private by keyword) called
你可以看到:

  • 可以从组件内部(这里是:类)访问私有组件(变量、子;函数和属性也是如此)
  • 公共组件可以从外部访问(未显示,但可能是合理的:公共组件也可以从内部使用)
  • 未明确指定访问权限/模式(?)(m_n2,s1)默认为“公共”
  • 对你的问题的简短回答:没有——因为(3)
  • “公开声明”的VBScript文档说

    声明公共变量并分配存储空间。声明,在一个 类块,一个公共变量

    公共语句变量可用于所有中的所有过程 脚本


    因此,可以研究/测试可访问性规则是否以及如何应用于(组合的)脚本(源代码文件)。由于我对QTP处理多个源文件一无所知,因此我无法帮助您。

    除了Ekkehard.Horner的答案外,在QTP中,还可以将QTP函数库(QFL)加载为.QFL或.vbs文件

    QFL中私有的
    函数
    常量
    变量
    不能用于其他QFL、模块或动作,而公共QFL、模块或动作可以使用

    默认情况下,函数、常量和变量是公共的:

    ' All public:
    Dim MyVariable
    Public MyOtherVariable
    Const PI = 3.1415
    Function GetHello
        GetHello = "Hello"
    End Function
    Sub SayHello
        MsgBox GetHello
    End Sub
    
    ' All private:
    Private myPrivates
    Private Const HELLO = "HELLO!"
    Private Function getHelloToo
        getHelloToo = HELLO
    End Function
    Private Sub sayHelloToo
        MsgBox getHelloToo
    End Sub
    
    Class Dog
        Public Function Bark
            Print "Bark! Bark! Bark!"
        End Function
    End Class
    
    是的,类在模块中始终是私有的。您必须从函数中返回,以使其公开:

    ' Placed in the same module as Class Dog
    Public Function GiveMeADog
        Set GiveMeADog = new Dog
    End Function