Merge branch 'master' of git://factorcode.org/git/factor
commit
c00963950d
|
@ -129,17 +129,18 @@ ARTICLE: "tuple-inheritance-example" "Tuple subclassing example"
|
|||
}
|
||||
"Rectangles and parallelograms use the same algorithm for computing the area, whereas they use different algorithms for computing perimiter. Also, rectangles and parallelograms both have " { $snippet "width" } " and " { $snippet "height" } " slots. We can exploit this with subclassing:"
|
||||
{ $code
|
||||
"USING: accessors kernel math math.constants math.functions ;"
|
||||
"GENERIC: area ( shape -- n )"
|
||||
"GENERIC: perimiter ( shape -- n )"
|
||||
""
|
||||
"TUPLE: shape ;"
|
||||
""
|
||||
"TUPLE: circle < shape radius ;"
|
||||
"M: area circle radius>> sq pi * ;"
|
||||
"M: perimiter circle radius>> 2 * pi * ;"
|
||||
"M: circle area radius>> sq pi * ;"
|
||||
"M: circle perimiter radius>> 2 * pi * ;"
|
||||
""
|
||||
"TUPLE: quad < shape width height"
|
||||
"M: area quad [ width>> ] [ height>> ] bi * ;"
|
||||
"TUPLE: quad < shape width height ;"
|
||||
"M: quad area [ width>> ] [ height>> ] bi * ;"
|
||||
""
|
||||
"TUPLE: rectangle < quad ;"
|
||||
"M: rectangle perimiter [ width>> 2 * ] [ height>> 2 * ] bi + ;"
|
||||
|
|
Loading…
Reference in New Issue