oop
play

OOP Caroline Lemieux March 7th 2019 Announcements Homeworks + - PowerPoint PPT Presentation

OOP Caroline Lemieux March 7th 2019 Announcements Homeworks + Labs Homework 5 is due Friday 3/8 Lab 6 is due Friday 3/8 Projects Ants due next Thursday! (Midpoint due next Monday) Midterm 2 Approaching Date TBD, March 19th or 20th.


  1. OOP Caroline Lemieux March 7th 2019

  2. Announcements Homeworks + Labs Homework 5 is due Friday 3/8 Lab 6 is due Friday 3/8 Projects Ants due next Thursday! (Midpoint due next Monday) Midterm 2 Approaching 😲 Date TBD, March 19th or 20th. Start studying! Get help with concepts @ OH/Lab/CSM/1-1 Advising

  3. Concept Check 1. What is the relationship between a class and an instance? class Professor: degree = “PhD” def __init__(self, name): self.name = name def lecture(self, topic): print(“Today we’re learning about “ + topic) dumbledore = Professor(“Professor Dumbledore”)

  4. Concept Check 1. What is the relationship between a class and an instance? class class Professor: degree = “PhD” def __init__(self, name): self.name = name def lecture(self, topic): print(“Today we’re learning about “ + topic) dumbledore = Professor(“Professor Dumbledore”) instance

  5. Concept Check 1. What is the relationship between a class and an instance? class class Professor: degree = “PhD” def __init__(self, name): self.name = name def lecture(self, topic): print(“Today we’re learning about “ + topic) dumbledore = Professor(“Professor Dumbledore”) instance creating an instance

  6. Concept Check 2. What is the difference between an instance attribute and a class attribute? class Professor: degree = “PhD” def __init__(self, name): self.name = name def lecture(self, topic): print(“Today we’re learning about “ + topic) dumbledore = Professor(“Professor Dumbledore”)

  7. Concept Check 2. What is the difference between an instance attribute and a class attribute? class Professor: class attribute: all Professors have PhD’s degree = “PhD” instance def __init__(self, name): attribute: each Professor has their self.name = name own name def lecture(self, topic): print(“Today we’re learning about “ + topic) dumbledore = Professor(“Professor Dumbledore”)

  8. Concept Check 3. What is the difference between a class method and a function? class Professor: degree = “PhD” def __init__(self, name): self.name = name def lecture(self, topic): print(“Today we’re learning about “ + topic) dumbledore = Professor(“Professor Dumbledore”)

  9. Concept Check 3. What is the difference between a class method and a function? class Professor: degree = “PhD” def __init__(self, name): method: only self.name = name Professors can lecture def lecture(self, topic): print(“Today we’re learning about “ + topic) dumbledore = Professor(“Professor Dumbledore”)

  10. Concept Check 3. What is the difference between a class method and a function? class Professor: degree = “PhD” def __init__(self, name): method: only self.name = name Professors can lecture def lecture(self, topic): print(“Today we’re learning about “ + topic) dumbledore = Professor(“Professor Dumbledore”) bound method dumbledore.lecture(“general wisdom”) Professor.lecture(dumbledore, “general wisdom”) function

  11. Concept Check - Summary 1. What is the relationship between a class and an instance? ○ An instance is a single object belonging to some class. 2. What is the difference between an instance attribute and a class attribute? ○ An instance attribute is specific to a certain instance . They can only be accessed through that instance. Changing an instance attribute of one instance does not affect the instance attributes of other instances. ○ Class attributes are shared among all instances of a class. They can be accessed either using the class name or through an instance. Class attributes can only be changed by accessing it using the class name. 3. What is the difference between a class method and a function? ○ A class method is a function that belongs to a class and can only be called on an instance of that class . A function has no association with any objects.

  12. Dot Notation Lookup Evaluate name to the left of dot name evaluates to instance name evaluates to class Look through Look through instance attributes class attributes for attribute for attribute name of a class no instance or an instance attribute name not in attributes attribute found Error foo.bar

  13. Attendance links.cs61a.org/caro-disc

  14. Inheritance ● sometimes you have different classes that have very similar attributes and methods! pet cat dog corgi rottweiler calico tabby ● inheritance allows objects to have all the attributes of another class

  15. Inheritance class Dog(Pet): class signature: class(superclass) def __init__(self, name, owner): Pet.__init__(self, name, owner) overriding a method: self.tricks = [] redefining a method dogs are just like any other that was inherited pet, but they also have a set of tricks! def talk(self): print(self.name + ‘ says woof!’) dogs can learn tricks! (cats can’t... boooo) def learn_trick(self, trick): self.tricks.append(trick)

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