How can I do this OOP as a ColdFusion Component (CFC)?
Would some one be able to explain what the following questions are asking me to do. I don开发者_如何学运维't need to know the code, just a different explanation in terms of Coldfusion would be good so I might be able to understand what they are asking me to write.
As far as I know ColdFusion doesn't have "properties", This has to be simulated with a CFC.
Create a class with the following characteristics
- It should have three data members (one integer, one string, and one double) When constructed the object should initialize its three data members with the following values respectively: 10, "hello world", and 1.234
- It should contain properties that allow a consumer using the class to retrieve and modify any of the data member values.
- It should have a method that allows the consumer to set all 3 values in one call.
Create a method which creates a new instance of the class specified in question 4: (you do not have to write any code to test the procedure)
- Use properties provided by the class to get the value of each data member and output the value to the debug window using the fictional and globally available method of WriteToDebugWindow(). WriteToDebugWindow() takes one string parameter whose value is outputted to the debug window. Since this method is fictional it will therefore be sufficient no matter what language you choose to take this test in.
- Use properties provided by the class to modify each data member to values of your choosing and then output the values of the properties you changed to the debug window using WriteToDebugWindow() as before.
This reeks of homework, but needed a break from a dreary day.
Below is a ColdFusion 9 specific implementation using <cfproperty/>
implicit getters and setters. The CFC overrides those setters for the integer and double members to perform data type validation ColdFusion does not do natively.
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())>
So you'll need an init method that initialises your values into the this scope, then returns this. The returntype should be like "path.to.yourCFC". You'll need individual getter and setter methods, as well as one method that will update them all. The latter method should just call the individual setters.
精彩评论