如何将这个 OOP 作为 ColdFusion 组件 (CFC) 来执行?

发布于 2024-11-08 04:46:54 字数 676 浏览 0 评论 0原文

有人能够解释以下问题要求我做什么吗?我不需要了解代码,只需根据 Coldfusion 进行不同的解释就可以了,这样我也许就能理解他们要求我写的内容。

据我所知,ColdFusion 没有“属性”,这必须使用 CFC 进行模拟。

  1. 创建一个具有以下特征的类

    • 它应该有 3 个数据成员(1 个整数、1 个字符串和 1 个双精度) 构造时,对象应该分别使用以下值初始化其三个数据成员:10、“hello world”和 1.234
    • 它应该包含允许使用该类的使用者检索和修改任何数据成员值的属性。
    • 它应该有一个方法,允许消费者在一次调用中设置所有 3 个值。
  2. 创建一个方法,该方法创建问题 4 中指定的类的新实例:(您不必编写任何代码来测试该过程)

    • 使用类提供的属性 获取每个数据成员的值 并将值输出到调试 窗口使用虚构的和 全局可用的方法 WriteToDebugWindow()。
      WriteToDebugWindow() 接受一个字符串 其值输出到的参数 调试窗口。由于这个方法 是虚构的,因此将是 无论什么语言都足够 您选择参加此测试。
    • 使用类提供的属性 将每个数据成员修改为值 您选择的然后输出 您更改的属性的值 使用到调试窗口 和以前一样,WriteToDebugWindow()。

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.

  1. 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.
  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2024-11-15 04:46:54

这充满了家庭作业的味道,但需要从沉闷的一天中休息一下。

下面是使用 隐式 getter 和 setter 的 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())>

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())>
伪装你 2024-11-15 04:46:54

因此,您需要一个 init 方法,将您的值初始化到 this 范围,然后返回 this。返回类型应类似于“path.to.yourCFC”。您将需要单独的 getter 和 setter 方法,以及一种可以更新所有这些方法的方法。后一种方法应该只调用各个设置器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文