Object Oriented Programming Overview

Classes can be used to represent many categories of things.

  • Characters in a Game

  • User Accounts

  • Types of Data

  • etc

Classes are a way to essentially create your own variable types. It requires you to think deeply about what categories of things your program is using, what attributes those things have, and what those things can do.

Classes are essentially blueprints of attributes. In this example, the entity “Student” is the class, and the 3 students are “instances” of these objects belonging to the class. Once the class is defined, we can create as many instances as we want.

Classes are made up of attributes and methods

Attributes

class Dog:
  # To create a Dog, give it a name, breed, and age. age is in years. 
  # If you don't give an age, we'll say it's 0 years old (a puppy).
  # All dogs also start as friendly!
  def __init__(self, input_name, input_breed, input_age = 0, input_friendliness = True):
    # self.name is this specific dog's name
    #these are attributes
    self.name = input_name
    self.breed = input_breed
    self.age = input_age
    self.is_friendly = input_friendliness
    self.friends = [] # an empty list to add other dogs to - see def become friends
    # this is a method (a function that would add 1 year to the dog's age
  
  def have_birthday(self):
    self.age = self.age + 1
    print("{name} had a birthday! {name} is {age} years old.".format(name = self.name, age = self.age))
  
  def become_friends(self, other_dog):
    if(other_dog.is_friendly):  #if yes, will add other dog to friends list
      self.friends.append(other_dog)
      other_dog.friends.append(self)
      print("{name} became friends with {other_name}!".format(name = self.name, other_name = other_dog.name))
    else:
      print("{other_name} did not want to become friends with {name}!".format(name = self.name, other_name = other_dog.name))

  # __repr__ will print out all of an object's attributes in a readable way
  def __repr__(self):
    description = "This {breed} named {name} is {age} years old and has {number_of_friends} friends.".format(breed = self.breed, name = self.name, age = self.age, number_of_friends = len(self.friends))
    if self.is_friendly:
      description += " {name} is a friendly dog.".format(name = self.name)
    else:
      description += " {name} is an unfriendly dog.".format(name = self.name)
    return description

#below are 3 INSTANCES of the dog class

dog_one = Dog("Sparky", "Golden Retriever", 5)
#calling the have.birthday function will print that  Sparky  is 6
dog_one.have_birthday()

# This dog will have self.is_friendly set to False
dog_two = Dog("Bruno", "Chihuahua", 10, False)

dog_three = Dog("Marshmallow", "German Shepherd", 2, True)

dog_one.become_friends(dog_two) #they will not become friends because Bruno is not friendly

dog_one.become_friends(dog_three) #they will become friends beacuse Marshamllow is friendly

print(dog_one) #this will output def __repr__

print(dog_two)

print(dog_three)