0

大家好,我必须在 Classic ASP 中创建一个网站,我真的想做一个干净的设计,所以我会尝试将所有内容放在类和模型中以模拟 ViewModel 模式。

所以你能告诉我这种创建对象的方法是否很好,因为我会在任何地方使用它

Class User
Public Name
Public Adress
Public Title
Public Text 
Public Rights 'ArrayList of Right objects

    Private Sub Class_Initialize()
       Initialize
    End Sub

    Private Sub Class_Terminate()
       Dispose
    End Sub

   Private Sub Initialize()
       Name    = ""
       Adress  = ""
       Title   = ""
   Text    = ""
   Set Rights = Server.CreateObject("System.Collections.ArrayList") ' Arraylist .net
   End Sub

   Private Sub Dispose()      
   End Sub

End Class

Class Right

    Public Name

    Private Sub Class_Initialize()
        Initialize
    End Sub

   Private Sub Class_Terminate()
       Dispose
   End Sub

   Private Sub Initialize()
       Name    = ""
   End Sub

   Private Sub Dispose()      
   End Sub

End Class

然后我这样做是为了实例化对象:

Dim myUser 
Set myUser = New User

'Do some work

Set myUser = nothing 'Dispose the object 

欢迎任何想法,建议或更正。感谢帮助。

4

2 回答 2

2

VBScript does not support object instantiation with parameters, but you could add it like this:

Public Function [New User](name)
    Set [New User] = New User
    [New User].Name = name
End Function

Now you can create a user like:

Set myUser = [New User]("Arthur Dent")

Usefull when you have mandatory fields you want to set during initialization.

You can also make composites with this technique:

Public Function [New PowerUser](name)
    Set [New PowerUser] = New User
    [New PowerUser].Name = name
    Set [New PowerUser].Rights = RightsCollection("PowerUser")
End Function

Public Function [New GuestUser](name)
    Set [New GuestUser] = New User
    [New GuestUser].Name = name
    Set [New GuestUser].Rights = RightsCollection("Guest")
End Function
于 2012-09-04T10:57:17.843 回答
1

它认为不需要创建额外的函数“Initialize”和“Dispose”并分别在 Class_Initialize() 和 Class_Terminate() 中调用它。您可以将代码放在类构造函数和析构函数中。

其次,你公开了你的成员变量,如果你想让你的经典 asp 尽可能多地面向对象工作,你应该开始使用 getter 和 setter。因此像这样:

Private m_Name

Public Property Get Name()  
    Name = m_Name
End Property

Public Property Let Name(sName)
    m_Name = sName
End Property

使用 Rights 成员变量,您将获得更多乐趣 :)

于 2012-09-04T08:23:02.870 回答