正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 14.2.3 Now we add a sub-class of person, called a professor. An initial class hierarchy Note that this class does not have any specific internal class variables. However, because it is a subclass of a person, it z-1'say '(helle there) should inherit the class variables of its superclass. In other words, professors also have a first and last name, because their person superclass instance has such variables. And professors PROFESSOR have the ability to say things by virtue of being a subclass of LECTURE person. Note that we again assume there is some constructor for making instances of a class(which should will see. take care of creating superclass instances as part of the process). Because of this hierarchy of classes, we should be able to ask the professor to say something, and it will use its inherited method from the person instance to do this An initial class hierarchy Slide 14.2, 4 PERSON (ask professor-1'WHOAREYOU?) In our little world, professors have no class variables of their own(ah, the irony!) but they do have two methods,a whoareyou? method and a lecture method. Notice that a WHOAREYOU? professor has its own whoareyou? method, distinct from the identically named method in person. If we ask a professor whoareyou? it will run its own method to answer the question, LECTURE with a different behavior. when a subclass has a method of the same name as a superclass, the subclass method is said to shadow the inherited method in the superclass instance Slide 14. 2.5 An initial class hierarchy Now in the world we are creating, it is traditional that when a professor lectures he starts every sentence with" Therefore e professor Snith An interesting question to consider when actually implementing the professor class is whether this lecture method is a distinct sk professor -1 LECTURE(the sky is method, or whether it shares structure with the underlying say +Therefore, the sky is blue method of the inherited person class. Conceptually, we would like to think that lecturing is a particular variant on saying Royd」 professor should"delegate"part indeed one simply says the word"Therefore"and then says the of the lecture method to a persons emaining text. This idea of using a superclass method to accomplish part of a method is called" delegation Note that this is an important requirement to place on an object system. The idea is that at a conceptual level, just as classes can be related to one another(e.g. via the subclass hierarchy ), so too can methods be related to one another, by this delegation idea. And at the implementation level, delegation can be seen as a mechanism that allows subclasses to specialize(and use )methods found in superclasses This has two important consequences. The first is that if we design our object system correctly, we will have a clean modularity of code, so that there is only one place to implement saying some thing, and thus only one place to worry about if we decide to change the manner in which this method executes. Secondly, we have an explicit indication (through the act of delegation that the lecture method and the say method are related conceptually. We will return to this point when we consider an explicit implementation of an object-oriented system in Scheme6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 14.2.3 Now we add a sub-class of person, called a professor. Note that this class does not have any specific internal class variables. However, because it is a subclass of a person, it should inherit the class variables of its superclass. In other words, professors also have a first and last name, because their person superclass instance has such variables. And professors have the ability to say things by virtue of being a subclass of person. Note that we again assume there is some constructor for making instances of a class (which should, as we will see, take care of creating superclass instances as part of the process). Because of this hierarchy of classes, we should be able to ask the professor to say something, and it will use its inherited method from the person instance to do this. Slide 14.2.4 In our little world, professors have no class variables of their own (ah, the irony!) but they do have two methods, a whoareyou? method and a lecture method. Notice that a professor has its own whoareyou? method, distinct from the identically named method in person. If we ask a professor whoareyou? it will run its own method to answer the question, with a different behavior. When a subclass has a method of the same name as a superclass, the subclass method is said to shadow the inherited method in the superclass instance. Slide 14.2.5 Now in the world we are creating, it is traditional that when a professor lectures he starts every sentence with “Therefore”. An interesting question to consider when actually implementing the professor class is whether this lecture method is a distinct method, or whether it shares structure with the underlying say method of the inherited person class. Conceptually, we would like to think that lecturing is a particular variant on saying: indeed one simply says the word “Therefore” and then says the remaining text. This idea of using a superclass’ method to accomplish part of a method is called “delegation”. Note that this is an important requirement to place on an object system. The idea is that at a conceptual level, just as classes can be related to one another (e.g. via the subclass hierarchy), so too can methods be related to one another, by this delegation idea. And at the implementation level, delegation can be seen as a mechanism that allows subclasses to specialize (and use) methods found in superclasses. This has two important consequences. The first is that if we design our object system correctly, we will have a clean modularity of code, so that there is only one place to implement saying some thing, and thus only one place to worry about if we decide to change the manner in which this method executes. Secondly, we have an explicit indication (through the act of delegation) that the lecture method and the say method are related conceptually. We will return to this point when we consider an explicit implementation of an object-oriented system in Scheme
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有