cs 61a topical review
play

CS 61A Topical Review Object Oriented Programming Albert Xu Slides: - PowerPoint PPT Presentation

CS 61A Topical Review Object Oriented Programming Albert Xu Slides: albertxu.xyz/teaching/cs61a/ OOP Formal Definitions Well need to know these words to write Dog Class - a template for an object represents attributes of a dog,


  1. CS 61A Topical Review Object Oriented Programming Albert Xu Slides: albertxu.xyz/teaching/cs61a/

  2. OOP Formal Definitions We’ll need to know these words to write Dog • Class - a “template” for an object • represents attributes of a dog, and the actions( methods ) it can take

  3. OOP Formal Definitions We’ll need to know these words to write Dog • Class - a “template” for an object • represents attributes of a dog, and the actions( methods ) it can take • Instance - each object built using that template • a specific object, e.g. a specific dog!

  4. OOP Formal Definitions We’ll need to know these words to write Dog • Class - a “template” for an object • represents attributes of a dog, and the actions( methods ) it can take • Instance - each object built using that template • a specific object, e.g. a specific dog! • Attributes

  5. OOP Formal Definitions We’ll need to know these words to write Dog • Class - a “template” for an object • represents attributes of a dog, and the actions( methods ) it can take • Instance - each object built using that template • a specific object, e.g. a specific dog! • Attributes • instance • class

  6. OOP Formal Definitions We’ll need to know these words to write Dog • Class - a “template” for an object • represents attributes of a dog, and the actions( methods ) it can take • Instance - each object built using that template • a specific object, e.g. a specific dog! • Attributes • instance • class • Method - a function bound to a particular class • an action a dog can take, e.g. “drink_water”

  7. On Inheritance some things to remember superclass Animal By default, subclasses inherit their parent subclass class’ attributes and methods Dog Retriever Dog inherits from Animal

  8. On Inheritance some things to remember superclass Animal By default, subclasses inherit their parent subclass class’ attributes and methods Dog Think about looking up attributes and methods of an object like looking up Retriever variable in environment diagrams! Check the object itself, then its parent object, then that object’s parent, etc. Animal inherits from Dog

  9. On Inheritance some things to remember superclass Animal By default, subclasses inherit their parent class subclass attributes and methods. This means that they share attributes from the parent - they do not copy them! Dog Think about looking up attributes and methods of an object like looking up Poodle Retriever variable in environment diagrams! Check the object itself, then its parent object, then that object’s parent, etc. Animal inherits from Dog

  10. Object Retriever Now write the Retriever object! 1) What should Retriever ’s 2) Does Retriever need a constructor look like? drink_water method? (hint: how should we set color?) We need to set the color attribute of itself to a specific color, “golden”! Because it’s in the subclass, this constructor will override the constructor inherited from the Dog class!

  11. Object Retriever Now write the Retriever object! 1) What should Retriever ’s 2) Does Retriever need a constructor look like? drink_water method? (hint: how should we set color?) Nope! Since we’re fine with the We need to set the color attribute of behavior of the original itself to a specific color, “golden”! Because it’s in the subclass, this drink_water method, it’s constructor will override the automatically inherited from the constructor inherited from the Dog parent class, Dog ! class!

  12. Object Retriever Now write the Retriever object! 1) What should Retriever ’s 2) Does Retriever need a constructor look like? drink_water method? (hint: how should we set color?) Nope! Since we’re fine with the We need to set the color attribute of behavior of the original itself to a specific color, “golden”! Because it’s in the subclass, this drink_water method, it’s constructor will override the automatically inherited from the constructor inherited from the Dog parent class, Dog ! class! class Retriever(Dog): def __init__(self); __________ = ________

  13. Object Retriever Now write the Retriever object! 1) What should Retriever ’s 2) Does Retriever need a constructor look like? drink_water method? (hint: how should we set color?) Nope! Since we’re fine with the We need to set the color attribute of behavior of the original itself to a specific color, “golden”! Because it’s in the subclass, this drink_water method, it’s constructor will override the automatically inherited from the constructor inherited from the Dog parent class, Dog ! class! class Retriever(Dog): def __init__(self); self.color = “golden”

  14. Anatomy

  15. Anatomy class header

  16. Anatomy class header class attributes

  17. Anatomy class header class attributes constructor!

  18. Anatomy class header class attributes constructor! instance method

  19. Anatomy class header class attributes constructor! instance method

  20. Anatomy class header class attributes constructor! instance method Two ways to call a method : albert = Dog(“green”)

  21. Anatomy class header class attributes constructor! instance method Two ways to call a method : albert = Dog(“green”) Dog.drink_water(albert) unbound method call

  22. Anatomy class header class attributes constructor! instance method Two ways to call a method : albert = Dog(“green”) Dog.drink_water(albert) albert.drink_water() unbound method call bound method call

  23. *taken whole from a past CSM 61A MT2 Review

  24. Tricky WWPD

  25. Tricky WWPD

  26. Tricky WWPD

  27. Tricky WWPD

  28. Tricky WWPD

  29. Tricky WWPD

  30. Tricky WWPD

  31. Tricky WWPD

  32. Tricky WWPD

  33. A quick aside…

  34. A quick aside… Why does this call even work? Shouldn’t we be passing in an instance?

  35. A quick aside… Why does this call even work? Shouldn’t we be passing in an instance? turns out Python doesn’t check whether self is actually an instance! You can pass in anything to an unbound call. Of course, if you ever use self later on you’ll run into issues….

  36. A quick aside… Why does this call even work? Shouldn’t we be passing in an instance? turns out Python doesn’t check whether self is actually an instance! You can pass in anything to an unbound call. Of course, if you ever use self later on you’ll run into issues…. Also, we can do this:

  37. A quick aside… Why does this call even work? Shouldn’t we be passing in an instance? turns out Python doesn’t check whether self is actually an instance! You can pass in anything to an unbound call. Of course, if you ever use self later on you’ll run into issues…. Also, we can do this:

  38. A quick aside… Why does this call even work? Shouldn’t we be passing in an instance? turns out Python doesn’t check whether self is actually an instance! You can pass in anything to an unbound call. Of course, if you ever use self later on you’ll run into issues…. Also, we can do this: because self is actually just a convention in Python, not an enforced rule.

  39. Problem 1

  40. Problem 1 A new pet!

  41. Problem 1 A new pet! Error

  42. Problem 1 A new pet! Error Error

  43. Problem 1 A new pet! Error Error Quack

  44. Problem 1 A new pet! Error Error Quack A new pet!

  45. Problem 1 A new pet! Error Error Quack A new pet! ‘DaisyDaisy’

  46. Problem 1 A new pet! Error Error Quack A new pet! ‘DaisyDaisy’ What is wrong?

  47. Problem 1 Solution A new pet! Error Error Quack A new pet! ‘DaisyDaisy’ What is wrong? None

  48. Problem 2

  49. Problem 2 Solution “Sir, I work” Peon “Maam, I work” Peon, I work “to gather wealth” Comrade Peon, I work Peon “Comrade Peon, I work”

  50. Thanks for coming. Have a great rest of your week! :) Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend