Hello guys, I finished an abstract about OOP and I want to share my annotations with you. It's my first post, then if you find any mistakes, comment. I hope you enjoy it!
Pre-OOP
The programmer can put related functions together in one file, but the grouping is just a convention and the compiler does not enforce it in a significant way.
OOP
In OOP, the logical arrangement of the code is changed. Instead of an informal arrangement of functions into different files, functionality is officially and tightly grouped with the type that it operates on.
You can use easily libraries — Code Re-Use
Modularity features
The clueless Client Test = The language should encourage the right client behavior, discourage common client errors, and politely alert the client if there is an error
Basic concepts
Class
A class is like a type in the classical language. Instead of just storing size and structural information for its data, a class also stores the operation which will apply to the data.
Class = Storage + Behavior
It creates a logical coupling between data and the operations on that data.
Bundle the verbs with their nouns.
Names of classes always start with capital characters
constructor is a type of subroutine called to create an object.
It prepares the new object for use.
Objects
An object is a run-time value that stores the state and belongs to some class.
Objects know what class they belong to, and so they automatically know what operations they are capable of.
The word "instance" is another word of "object"
Example of how to create an object instance in Javascript
classCar{constructor(_factory,_model){this.factory=_factorythis.model=_model}}// "New" show you will create an object.constford=newCar('Ford','Mustang')
Massage and Method
OOP uses "messages" instead of function calls.
Method ≠ Functions
An object to operate on itself.
Object is like a Receiver.
It knows what operations it can perform, because it knows its class, and the class defines all methods or operations for its instances.
Example of class in Javascript
classCar{constructor(_factory,_model){this.factory=_factorythis.model=_model}turnOn(){return"Car is turning on... Turned on."}turnOff(){return"Car is turning off... turned off"}}// "New" show you will create an object.constford=newCar('Ford','Mustang')
Message Send Syntax
Almost all languages use syntax: appending the message to the desired receiver with a dot.
Example
classCar{constructor(_factory,_model){this.factory=_factorythis.model=_model}turnOn(){return"Car is turning on... Turned on."}turnOff(){return"Car is turning off... turned off"}}
C++, Java and Javascript syntax
objectInstance.method();
Python syntax
objectInstance.method()
PHP syntax
$objectInstance->method();
Important principles
Encapsulation
Refers to protecting the internals of an object from direct manipulation by the client.
The client can send messages, but he can not change the bits in an object.
The object's state is only touched by its own methods.
// In any cases you need to use babel-presets.classCar{factory;model;#year=2021constructor(_factory,_model){this.factory=_factorythis.model=_model}getfactory(){returnthis.factory}getmodel(){returnthis.model}getyear(){returnthis.#year}turnOn(){return"Car is turning on... Turned on."}turnOff(){return"Car is turning off... turned off"}}
I will explain about public, private, and protected attributes another day. They also are really important concepts.
Top comments (0)