Function ColdFusion中通过组件继承的公私函数访问
我遇到了ColdFusion标记语言的意外问题。假设我有以下组件。如果公共函数和私有函数都在“基”组件中定义,那么前者在从扩展“子”类型的实例调用时是否仍能调用私有函数 程序。cfcFunction ColdFusion中通过组件继承的公私函数访问,function,inheritance,coldfusion,encapsulation,cfml,Function,Inheritance,Coldfusion,Encapsulation,Cfml,我遇到了ColdFusion标记语言的意外问题。假设我有以下组件。如果公共函数和私有函数都在“基”组件中定义,那么前者在从扩展“子”类型的实例调用时是否仍能调用私有函数 程序。cfc <cfcomponent> <cffunction access="public" name="doOperation"> <cfset this.checkValue(-14)> </cffunction> <
<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>
<cfcomponent extends="Program">
</cfcomponent>
子程序.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>
<cfcomponent extends="Program">
</cfcomponent>
Run.cfm
<cfobject component="SubProgram" name="this.instance">
<cfset this.instance.doOperation()> <<<<----ERROR---->>>>
ColdFusion抛出错误
在组件子程序
中未找到方法检查值
。请确保已定义该方法
这里的问题是什么?封装时没有布朗尼点数 问题是您试图将
checkValue()
方法作为公共方法调用此
在CFML中的工作方式与在其他语言中的工作方式不同(对于Macromedia而言,这是一个非常糟糕的设计决策):此
是对象本身的外部引用,因此如果调用this.someMethod()
,则尝试调用名为someMethod()
的公共
方法(就像调用myObject.someMethod()
)。用CFML的说法,变量
范围是对私有数据/成员的引用
您要做的是:
<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有。谢谢,我知道我没有疯。顺便说一句,这是CF8。啊,好的:坚持使用标记。你不能升级到仍然支持的CF版本(例如:10或11?).8有点垃圾。