2.3 Fields
rectangle
abstraction can inherit from the polygon
abstraction but alter the representation to something more appropriate for rectangles:
Even within a single abstraction, programmers can change their minds about what is stored and what is computed without rewriting lots of code. Syntactically, a simple message send that accesses an accessor method is just as concise as would be a variable access (using theobject
polygon;var field
vertices(@polygon);method
draw(p@polygon, d@output_device) { (-- draw the polygon on an output device, accessingvertices --)
}object
rectangleisa
polygon;var field
top(@rectangle);var field
bottom(@rectangle);var field
left(@rectangle);var field
right(@rectangle);method
vectices(r@rectangle) {-- ++ is a binary operator, here creating a new point object
[r.top ++ r.left, r.top ++ r.right, r.bottom ++ r.right, r.bottom ++ r.left] }method
set_vertices(r@rectangle, vs) { (-- set corners of rectangle fromvs list, if possible --)
}
p.x
syntactic sugar, described in section 2.5.6), thus imposing no burden on the programmer for the extra expressiveness. Other object-oriented languages such as Self and Trellis have shown the advantages of accessing instance variables solely through special get and set accessor methods. CLOS enables get and/or set accessor methods to be defined automatically as part of the defclass
form, but CLOS also provides a lower-level slot-value
primitive that can read and write any slot directly. Dylan joins Self and Trellis in accessing instance variables solely through accessor methods.
An object may define or inherit several fields with the same name. Just as with overloaded methods, this is legal as long as two methods, accessor or otherwise, do not have the same name, number of arguments, and argument specializers. A method may override a field accessor method without removing the field's memory location from the object, since a resend within the overriding method may invoke the field accessor method. Implementations may optimize away the storage for a field in an object if it cannot be accessed, as with the vertices
field in the rectangle
object.
Generated with Harlequin WebMaker