我只是想知道“公共功能”和“功能”
之间的区别如果有人可以提供帮助,那将非常感谢..
谢谢
答案 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
你可以看到:
“公开声明”的VBScript文档说
声明公共变量并分配存储空间。声明,在一个 类块,公共变量。
和
所有程序都可以使用公共语句变量 脚本。
因此,可以研究/测试可访问性规则是否以及如何应用于(组合)脚本(源代码文件)。由于我对QTP处理多个源文件一无所知,所以我无法帮助你。
答案 1 :(得分:3)
除了Ekkehard.Horner的答案之外,在QTP中还可以将Qtp函数库(QFL)加载为.qfl或.vbs文件。
私密的QFL中的function
,const
或variable
不能在其他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会使该功能无法从类外部访问。