|
|
— |
clips:clip5foj [2010/01/04 19:46] (current) |
| |
| <HTML> |
| <HEAD> |
| <META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252"> |
| <TITLE>DEFGENERIC / DEFMETHOD</TITLE> |
| </HEAD> |
| |
| <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> |
| |
| <OBJECT TYPE="application/x-oleobject" CLASSID="clsid:1e2a7bd0-dab9-11d0-b93a-00c04fc99f9e"> |
| <PARAM NAME="Keyword" VALUE="DEFGENERIC"> |
| <PARAM NAME="Keyword" VALUE="DEFMETHOD"> |
| </OBJECT> |
| |
| <H1><A NAME="defgeneric"></A>DEFGENERIC / DEFMETHOD</H1> |
| |
| <P></P> |
| |
| <P>With the defgeneric/defmethod constructs, new generic functions may be written </P> |
| |
| <P>directly in CLIPS. Generic functions are similar to deffunctions because </P> |
| |
| <P>they can be used to define new procedural code directly in CLIPS, and they can </P> |
| |
| <P>be called like any other function. However, generic functions are much more </P> |
| |
| <P>powerful because they can do different things depending on the types </P> |
| |
| <P>(or classes) and number of their arguments. For example, a '+' operator could </P> |
| |
| <P>be defined which performs concatenation for strings but still performs </P> |
| |
| <P>arithmetic addition for numbers. Generic functions are comprised of </P> |
| |
| <P>multiple components called methods, where each method handles different </P> |
| |
| <P>cases of arguments for the generic function. A generic function which has </P> |
| |
| <P>more than one method is said to be overloaded.</P> |
| |
| <P>A generic function is comprised of a header (similar to a forward declaration) </P> |
| |
| <P>and zero or more methods. A generic function header can either be explicitly </P> |
| |
| <P>declared by the user or implicitly declared by the definition of at least one </P> |
| |
| <P>method. The defgeneric construct is used to specify the generic function header,</P> |
| |
| <P>and the defmethod construct is used for each of the generic function's methods.</P> |
| |
| <P>The syntax of the defgeneric/defmethod constructs is:</P> |
| |
| <PRE><CODE>(defgeneric <name> [<comment>]) |
| |
| (defmethod <name> [<index>] [<comment>] |
| (<parameter-restriction>* [<wildcard-parameter>]) |
| <action>*) |
| |
| <parameter-restriction> ::= <single-field-variable> | |
| (<single-field-variable> <type>* [<query>]) |
| |
| <wildcard-parameter> ::= <multifield-variable> |
| |
| <type> ::= <class-name> |
| |
| <query> ::= <global-variable> | <function-call></CODE></PRE> |
| |
| </BODY> |
| </HTML> |
| |