61a extra lecture 6 implementing an object system
play

61A Extra Lecture 6 Implementing an Object System 3 Implementing - PowerPoint PPT Presentation

61A Extra Lecture 6 Implementing an Object System 3 Implementing an Object System Today's topics: 3 Implementing an Object System Today's topics: What is a class? 3 Implementing an Object System Today's topics: What is a class?


  1. 61A Extra Lecture 6

  2. Implementing an Object System 3

  3. Implementing an Object System Today's topics: 3

  4. Implementing an Object System Today's topics: • What is a class? 3

  5. Implementing an Object System Today's topics: • What is a class? • What is an instance? 3

  6. Implementing an Object System Today's topics: • What is a class? • What is an instance? • How do we create inheritance relationships? 3

  7. Implementing an Object System Today's topics: • What is a class? • What is an instance? • How do we create inheritance relationships? • How do we write code for attribute look-up procedures? 3

  8. Implementing an Object System Today's topics: • What is a class? • What is an instance? • How do we create inheritance relationships? • How do we write code for attribute look-up procedures? Tools we'll use: 3

  9. Implementing an Object System Today's topics: • What is a class? • What is an instance? • How do we create inheritance relationships? • How do we write code for attribute look-up procedures? Tools we'll use: • Dispatch dictionaries 3

  10. Implementing an Object System Today's topics: • What is a class? • What is an instance? • How do we create inheritance relationships? • How do we write code for attribute look-up procedures? Tools we'll use: • Dispatch dictionaries • Higher-order functions 3

  11. The OOP Abstraction Barrier (a.k.a. the Line) 4

  12. The OOP Abstraction Barrier (a.k.a. the Line) THE LINE 4

  13. The OOP Abstraction Barrier (a.k.a. the Line) Above the Line: THE LINE 4

  14. The OOP Abstraction Barrier (a.k.a. the Line) Above the Line: • Objects with local state & interact via message passing THE LINE 4

  15. The OOP Abstraction Barrier (a.k.a. the Line) Above the Line: • Objects with local state & interact via message passing • Objects are instantiated by classes, which are also objects THE LINE 4

  16. The OOP Abstraction Barrier (a.k.a. the Line) Above the Line: • Objects with local state & interact via message passing • Objects are instantiated by classes, which are also objects • Classes may inherit from other classes to share behavior THE LINE 4

  17. The OOP Abstraction Barrier (a.k.a. the Line) Above the Line: • Objects with local state & interact via message passing • Objects are instantiated by classes, which are also objects • Classes may inherit from other classes to share behavior • Mechanics of objects are governed by " evaluation procedures " THE LINE 4

  18. The OOP Abstraction Barrier (a.k.a. the Line) Above the Line: • Objects with local state & interact via message passing • Objects are instantiated by classes, which are also objects • Classes may inherit from other classes to share behavior • Mechanics of objects are governed by " evaluation procedures " THE LINE Below the Line: 4

  19. The OOP Abstraction Barrier (a.k.a. the Line) Above the Line: • Objects with local state & interact via message passing • Objects are instantiated by classes, which are also objects • Classes may inherit from other classes to share behavior • Mechanics of objects are governed by " evaluation procedures " THE LINE Below the Line: • Objects have mutable dictionaries of attributes 4

  20. The OOP Abstraction Barrier (a.k.a. the Line) Above the Line: • Objects with local state & interact via message passing • Objects are instantiated by classes, which are also objects • Classes may inherit from other classes to share behavior • Mechanics of objects are governed by " evaluation procedures " THE LINE Below the Line: • Objects have mutable dictionaries of attributes • Attribute look-up for instances is a function 4

  21. The OOP Abstraction Barrier (a.k.a. the Line) Above the Line: • Objects with local state & interact via message passing • Objects are instantiated by classes, which are also objects • Classes may inherit from other classes to share behavior • Mechanics of objects are governed by " evaluation procedures " THE LINE Below the Line: • Objects have mutable dictionaries of attributes • Attribute look-up for instances is a function • Attribute look-up for classes is another function 4

  22. The OOP Abstraction Barrier (a.k.a. the Line) Above the Line: • Objects with local state & interact via message passing • Objects are instantiated by classes, which are also objects • Classes may inherit from other classes to share behavior • Mechanics of objects are governed by " evaluation procedures " THE LINE Below the Line: • Objects have mutable dictionaries of attributes • Attribute look-up for instances is a function • Attribute look-up for classes is another function • Object instantiation is another function 4

  23. Implementing the Object Abstraction 5

  24. Implementing the Object Abstraction Fundamental OOP concepts: 5

  25. Implementing the Object Abstraction Fundamental OOP concepts: • Object instantiation and initialization 5

  26. Implementing the Object Abstraction Fundamental OOP concepts: • Object instantiation and initialization • Attribute look-up and assignment 5

  27. Implementing the Object Abstraction Fundamental OOP concepts: • Object instantiation and initialization • Attribute look-up and assignment • Method invocation 5

  28. Implementing the Object Abstraction Fundamental OOP concepts: • Object instantiation and initialization • Attribute look-up and assignment • Method invocation • Inheritance 5

  29. Implementing the Object Abstraction Fundamental OOP concepts: • Object instantiation and initialization • Attribute look-up and assignment • Method invocation • Inheritance Not-so-fundamental issues (that we'll skip): 5

  30. Implementing the Object Abstraction Fundamental OOP concepts: • Object instantiation and initialization • Attribute look-up and assignment • Method invocation • Inheritance Not-so-fundamental issues (that we'll skip): • Dot expression syntax 5

  31. Implementing the Object Abstraction Fundamental OOP concepts: • Object instantiation and initialization • Attribute look-up and assignment • Method invocation • Inheritance Not-so-fundamental issues (that we'll skip): • Dot expression syntax • Multiple inheritance (on your homework) 5

  32. Implementing the Object Abstraction Fundamental OOP concepts: • Object instantiation and initialization • Attribute look-up and assignment • Method invocation • Inheritance Not-so-fundamental issues (that we'll skip): • Dot expression syntax • Multiple inheritance (on your homework) • Introspection (e.g., what class does this object have?) 5

  33. Instances 6

  34. Instances Dispatch dictionary with messages 'get' and 'set' 6

  35. Instances Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes" 6

  36. Instances Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes" 6 (Demo)

  37. Instances Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes" def make_instance(cls): """Return a new object instance.""" def get_value(name): if name in attributes: return attributes[name] else: value = cls['get'](name) return bind_method(value, instance) def set_value(name, value): attributes[name] = value attributes = {} instance = {'get': get_value, 'set': set_value} return instance 6 (Demo)

  38. Instances Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes" The class of the instance def make_instance(cls): """Return a new object instance.""" def get_value(name): if name in attributes: return attributes[name] else: value = cls['get'](name) return bind_method(value, instance) def set_value(name, value): attributes[name] = value attributes = {} instance = {'get': get_value, 'set': set_value} return instance 6 (Demo)

  39. Instances Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes" The class of the instance def make_instance(cls): """Return a new object instance.""" Match name against def get_value(name): instance attributes if name in attributes: return attributes[name] else: value = cls['get'](name) return bind_method(value, instance) def set_value(name, value): attributes[name] = value attributes = {} instance = {'get': get_value, 'set': set_value} return instance 6 (Demo)

  40. Instances Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes" The class of the instance def make_instance(cls): """Return a new object instance.""" Match name against def get_value(name): instance attributes if name in attributes: return attributes[name] Look up the name else: in the class value = cls['get'](name) return bind_method(value, instance) def set_value(name, value): attributes[name] = value attributes = {} instance = {'get': get_value, 'set': set_value} return instance 6 (Demo)

Recommend


More recommend