epicours/Prog/OOP.md

104 lines
1.7 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
---
author: Louis Gallet
Date: 30/11/2023
---
---
# Object Oriented Programming
<img src="https://i.imgur.com/qPsUYyd.jpg" alt="meme" style="zoom:50%;" />
## Introduction
- A programming paradigm
- Allows:
- Model objects
- Make objects interact with each other
- Objcts refer to complex variables
## Benefits and motivation
- Modularity
- Abstraction
- Productivity and reusability
- Security and encapsulation
## Concrete example
- When we develop a game with cars inside
- We develop the car one time and all other games can re-use the base of the car that we created
- We have access to characteristics
- Model
- Color
- Fuel type
- ...
- We also have the behavior of the car
- Start
- Accelerate
- Stop
## Object
- Data part: object characteristcs
- Attributes
- Properties
- Fields
- ...
- Code part = oject behavior
- Methods
- Procedure
## Class
- Object = classs instance
- Class
- New data type
- Collection of objects
- Figuratively
- Class = mold
- Object = cake
- Class at compilation VS object at runtime
```csharp
public class MyFirstClass {
public MyFirstClass() {} //constructor
}
MyFirstClass c = new MyFirstClass(); //new object
```
## Modelization
- First mandatory step
- Data modeling
- UML: Unified Modeling Language
- Class diagram
- Class Diagram example:![UML example](https://i.imgur.com/8zGdBkP.png)
- Code example:
```csharp
public class MuFirstCar {
private string _color;
public string GetColor(){
return _color;
}
public void SetColor(string color){
_color = color;
}
}
```
## OOP - Main principles
- Encapsulation
- Abstraction
- Inheritance (Héritage in french)
- Polymorphism