2

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

如果有人可以提供帮助,那将不胜感激..

谢谢

4

3 回答 3

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. 可以从组件内部(这里:类)访问私有组件(变量、子;同样适用于函数和属性)
  2. 可以从外部访问公共组件(未显示但可能是合理的:也可以从内部使用公共组件)
  3. 未明确指定访问权限/模式 (?) (m_n2, s1) 默认为“公共”
  4. 对您的问题的简短回答:无 - 因为 (3)

“公共声明”的 VBScript Docs 说

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

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

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

于 2012-06-11T12:41:36.137 回答
3

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

Afunction或在私有的 QFL 中不能在另一个 QFL、模块或操作中使用,而公共的可以constvariable

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

' 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
于 2012-06-11T13:05:45.140 回答
2

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

于 2012-06-11T12:38:38.430 回答