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

发布于 2024-12-07 16:24:40 字数 557 浏览 0 评论 0原文

有没有办法使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永不分离 2024-12-14 16:24:40

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

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

isDevMode = false;
devModeToEmailAddress = "[email protected]";
devModeFromEmailAddress = "[email protected]";

/* 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);
}

但由于这些位于组件局部变量范围内,因此无法从组件外部修改它们。

You can add setter="false" and getter="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.

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

isDevMode = false;
devModeToEmailAddress = "[email protected]";
devModeFromEmailAddress = "[email protected]";

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


}

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 the init() method for your function. I usually do it like this:

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;

    {

}

Then, any time I need those values I just get variables.Instance.isDevMode. I also create a generic get() method that will return the variables.instance so I can see what's in there.

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

But because these are in the components local variables scope, they can't be modified from outside the component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文