Posts

Showing posts from January, 2019

Passing Parameters into a object in Python

Image
Once objects are created from classes in Python, they can be made unique from other objects made from the same class by giving them different data. This is done by giving the attributes unique data. For example, an attribute of a class could be 'name'. So we could give different objects made with that class, different names. This is done by adding data to the class by putting this data in brackets next to the class name when it is called. bob = human("Bob", "Male", "British")

Programming a Class in Python

Image
To define a class, it is similar to a function: class [insert name of class here](): Then underneath this attributes can be defined. A class constructor has to be used: def __init__ Self must be then added to the brackets first, this is used so each individual object that is created using this class can keep hold of its own attributes over time - so the object can refer to its 'own data': (self, [attributes here]): Attributes are assigned to variables so they can be accessed throughout the object.  (self, nationality = "British", gender = "male" ):             self.nationality = nationality             self.gender = gender Once the attributes have been created, methods (functions of the class) can also be added, this example is what would be required to make the 'human object' speak: def speak(self):          print("Hello my gender is: ", self.gender) Once the class has been created with attribut

UML Diagram

Image
A UML diagram represents the class. The top section states the class name, the second section shows the classes' attributes and then the third section displays the methods. Using the example object of a human:

Advantages of OOP

Advantages of OOP, using the example of a racing game: - Instead of individually creating variables to describe an object and then creating similar variables to describe similar objects, a singular blueprint (class) can be created which multiple objects can be created off. - This way we are only defining the car once - more efficient for programmers, using less storage and easy to keep track of data.

Object Oriented Programming - Key Terms

Object-Oriented Programming - involves solutions being constructed by means of objects that interact with each other. Using objects means programs can be broken down into parts. Classes - templates used in OOP to construct objects. Attributes - variables associated with it. Methods - subroutines that form the actions an object carries out. Instantiation - The creation of objects is called ‘instantiation’. When creating an object we create an instance of the class. Inheritance - where a class retains the methods and attributes of its parent class as well as having its own. Means one class can be coded and used multiple times for similar objects. Encapsulation - is the process of keeping objects attributes private so they can only be accessed and changed via public methods.  Means object can only behave in expected ways, no unforeseen circumstances. Polymorphism - means that objects of different types can be treated in the same way.