ColdFusion 9 CFScript 私有属性和公共属性
有没有办法使 isDevMode、devModeToEmailAddress、devModeFromEmailAddress 成为私有属性?
代码:
/**
* email
* @accessors true
*/
component email output="false" hint="This is email object." {
/* properties */
property name="toEmailAddress" type="string";
property name="fromEmailAddress" type="string";
property name="subject" type="string";
property name="body" type="string";
property name="attachments" type="array";
/*
private isDevMode
private devModeToEmailAddress
private devModeFromEmailAddress
*/
}
Is there a way to make isDevMode, devModeToEmailAddress, devModeFromEmailAddress to be private properites?
Code:
/**
* email
* @accessors true
*/
component email output="false" hint="This is email object." {
/* properties */
property name="toEmailAddress" type="string";
property name="fromEmailAddress" type="string";
property name="subject" type="string";
property name="body" type="string";
property name="attachments" type="array";
/*
private isDevMode
private devModeToEmailAddress
private devModeFromEmailAddress
*/
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以添加
setter="false"
和getter="false"
来阻止 getter 和 setter,但不能直接限制对属性的访问。最好的选择是将它们放入组件本地范围内的构造函数中。然后,当您需要使用这些值时,只需在任何函数中引用
variables.isDevMode
即可获取值。如果您需要在运行时设置它们,可以在函数的init()
方法中设置它们。我通常这样做:然后,每当我需要这些值时,我都会得到
variables.Instance.isDevMode
。我还创建了一个通用的get()
方法,该方法将返回variables.instance
,以便我可以看到其中的内容。但由于这些位于组件局部变量范围内,因此无法从组件外部修改它们。
You can add
setter="false"
andgetter="false"
to prevent getters and setters, but you can't restrict access to the properties directly. Your best bet is put those into your constructor in the component's local scope.Then, when you need to use those, just reference
variables.isDevMode
in any function to pick up the value. If you need to set those at runtime, you can set them in theinit()
method for your function. I usually do it like this:Then, any time I need those values I just get
variables.Instance.isDevMode
. I also create a genericget()
method that will return thevariables.instance
so I can see what's in there.But because these are in the components local variables scope, they can't be modified from outside the component.