Week 5 subject 1: Python is an OOPL
- Traditionally programming languages are divided into 2 wide categories: procedural programming languages and object-oriented programming languages. Earlier languages are typically procedural languages, including BASIC, C, Fortran and many others. More recent languages are often OOPLs, among which there are C++, Java and Python.
- Programming in procedural languages is like writing a cook book, which includes a series of steps that are executed either sequentially or according to defined control flows.
- On the other hand OOPLs mimic the real world by defining 'objects' and how they interact with each other, so that we can stay reasonably far away from the tedious work of recreating all the 'steps' each time we need to describe the interactions, and let the 'objects' do it according to their definitions.
Week 5 subject 2: Classes
- 'Objects' are grouped into Classes. Classes define the common properties and capabilities of objects. Objects are 'instances' of their respective Classes. Example: 'Cars' is a Class, and my 2020 BMW 320i is an object of this Class, my friend's 2018 Honda CRV is another object of this Class.
- Instances of Classes are accessed in Python by declaring variables of a Class. A Class is essentially a type, like int or string.
- Just like there are different levels of group objects in real life, there can be hierarchies of Class definitions. Objects can be instances of different Classes.
- Parent Classes / Child Classes; Base Classes / Sub Classes.
- There's inheritance between parent and child classes.
- Define Classes, use pass to indicate you don't define the contents of the Class at this moment;
Class Car:
pass
Class Car(Vehicle):
define drive():
pass
define park():
pass
- You can define variables and functions inside Classes. They are called characteristics, or members of Classes. Particularly functions inside Classes are called 'methods' of Classes.
- Note: because of inheritance, instances of child classes can access members of parent classes.
No comments:
Post a Comment