[注意:在cfcs中包含代码通常是不好的做法,(请参阅下面的答案),所以请考虑这只是研究]
总而言之,我有一个类和一个子类以及一个被子类覆盖的方法.当我在子类中对方法进行硬编码时,一切正常,当我使用cfinclude将它包含在伪构造函数中时,mixin样式,我得到一个"例程不能多次声明".错误.
这看起来非常简单.我想念的是什么:这个混合?
父类:
<cfcomponent >
<cffunction name="hola" hint="i am the parent method">
<cfreturn "hola - parent">
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)
儿童班:
<cfcomponent extends="mixinTestParent">
<!--- this would work, successfully overridding parent method
<cffunction name="hola" hint="i am the child method">
<cfreturn "hola - child">
</cffunction>--->
<cfinclude template="mixinTestInc.cfm">
<cffunction name="init" access="public" returntype="any" output="false">
<cfreturn this>
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)
包括:
<cffunction name="hola" hint="i am the child method" access="public">
<cfreturn "hola - child">
</cffunction>
Run Code Online (Sandbox Code Playgroud)
亚军:
<cfset test = new mixinTestChild().init()>
<cfdump var="#test.hola()#">
Run Code Online (Sandbox Code Playgroud)
提前致谢!!
由于实例化CFC的方式,您收到错误.
当您hola()在父级和hola()子级中,子级扩展父级时,在创建子级CFC时,它将hola()在父级中查看并覆盖它.但是,该功能仍存在于CFC中.
从子CFC中,您可以引用两者hola()(在子CFC中super.hola()定义)和(在父级中定义).
使用时<cfinclude/>,将实例化CFC,并将包含文件的内容添加到组合中.但是,它们不被视为继承模型的一部分,就像"此CFC中的其他函数"一样,因此您会收到错误.
我同意这是一个不好的做法,而不是重构,但它是一个很好的方法,允许实用程序UDF混合而不使它们成为您的模型的一部分.