- Object Oriented Programming
- Class (Character Methods – Textfile)
- Class (Character with STR – Textfile)
- Gorgatron Game
Object Oriented Programming (A-Level):
Object Oriented Programming (OOP) refers to a type of software design in which programmers define the data type of a data structure, and also the types of functions that can be applied to the data structure.
An object-oriented program is composed of a number of interacting objects, each of which is responsible for its own data and the operations on that data.
In object oriented programming, we define new data types called ‘Classes’.
- We decide the attributes of the class.
- We decide the methods of the class.
- Which then we make variables, which are instances of the new class.
- Class: A data type defined by the programmer (can be considered a template for new variables.
- Instance: A new variable of that class.
- Attribute: A data value stored in the variable.
- Method: An action that can be carried out by variables of that class.
Defining a class can be visualised like this:
Let’s create a character with a name and a randomised strength.
First we define character:
We then must build the ‘constructor’:
- This tells the computer how to ‘construct’ a new variable of this class.
- In python, it is called ‘__init__’ (the initialiser’)
- We then list all the attributes we want the variable to have.
We don’t know what any variable is going to be called so we use ‘self’ to stand in for the variable name throughout the class definition. It’s also a python thing.
Under the constructor, we set the attributes we want to have.
Let’s complete the constructor:
- Name is a parameter; this is because the user will choose the name of the character.
- Strength is a random integer between 1 to 10.
- Weapons is an empty list.
We must create an instance: (which will make a new variable of our class)
This is placed in the main program:
Notice how the class name & the instance begins with a capital letter.
We have made a variable called ‘hero’. If we try to print out the variable, it will not work. This is because we haven’t designed anything that will let us represent the variable as a printable string.
This is where ‘__str__’ comes in.
Notice how this function returns the string value. We pass ‘self’ through the function. This allows us to convert self.strength into a string and return our character’s name & strength to the main program.
Now let’s design some methods for our class:
The first method we are going to create is going to be named ‘showinfo’
The main objective of this method is to print a full list of information about our character. This will be a procedure, not a function.
This will print our character’s name, strength and weapons when we use the method:
The second method we are going to create is going to be named ‘setweapon’.
The user’s choice passed as a parameter. We can then call the ‘showinfo’ method to see the new character information.
This method adds strength to the character when a weapon is chosen.
Hopefully you understand classes a little more.
200 total views, 1 views today