epicours/Prog/OOP.md

1.6 KiB

Object Oriented Programming

meme

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
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

    • Code example:

      public class MuFirstCar {
        private string _color;
        public string GetColor(){
          return _color;
        }
        public void SetColor(string color){
          _color = color;
        }
      }