Object Oriented Programming (OOP) is the fifth and last core-building blocks of software. Together with the other four building blocks, you can create anything in software.
Before we go more in depth on OOP, please watch the following video first. This is a video I created while at Coding Dojo and has been a key in helping students understand what OOP is.
In this 10 minute video, you'll learn about the following:
What Object Oriented Programming is What a Class is and why the term 'blueprint' is often used to describe a class Class attributes (what it can "have") Class methods (what if can "do") Objects or Instances Public vs Private attributes Public vs Private methods Constructor (in Python this is called an initializer) Destructor (a function that gets called when the object is removed - some languages have this) What an inheritance is What a parent class and child class means How a private attributes/method gets passed to its children (it doesn't). Introducing protected attributes/methods - these get passed to the children class
There are a lot of concepts covered in this short video.
As a starting point, let's have you create a simple class called 'Barrack' in Python:
def __init__(self, name):
self.hp = 100
self.x = 0
self.y = 0
self.name = name
self.mp = None
def move_x(self, val):
self.x += val
def move_y(self, val):
print("X is", self.x, "Y is", self.y)
print("Barrack ", self.name, " is exploding!")
obj1 = Barrack("treo")
obj2 = Barrack("oreo")
Note that in all of Python's methods, we include self as the first argument to each of its methods.
Now that you've learned a bit about OOP in Python, it's your turn to create a few objects.
In this challenge, create a class called Temple with the following attributes:
Create three attributes: hp (this is health points) - set this to be 200 when the object is initialized. Also create x and y where both are set as 20, 50 respectively when they are created, as well as name which should be set to None in the beginning Create two public methods: double_x(), double_y(), which when called doubles its x/y value. Create another public method called: changeName() where you can pass a new name for the temple
Your code, once done, should meet all the test cases.