For the past few weeks, we have learned a lot of the basic concepts of object oriented programming.
Classes
Classes was a concept also learned in CSC108. A class declaration is creates a new class of creating objects of that type. A class can contain various methods and different attributes. Lets use an example of the class Dog.
class Dog:
"""A class that contains the dog's name, age and species
"""
def __init__(self, name, age, species ):
""" (Dog, str, int, str) -> NoneType
Initialize the dog's name, age in years and the species
"""
self.name = name # instance variable unique to each instance
self.age = age
self.species = species
We first initialize our class by using the __init__ method. We initialize the dog's name, age and the species. The parameter self refers to the instance of the class. How does it work?
For example:
>>> dog = Dog('Max', 3, 'Poodle')
>>> dog.age
3
The self refers to the variable dog. We can use the self.name, self.age and self.species in our methods we create.
Methods
What types of properties does the dog class possess?
When it is the dog's birthday, the dog grows a year older. How do we change it's age? We can use a function to indicate that it's the dog's birthday and add a year to it's age.
def birthday(self):
""" (Dog) -> NoneType
Add a year to the dog's age
"""
self.age += 1
Now if we call birthday on dog, it will add an additional year to the dog.
>>> dog.birthday()
>>> dog.age = 4
A special case of methods are __str__, __eq__, and __repr__. __str__ can help create a string representation for the class. __eq__ can compare if two objects of the same class have the same properties, and __repr__ returns a str that is in Python code form which is easy to read for Python.
Inheritance
Perhaps we want to create a general class not only for dogs, but for all pets. This general class can be used for specific classes on specific animals. All pets have a name, age, and species so we can create a general class called Pet.
class Pet:
"""A class that contains the pet's name, age and species
"""
def __init__(self, name, age, species ):
""" (Pet, str, int, str) -> NoneType
Initialize the pet's name, age in years and the species
"""
self.name = name # instance variable unique to each instance
self.age = age
self.species = species
The class Dog can inherit all the properties of Pet.
class Dog(Pet):
"""A class that contains the dog's name, age and species
"""
def __init__(self, name, age, species ):
""" (Dog, str, int, str) -> NoneType
Initialize the dog's name, age in years and the species
"""
Pet.__init__(name, age, species)
Now the class Dog inherits all the attributes of the class Pet. You can do that with various other classes of pets such as cats, fishes, or even create a pet of your own!
Object oriented programming is very flexible and one can create a lot of different programs.
A very detailed and thorough summary of object oriented programming. Well done! A nice and creative choice of examples. Definitely made the explanation a lot more intuitive and easy to grasp.
ReplyDelete