我又来问基础问题了,public Property let,get和直接用public什么区别?
看别人写的类的例子,里面经常出现类似语句:
Public Property Get Version
Version="xxxxxx"
End Property
那这样跟直接:
public version="xxxxx"
有什么区别?
还有的用:
Public Property Let aaa(str)
a=str
End Property
Public Property Get aa
aa=a
End Property
这种情况a的值是手动输入进来的,如果不是手动输入而是固定值的话,那根直接:
public aa=a 又有什么区别啊?
小弟新人求解
[解决办法]
在 Class 块中,成员通过相应的声明语句被声明为 Private(私有成员,只能在类内部调用) 或 Public(公有成员,能在类内外部调用)。被声明为 Private 的将只在 Class 块内是可见的。被声明为 Public 不仅在 Class 块的内部是可见的,对 Class 块之外的代码也是可见的。没有使用 Private 或 Public 明确声明的被默认为 Public。在类的块内部被声明为 Public 的过程(Sub 或 Function)将成为类的方法。Public 变量将成为类的属性,同使用 Property Get、Property Let 和 Property Set 显式声明的属性相同。
[解决办法]
看看这个例子,可能有所理解
Class PencilClass
Private recentPencil, recentColor
Property Get Pencil()
Set Pencil = recentPencil
End Property
Property Set Pencil(x)
Set recentPencil = x
End Property
Property Get Pencilcolor()
Select Case recentColor
Case 1: Pencilcolor = "Orange"
Case 2: Pencilcolor = "Green"
Case Else: Pencilcolor = "yellow"
End Select
End Property
Property Let Pencilcolor(x)
If x = "Orange" Then
recentColor = 1
Else
If x = "Green" Then
recentColor = 2
Else
recentColor = 0
End If
End If
End Property
End Class
Class CUser
private sUserName'私有字段,只有本类可以用,外面不能访问
'不能访问只能通过字段属性来访问字段,如下:
Public Property Get UserName 用属性,可以对传进来的值进行做些过滤操作
UserName = sUserName
End Property
Public Property Let UserName(Byval Value)
sUserName = Value
End Property
End Class