CS 61A Topical Review Object Oriented Programming Albert Xu Slides: albertxu.xyz/teaching/cs61a/
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
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!
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
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
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”
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
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
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
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!
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!
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); __________ = ________
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”
Anatomy
Anatomy class header
Anatomy class header class attributes
Anatomy class header class attributes constructor!
Anatomy class header class attributes constructor! instance method
Anatomy class header class attributes constructor! instance method
Anatomy class header class attributes constructor! instance method Two ways to call a method : albert = Dog(“green”)
Anatomy class header class attributes constructor! instance method Two ways to call a method : albert = Dog(“green”) Dog.drink_water(albert) unbound method call
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
*taken whole from a past CSM 61A MT2 Review
Tricky WWPD
Tricky WWPD
Tricky WWPD
Tricky WWPD
Tricky WWPD
Tricky WWPD
Tricky WWPD
Tricky WWPD
Tricky WWPD
A quick aside…
A quick aside… Why does this call even work? Shouldn’t we be passing in an instance?
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….
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:
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:
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.
Problem 1
Problem 1 A new pet!
Problem 1 A new pet! Error
Problem 1 A new pet! Error Error
Problem 1 A new pet! Error Error Quack
Problem 1 A new pet! Error Error Quack A new pet!
Problem 1 A new pet! Error Error Quack A new pet! ‘DaisyDaisy’
Problem 1 A new pet! Error Error Quack A new pet! ‘DaisyDaisy’ What is wrong?
Problem 1 Solution A new pet! Error Error Quack A new pet! ‘DaisyDaisy’ What is wrong? None
Problem 2
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”
Thanks for coming. Have a great rest of your week! :) Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/
Recommend
More recommend