ColdFusion 9 CFScript私有属性和公共属性

时间:2011-10-03 15:38:54

标签: oop coldfusion coldfusion-9

有没有办法让isDevMode,devMode To EmailAddress,devMode FromEmailAddress成为私有属性?

代码:

/**
* 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
*/

}

1 个答案:

答案 0 :(得分:7)

您可以添加setter="false"getter="false"以阻止getter和setter,但不能直接限制对属性的访问。您最好的选择是将它们放入组件本地范围内的构造函数中。

/**
* email
* @accessors true
*/
component email output="false" hint="This is email object." {

isDevMode = false;
devModeToEmailAddress = "foo@foo.com";
devModeFromEmailAddress = "bar@foo.com";

/* 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";


}

然后,当您需要使用它们时,只需在任何函数中引用variables.isDevMode来获取值。如果需要在运行时设置它们,可以在函数的init()方法中设置它们。我通常这样做:

component email output="false" hint="This is email object." {

    instance = {};

    /* 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";


    public email function(required boolean isDevMode, required string devModeToEmailAddress, required string devModeFromEmailAddress){

        variables.Instance.isDevMode = Arguments.isDevMode;
        variables.Instance.devModeToEmailAddress = Arguments.devModeToEmailAddress;
        variables.Instance.devModeFromEmailAddress = Arguments.devModeFromEmailAddress;

    {

}

然后,只要我需要这些值,我就会得到variables.Instance.isDevMode。我还创建了一个通用的get()方法,它将返回variables.instance,以便我可以看到其中的内容。

public struct function get(){
    return Duplicate(variables.Instance);
}

但是因为它们在组件局部变量范围内,所以不能从组件外部修改它们。