gpt4 book ai didi

function - 通过 ColdFusion 中的组件继承访问公私功能

转载 作者:行者123 更新时间:2023-12-05 01:02:00 26 4
gpt4 key购买 nike

我遇到了 ColdFusion 标记语言的意外问题。假设我有以下组件。如果在“基本”组件中定义了公共(public)和私有(private)函数,那么当从扩展的“子”类型的实例调用时,前者仍然可以调用私有(private)函数吗?

程序.cfc

<cfcomponent>

<cffunction access="public" name="doOperation">
<cfset this.checkValue(-14)>
</cffunction>


<cffunction access="private" name="checkValue">
<cfargument name="notNeg" required="yes" type="numeric">

<cfif arguments.notNeg LT 0 >
<cfthrow message="Negative Numbers not allowed">
</cfif>
</cffunction>

</cfcomponent>

子程序.cfc
<cfcomponent extends="Program">

</cfcomponent>

运行.cfm
 <cfobject component="SubProgram" name="this.instance">

<cfset this.instance.doOperation()> <<<<----ERROR---->>>>

ColdFusion 抛出错误

method checkValue was not found in component SubProgram. Ensure that the method is defined...



这里有什么问题?没有用于封装的布朗尼点!

最佳答案

问题是您正在尝试调用 checkValue()方法作为公共(public)方法。 this在 CFML 中的工作方式与在其他语言中的工作方式不同(Macromedia 的设计决策非常糟糕):this是对对象本身的外部引用,所以如果你调用 this.someMethod() ,这是试图调用 public方法称为 someMethod() (就像你调用 myObject.someMethod() 一样)。用 CFML 的说法,variables范围是对私有(private)数据/成员的引用。

你想要做的是:

<cffunction access="public" name="doOperation"> 
<cfset variables.checkValue(-14)>
</cffunction>

或者简单地说:
<cffunction access="public" name="doOperation"> 
<cfset checkValue(-14)>
</cffunction>

此外,如果您使用的是最新版本的 CF(例如:CF10 或 CF11),您真的不想在标签中编写组件。它使代码看起来很糟糕。尝试限制标签使用以查看文件。 CF10 仍然没有 100% 支持脚本中的所有 CFML 结构,但 CF11 可以。

关于function - 通过 ColdFusion 中的组件继承访问公私功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27214545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com