正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology 6.001 Notes: Section 14.3 Slide 14.3.1 How build an OoP system in Scheme? Now, lets revisit these ideas by looking at how one might build an actual object-oriented system in Scheme. We are going to intertwine the idea of building class systems in Scheme, with the idea of using classes as a design methodology. Although not optimal, we will use a language we understand(Scheme)as a basis for examining how to create procedures that support class based systems, while trying to abstract the general methods from the specifics of this implementation in Scheme How build an OOP system in Scheme? Slide 14.3.2 Objects: as procedures that take messages Here is our blueprint for making this happen. First of all, objects in our system, i.e. the particular instances, we will represent as bject instances are unique Scheme procedures Local State gives each object (each instance of a class procedures that accept messages as input. We will use the same idea we saw earlier, message-passing procedures that accept Each instance procedure has own local environment messages and cause changes to state variables as a function of those messages. One of the advantages of this choice is that each instance will be uniquely identified. We can use eq? to tell them apart, because each object instance will be a unique Scheme procedure with a local frame that captures its information as local state Each object or instance can be formed differently because it has a procedure that points to a local environment that captures the state information, just as we saw in the previous examples of the earlier lectures Thus we will represent instances as local procedures with local state captured in local environments Slide 14.3.3 How build an OOP system in Scheme? Instances, of course, are simply particular versions of a class. So we will also need in our system a way of defining classes, and we will use a particular convention. We will define a Scheme Object instances are unique Scheme procedures procedure called ma ke-something(e.g. make-person, Each instance procedure has own local environment make-professor). Inside of that procedure, that classes: Scheme make-<object procedures definition of a class. we need two things we will need a set of Methods returned in response to pendent arguments) methods that will be returned in response to messages, so the -Inheritance Rule telling what method to use class procedures will need to contain within themselves ways of ssages methods taking in a message and returning a local method The second thing we need is an inheritance chain, a way of telling the class what methods to use, and by this we mean not just the local method, but rather if the class is a subclass of some superclass, we need a convention for deciding how to inherit methods from that superclass. In the case of a professor, for example, we might have methods to deal with LECTURE or WHOAREYOU?, but we also want to inherit the method of saY from the superclass of persoN So we will need to set up conventions for inheriting methods from superclasses6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. 6.001 Notes: Section 14.3 Slide 14.3.1 Now, let’s revisit these ideas by looking at how one might build an actual object-oriented system in Scheme. We are going to intertwine the idea of building class systems in Scheme, with the idea of using classes as a design methodology. Although not optimal, we will use a language we understand (Scheme) as a basis for examining how to create procedures that support class based systems, while trying to abstract the general methods from the specifics of this implementation in Scheme. Slide 14.3.2 Here is our blueprint for making this happen. First of all, objects in our system, i.e. the particular instances, we will represent as procedures that accept messages as input. We will use the same idea we saw earlier, message-passing procedures that accept messages and cause changes to state variables as a function of those messages. One of the advantages of this choice is that each instance will be uniquely identified. We can use eq? to tell them apart, because each object instance will be a unique Scheme procedure with a local frame that captures its information as local state. Each object or instance can be formed differently because it has a procedure that points to a local environment that captures the state information, just as we saw in the previous examples of the earlier lectures. Thus we will represent instances as local procedures with local state captured in local environments. Slide 14.3.3 Instances, of course, are simply particular versions of a class. So we will also need in our system a way of defining classes, and we will use a particular convention. We will define a Scheme procedure called make-something (e.g. make-person, make-professor). Inside of that procedure, that definition of a class, we need two things: we will need a set of methods that will be returned in response to messages, so the class procedures will need to contain within themselves ways of taking in a message and returning a local method. The second thing we need is an inheritance chain, a way of telling the class what methods to use, and by this we mean not just the local method, but rather if the class is a subclass of some superclass, we need a convention for deciding how to inherit methods from that superclass. In the case of a professor, for example, we might have methods to deal with LECTURE or WHOAREYOU?, but we also want to inherit the method of SAY from the superclass of PERSON. So we will need to set up conventions for inheriting methods from superclasses
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有