For this week, i'd like to indicate that i'd want my Object Oriented Programming SLOG that i've previously written to be marked which is linked here.
In addition to that post, i'd like to add more details and go into depth into the concept of OOP. I've gotten positive feedback that my post was very informative and useful but i'd like to demonstrate these definitions into examples that we have come across throughout the course to get an idea of what it's really talking about and to help those who learn through examples instead of merely memorizing the definition, I know I do.
I will show all of these examples based off of our assignment with Subtract Square, as you all know was our Assignment 1 which was a very good exercise to practice these new concepts in OOP.
So firstly, in Subtract Square, we have encountered many Classes which is defined in my previous post.
Examples of Classes in our assignment are shown in the General Classes that we had to make for the game followed by its docstring that informs us what types, methods..etc are in this particular class, such as:
class GameView:
'''
A game view for a two-player, sequential move, zero-
sum, perfect-information game.
'''
class Strategy:
'''Interface to suggest moves for a GameState.
Must be subclassed to a concrete strategy. Our
intention is to provide a uniform interface for
functions that suggest moves.
'''
class GameState:
'''(Insert docstring of all the methods in this class...
you get the idea)
'''
As stated in the previous post, these classes contain methods and also __init__, __str___, __repr__ etc and whatever relevant methods the programmer wishes to include into this class.
The second thing we've encountered in Subtract Square that is an OOP is Inheritance (Refer to previous post for the definition). We encountered this in Subtract Square when we were writing specific methods of Subtract Square in its individual Subclass.
Here's an example of how Inheritance and Subclasses came into play in our assignment:
We had to make a subclass for the general class because the general class is vague enough that we have to refer to a subclass for specific games, situations, etc. Like our assignment, we made a general class of any particular Game, but what if we wanted to play a specific game? We use Subclasses because every game has different rules and in these subclasses we can use inheritance or override these methods to correspond to the specific game we wanted to play, in this case Subtract Square:
from game_state import GameState
from subtract_square_move import SubstractSquareMove
from math import sqrt
from random import Randint
#This is a crucial step, not importing causes Errors and will not be
#able to inherit from your general class if you did not import it
class SubstractSquareState(GameState):
#General structure of subclass referred to previous post
#"class SubclassName(ClassName):"
'''The state of a Subtract Square game
current_total: int --- total to be subtracted from
'''
So above shows an example of how we created a Subclass in our Assignment Subtract Square. Next is how does this Subclass use Inheritance. Inheritance can be represented in the __init__ method such as so:
def __init__(self, p, interactive=False, current_total=0):
'''(SubstractSquareState, int, str) -> NoneType
(insert docstring here)
'''
GameState.__init__(self, p)
#Notice that I cut out most of the code in __init__
#I just wanted to emphasize the Inheritance part of this code
So the code above that is in the Subclass SubtractSquareState shows inheritance in its __init__method. In this example, it is inheriting GameState and also adding another argument 'p'. This causes the Subclass to inherit everything the general class has but also adding any argument the programmer feels is appropriate for this specific game that is not in the general class because it is not general enough for each game to use.
I hope these examples are relatable and explain these major concepts of OOP that we've come across in this class. If you have any further suggestions, commentary, or opinions, feel free to comment. Also refer back to my post which is linked at the beginning of this post to get the general idea and definitions of these concepts. Happy Programming Sloggers!
No comments:
Post a Comment