Quick actions

cmd+k|ctrl+k

Navigation

Languages

Encapsulation

Snippet info

Language

Python

Visibility

public

Author

fpuleano

Created

2023-05-08T13:42:21.038907Z

Updated

2023-05-08T14:28:34.069293Z

# The 4 Pillars Of OOPP
# Encapsulation is the binding of data and the functions that manipulate that data.
# We encapsulate into one big object so that we can keep everything in that box that user's or code, or other machines...
# ...can interact with.
# This data and functions are what we call attributes and methods

class PlayerChar:  # here we are able to encahpsulate our PlayerChar by having name and age attributes
  def __init__(self, name, age):
   self.name = name 
   self.age = age
    
  def run(self): # here we have fuctions that can act upon those name and age attributes
      print('run')
 
  def speak(self): # here we have a speak method, and it can now use name and age
      print(f'my name is {self.name}, and i am {self.age} years old')

player1 = PlayerChar('Fred', 66)

player1.speak()

# if we didn't have the class playerChar...we would only have two variables and a couple of functions.

# but by using class Playerchar...we have packaged it all up into a blueprint for creating multiple objects, we can mimic what happens in the real world.
INFO