...

/

التغليف في Python

التغليف في Python

تعرف على التغليف في Python، بما في ذلك الأعضاء العامة والخاصة، وفوائدها، والأمثلة العملية.

سنغطي ما يلي...

Encapsulation is a key element of OOP. It means that the data and the methods are grouped together under the single unit called class. This concept also assists in limiting the direct access of some of the object’s components, as this might lead to the changes in data. In Python, encapsulation is implemented through public, and private attributes and methods.

أعضاء الجمهور

في Python، جميع الأعضاء عامة افتراضيًا. يمكن الوصول إلى الأعضاء العامة من أي مكان، داخل وخارج الصف. لنفهم الأعضاء العامة في التغليف بمساعدة صف Person :

Press + to interact
class Person:
name = "Public Name" # Public member
def __init__(self, name):
self.name = name # Public attribute
# Public method
def public_method(self):
print("Public Method")
print(f"Name: {self.name}")
person = Person("Bob")
# Accessing public member
print(person.name)
person.public_method()

في المثال أعلاه، لدينا فئة ...