ASP中Property Let、Property Set、Property Get语句的用法和区别

来源:互联网 发布:信誉好的单车淘宝店 编辑:程序博客网 时间:2024/04/23 18:14

Property Let 是用来设置属性值的。
Property Set 是用来设置对象引用的。
Property Get 是用来获取属性值的。

在这里假设name是类NewsClass的一个属性,调用为set News = new NewsClass

News.name = "news_class" 这里就调用了Public Property Let name这个name属性,let是让用户初始化name变量,一般用来初始化或重新设置类中的变量

set是类中的赋值方法,let和set的区别在于Let针对“变量”,而Set针对“对象、集合”,即Property Set过程是对“对象引用”赋值,Property Let过程只能用于属性赋值

获取属性值都用Property Get

下面是一个应用实例:

Class BookClass
private str_author

'/----class_initialize()是类的初始化事件,调用该类时就会自动调用这一个事件
private sub class_initialize()
str_author = "妫水山庄"
end sub

'/----class_terminate()是类的结束事件,只要一退出该类时就会触发该事件
private sub class_terminate()
response.write "<br/>BookClass结束了<br/>"
end sub

'/----定义类的属性,该属性是返回该类的作者号
public property get author
author = str_author
end property

public property let author(byval value)
str_author = value
end property

'/----该方法返回一个版本信息
public sub information()
response.write "<br/>coding by www.fzic.net.<br/>"
end sub

public property set authorObj(byval value)
end property
End Class

'/----以下是调用范例
set book = new BookClass
book.author = "妫水山庄信息" '调用了property let
set book.rs = new 对象 '调用了property set
response.write book.author '调用property get
book.information '调用了bookclass类中的information过程(方法)
set book = nothing '结束,释放对象

说明:
Property Let和Property Get后的变量名可以相同可以不相同;用Let声明的,调用的是classname.property = "接收值";用Get声明的,调用的是classname.property,一般用response.write classname.property读出它的值