Saturday, 4 April 2015
Post 4
Object Oriented Programming, as I mentioned in an earlier post, brought a lot of sense to programming for me. It explained how things like strings and lists actually worked and it helped understand how a programming worked in things like games and simulations.
Object Oriented programming allows you to define a certain 'object' and give it the parameters you wish and methods that help you manipulate said parameters as well define interactions with other objects. For example, the Subtract Square class allowed us to define a subtract square game, which had parameters for the score and the players. It also had methods that allowed us to alter the game state, determine possible moves, determine whether the game was over etc.
Some of the necessary methods included the __init__ method, which would initialize the parameters of the object when it was created by the user. the methods __str__, __repr__ and __eq__ overrode the existing string, repr and eq methods so that the object could be printed, repr could be used and '=' could be used to determine equivalence. These method were not used often but were important to avoid errors and were methods that were necessary to any object,
The idea of overriding is closely related to Inheritance, which is a way to efficiently create objects that were closely related to other objects. For Subtract Square, we first made a general Game class that had parameters and methods that were relevant to any game in general. Subtract Square inherited the game class and overrode some of the methods (e.g. __str__, __eq__ etc.) to make the code more specific to the game. However, by inheriting, we saved a lot of code and made the program more efficient and more organized.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment