這是家庭作業,但需要從沉悶的一天休息。
以下是使用<cfproperty/>
隱式獲取器和設置器的ColdFusion 9特定實現。 CFC覆蓋整數和雙精度元素的setter來執行數據類型驗證ColdFusion本身不執行。
BrownPeanut。CFC
<!--- accessors="true" causes CF9 to set data in the "variables" scope --->
<cfcomponent output="false" accessors="true">
<cfproperty name="MyDouble" type="numeric" />
<cfproperty name="MyInteger" type="numeric" />
<cfproperty name="MyString" type="string" />
<cffunction name="init" output="false" access="public" returntype="BrownPeanut" hint="Constructor">
<cfargument name="MyDouble" type="numeric" required="false" default="1.234"/>
<cfargument name="MyInteger" type="numeric" required="false" default="10"/>
<cfargument name="MyString" type="string" required="false" default="hello world"/>
<cfset setMyDouble(arguments.myDouble)>
<cfset setMyInteger(arguments.MyInteger)>
<cfset setMyString(arguments.MyString)>
<cfreturn this/>
</cffunction>
<cffunction name="setMyDouble" output="false" access="public" returntype="void"
hint="Overrides default setter">
<cfargument name="MyDouble" type="string" required="true"/>
<!--- data type checking because ColdFusion does not natively make the distinction --->
<cfset var jDouble = createObject("java", "java.lang.Double").init(arguments.myDouble)>
<cfif jDouble.toString() NEQ arguments.myDouble>
<cfthrow type="java.lang.IllegalArgumentException" message="Invalid double value '#arguments.MyDouble#'">
</cfif>
<cfset variables.MyDouble = arguments.MyDouble>
</cffunction>
<cffunction name="setMyInteger" output="false" access="public" returntype="void"
hint="Overrides default setter">
<cfargument name="MyInteger" type="string" required="true"/>
<!--- data type checking because ColdFusion does not natively make the distinction --->
<cfif NOT isValid("integer",arguments.MyInteger)>
<cfthrow type="java.lang.IllegalArgumentException" message="Invalid integer value '#arguments.myInteger#'">
</cfif>
<cfset variables.myInteger = arguments.myInteger>
</cffunction>
</cfcomponent>
BrownPeanut.cfm
<cffunction name="WriteToDebugWindow" output="true" access="public" returntype="void" hint="">
<cfargument name="data" type="string" required="true"/>
<cfset var local = structNew()/>
<!--- implementation goes here --->
<cfoutput>#arguments.data#<br /></cfoutput>
</cffunction>
<cfset BrownPeanut = new BrownPeanut()>
<cfset writeToDebugWindow(BrownPeanut.getMyDouble())>
<cfset writeToDebugWindow(BrownPeanut.getMyInteger())>
<cfset writeToDebugWindow(BrownPeanut.getMyString())>
不是真的知道你是問什麼。問題很簡單。您是否要求在Java術語中更多地解釋ColdFusion? – charliegriefer 2011-05-19 03:41:41