Week 8
Object-oriented programming
A class bundles data and the actions that belong with that data. Each object made from the class remembers its own data.
A class is a blueprint. An object is one thing made from it. Attributes are the data that object remembers. Methods are functions that belong to the object, and they receive the object itself as self. __init__ runs when you create the object and is where you set the starting attributes.
__str__ decides what the object looks like when printed. A property lets you read or write an attribute through a method, so you can reject a bad value. A class method belongs to the blueprint rather than one object. Inheritance is a new class that starts from an old one and changes a few things.
A fresh example: a class Lamp with a brightness attribute and a method that dims it, and that refuses a brightness below zero. Make two lamps and dim only one. The other stays as it was. That is the point of separate objects.
Problems
- Seasons of LoveDates and words, from libraries the spec names. The work is the minutes between two dates.
- Cookie JarOne object that remembers a capacity and a count, and refuses the actions that break its rules.
- CS50 ShirtificateA PDF library the spec names. The program asks for a name and draws a shirt with that name on it.