正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology superclass instances, we ensure consistency of behavior and isolate changes to a single method Slide 14.5.3 Example World: Professor Subclass Let's extend our current example to look at this issue. We already Viage have a class, called a person. We can create a subclass of A professor prefaces all lecture person, called a professor. A professor, because it is a kind of material with"Therefore person, has the same capabilities as a person. It has an internal variable for its names; it has methods for returning its name, for PROFESSOR changing its name, and for saying things. However, a professor has a unique capability, different from normal people. When a professor is"lecturing", it prefaces all said material with the word"Therefore. Thus it has a new method. LECTURE which does that Example World: Professor Subclass Slide 14.5. 4 Therefore"the behavior we expect is the following: If we define A professor prefaces all lecture material with "Therefore e in the global environment to be an instance of a professor (define e(nake-protessor erie rimson)) using a make- procedure that we will discuss shortly, then we aske“ AHIOARE YONI? Professor grimson can ask e to SaY things. In this case, it behaves just like a person, as it inherits the SAY method from its superclass ask e 'SAY (the sky is blue) the sky is blue If we ask e its name we get a different behavior from a normal 2 uuf-said person, which suggests that we will need a new method that (ask eLECTURE '(the sky ig blue) therefore the sky is blue shadows the persons method --)nuf-lectured I Finally, we can ask e to LECTURE about the chromaticity of the atmosphere. In that case, e as a professor should use the LECTURE method. This will, as a consequence, say therefore the sky is blue So we see that this object e should both have the ability to use methods specific to a professor and the ability to inherit methods from an underlying person Slide 14.5.5 2. Approach: Inheriting Superclass Methods Here is the approach we will take to make this happen. In Subclass will Inherit superclass behavior by adding an particular, we are going to allow the inheritance of methods from"interna profe or wl have an intemal per son object superclasses to subclasses, by within each subclass creating an If message is not recognized, pass the buc internal instance of a superclass. In other words, a professor will have within it an internal instance of a person. Then, if message is passed to the professor, the professor will first see if it has an explicit method for that message. If so, it does its thing If not, it"passes the buck"to the internal person instance, asking it to handle this message6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. superclass instances, we ensure consistency of behavior and isolate changes to a single method. Slide 14.5.3 Let's extend our current example to look at this issue. We already have a class, called a person. We can create a subclass of person, called a professor. A professor, because it is a kind of person, has the same capabilities as a person. It has an internal variable for its names; it has methods for returning its name, for changing its name, and for saying things. However, a professor has a unique capability, different from normal people. When a professor is "lecturing", it prefaces all said material with the word "Therefore". Thus it has a new method, LECTURE, which does that. Slide 14.5.4 "Therefore" the behavior we expect is the following: If we define e in the global environment to be an instance of a professor, using a make- procedure that we will discuss shortly, then we can ask e to SAY things. In this case, it behaves just like a person, as it inherits the SAY method from its superclass. If we ask e its name, we get a different behavior from a normal person, which suggests that we will need a new method that shadows the person’s method. Finally, we can ask e to LECTURE about the chromaticity of the atmosphere. In that case, e as a professor should use the LECTURE method. This will, as a consequence, say therefore the sky is blue. So we see that this object e should both have the ability to use methods specific to a professor and the ability to inherit methods from an underlying person. Slide 14.5.5 Here is the approach we will take to make this happen. In particular, we are going to allow the inheritance of methods from superclasses to subclasses, by within each subclass creating an internal instance of a superclass. In other words, a professor will have within it an internal instance of a person. Then, if a message is passed to the professor, the professor will first see if it has an explicit method for that message. If so, it does its thing. If not, it "passes the buck" to the internal person instance, asking it to handle this message
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有