QTP,VBScript,公共功能

时间:2012-06-11 11:19:40

标签: function vbscript qtp

我只是想知道“公共功能”和“功能”

之间的区别

如果有人可以提供帮助,那将非常感谢..

谢谢

3 个答案:

答案 0 :(得分:3)

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

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
你可以看到:

  1. 可以从组件内部访问私有组件(变量,sub;功能和属性相同)(此处为:class)
  2. 可以从外部访问公共组件(未显示但可能看似合理:公众也可以从内部使用)
  3. 未明确指定访问权限/模式(?)(m_n2,s1)默认为“公共”
  4. 对你的问题的简短回答:无 - 因为(3)
  5. “公开声明”的VBScript文档说

      

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

      

    所有程序都可以使用公共语句变量   脚本。

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

答案 1 :(得分:3)

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

私密的QFL中的functionconstvariable不能在其他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

答案 2 :(得分:2)

公共和私人问题只在课堂上使用时才有意义。在VBScript类中,函数默认是公共的,因此两者之间没有区别。使用Private会使该功能无法从类外部访问。