2012-06-11 63 views

回答

3

除了Ekkehard.Horner的回答,在QTP中,也可以將Qtp函數庫(QFL)加載爲.qfl或.vbs文件。

functionconst或在QFL即私人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

公共和私人問題只在課堂上使用時纔會出現。在VBScript類中,這些函數在默認情況下是公共的,因此兩者之間沒有區別。使用Private可以使課程無法從課堂外進入。

+0

感謝您的所有...... – Manaysah

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的文檔爲「公開聲明」說

聲明公共變量和分配存儲空間空間。在 類塊中聲明一個公共變量。

Public語句變量可用於所有 腳本的所有過程。

因此,可以研究/測試可訪問性規則是否以及如何應用於(組合)腳本(源代碼文件)。由於我對QTP處理多個源文件一無所知,因此我無法幫助您。

+0

謝謝...這是有益的 – Manaysah