正在加载图片...
(make-me thods 工s-A (lambda (type) (memg type (ask self TYPE)))))) The root object provides a basis for providing common behaviors to all classes Specifically, it provides a convenient method(Is-A)to see if a type descriptor is in the TYPE list. We will by convention use this class as the root for all other classe Named-objects have no other local state than the name variable. They do have four methods: TYPE NAME. INSTALL and DESTRoY. The TYPE method comes from the make handler procedure and it indicates that named-objects have the type named-object in ddition to any type descriptors that the root-part has. The INSTALL method is not required, but if it exists, it is called when an instance is created. In the case of named object, there is nothing to do at creation time, but we'll later see examples where this method is non-trivial. The name is a selector in that it returns the name with which the object was created However, the named-object procedure only builds a handler for an object of type named-object. In order to get an instance, we need a create-named-object procedure (define (create-named-object name) symbol - named-object (create-ins tance named-object name)) Here, an instance is created using the named-object procedure. The create-instance procedure builds a handler procedure that serves as a container for the real handler for the nstance. It also attaches a tag or label to the handler, so that we know we are working with an instance. We need this extra complexity because each foo procedure expects self as an argument, so we build an instance object, then create the handler, passing the instance object in for self. You'll explore more of this system in the questions below Using instances Once you have an instance, you can call the methods on it using the ask procedure (define book (create-named-object 'sicp)) (ask book 'NAME) alue: si (ask book : Value: (named-object root'root (make-methods 'IS-A (lambda (type) (memq type (ask self 'TYPE)))))) The root object provides a basis for providing common behaviors to all classes. Specifically, it provides a convenient method (IS-A) to see if a type descriptor is in the TYPE list. We will by convention use this class as the root for all other classes. Named-objects have no other local state than the name variable. They do have four methods: TYPE, NAME, INSTALL, and DESTROY. The TYPE method comes from the make￾handler procedure and it indicates that named-objects have the type named-object in addition to any type descriptors that the root-part has. The INSTALL method is not required, but if it exists, it is called when an instance is created. In the case of named￾object, there is nothing to do at creation time, but we'll later see examples where this method is non-trivial. The NAME is a selector in that it returns the name with which the object was created. However, the named-object procedure only builds a handler for an object of type named-object. In order to get an instance, we need a create-named-object procedure: (define (create-named-object name) ; symbol -> named-object (create-instance named-object name)) Here, an instance is created using the named-object procedure. The create-instance procedure builds a handler procedure that serves as a container for the real handler for the instance. It also attaches a tag or label to the handler, so that we know we are working with an instance. We need this extra complexity because each foo procedure expects self as an argument, so we build an instance object, then create the handler, passing the instance object in for self. You'll explore more of this system in the questions below. Using Instances Once you have an instance, you can call the methods on it using the ask procedure: (define book (create-named-object 'sicp)) (ask book 'NAME) ;Value: sicp (ask book 'TYPE) ;Value: (named-object root)
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有