Day 20 NuCamp — OOP — Classes
Class | Class and Instance Attributes |
The purpose of a class is to create an object
max_hp = 4000 is a CLASS ATTRIBUTE // is attached to the class itself.
Accessing attribute of stored object:
Class and Instance Attributes and Constructor Methods
Instance attributes | Constructor Method (Above code commented out)
Instance attributes are stored per instance as opposed to by class.
When you change an instance attribute, it doesn’t change the other instances.
Define an instance by initializing __init__( ) inside called a constructor method. (Line 16) — this method must also have a parameter list.
(self , name, hp) — First parameter will refer to object being created. By convention — referred to ‘self’. # This is a header for the constructor method.
“Class is like an object factory”
“Constructor method is what initializes each object created in the factory.”
self.score attribute attribute will start at 0 regardless for both player object, so we don’t need to pass a value via parameter list.
Line 22 -23
Assigning player 1 to the value of the object (Player) with specific arguments of name and HP
Now we can grab each object’s attribute by declaring the variable via dot notation with the attribute.
Now we can manipulate each object :