این دستورات از نظر پایتون بدون خطا اجرا میشوند؛ اما از نظر منطقی مقادیر نامعتبری هستند. شارژ باتری یک تلفن همراه نمیتواند بیشتر از 100 درصد یا مقدار منفی داشته باشد.
مشکل اینجاست که ویژگیهای کلاس بهصورت آزاد در دسترس هستند و هیچ کنترلی بر نحوۀ تغییر آنها وجود ندارد. این موضوع میتواند باعث بروز خطاهای منطقی در برنامه شود و در برنامههای بزرگ، نگهداری و توسعۀ آن را دشوار کند.
برای حل این مشکل، میتوان برخی ویژگیها را بهگونهای تعریف کرد که دسترسی مستقیم به آنها از بیرون کلاس محدود شود و تغییر آنها فقط از طریق متدهای مشخص انجام گیرد. به این نوع ویژگیها، ویژگی خصوصی (Private) گفته میشود.
ویژگیهای خصوصی (Private)
در پایتون، ویژگیهای خصوصی با استفاده از دو زیرخط (_ _) در ابتدای نام آنها تعریف میشوند.
برای مثال، در کلاس Mobile میتوان ویژگی باتری را بهصورت زیر تعریف کرد:
self._ _battery = 100
با این کار:
دسترسی مستقیم به ویژگی از بیرون کلاس محدود میشود.
تغییر مقدار آن فقط از طریق متدهای کلاس امکانپذیر است.
کنترل منطقی روی دادهها برقرار میشود.
ویژگیهای محافظتشده (Protected)
در بخشهای قبل دیدید که ویژگیهای یک کلاس میتوانند عمومی (Public) یا خصوصی (Private) باشند.
اما گاهی شرایطی وجود دارد که:
نمیخواهید یک ویژگی از بیرون کلاس مستقیماً استفاده یا تغییر داده شود.
ولی میخواهید کلاسهای فرزند بتوانند به آن ویژگی دسترسی داشته باشند.
برای چنین حالتی از ویژگیهای محافظتشده (Protected) استفاده میشود. در زبان پایتون، اگر نام یک ویژگی با یک زیرخط (_) شروع شود، آن ویژگی بهعنوان Protected در نظر گرفته میشود.
self._status = "on"
جدول ۳
سطح دسترسی
نحوه نوشتن
توضیح
عمومی (Public)
battery
دسترسی آزاد از همهجا
خصوصی (Private)
_ _battery
دسترسی فقط داخل همان کلاس
محافظتشده (Protected)
_status
دسترسی از درون کلاس و کلاسهای فرزند
صفحه ۲۵۱
این نوع نامگذاری نشان میدهد که ویژگی برای استفادۀ داخلی کلاس و کلاسهای فرزند طراحی شده است و نباید مستقیماً از بیرون کلاس تغییر داده شود.
تا اینجا با سه سطح دسترسی به ویژگیهای یک کلاس آشنا شدید:
سطوح دسترسی مختلف شامل ویژگیهای عمومی، خصوصی و محافظتشده، به شما کمک میکنند تا دسترسی به دادههای یک کلاس را کنترل کنید. به همین دلیل لازم است جزئیات داخلی کلاسها پنهان شوند و دسترسی به دادهها بهصورت کنترلشده انجام گیرد. این مفهوم در برنامهنویسی شیءگرا با عنوان کپسولهسازی (Encapsulation) شناخته میشود. کپسولهسازی به معنای:
پنهانسازی جزئیات داخلی یک کلاس و کنترل نحوۀ دسترسی به دادهها و رفتارهای آن میباشد و یکی از مفاهیم اصلی برنامهنویسی شیءگرا محسوب میشود.
Page 250
These statements run without error as far as Python is concerned; but logically they are invalid values. The battery charge of a mobile phone cannot be more than 100 percent or have a negative value.
The problem is that the properties of the class are freely available and there is no control over how they are changed. This can cause logical errors in the program and make its maintenance and development difficult in big programs.
To solve this problem, some properties can be defined in a way that direct access to them from outside the class is limited and changing them is done only through specific methods. This kind of property is called a private property.
Private properties
In Python, private properties are defined with two underscores (_ _) at the start of their name.
For example, in the class Mobile the battery property can be defined as follows:
self._ _battery = 100
By doing this:
Direct access to the property from outside the class is limited.
Changing its value is possible only through the methods of the class.
Logical control over the data is established.
Protected properties
In the earlier sections you saw that the properties of a class can be public or private.
But sometimes there are conditions where:
you do not want a property to be used or changed directly from outside the class.
but you want the child classes to be able to reach that property.
For such a case protected properties are used. In Python, if the name of a property starts with one underscore (_), that property is considered Protected.
self._status = "on"
Table 3
Access level
How to write it
Description
Public
battery
Free access from everywhere
Private
_ _battery
Access only inside the same class
Protected
_status
Access from inside the class and the child classes
Page 251
This kind of naming shows that the property is designed for the internal use of the class and the child classes and must not be changed directly from outside the class.
So far you have got to know three access levels for the properties of a class:
The different access levels, including public, private and protected properties, help you control access to the data of a class. For this reason the internal details of classes need to be hidden and access to the data done in a controlled way. This concept is known in object-oriented programming as encapsulation. Encapsulation means:
hiding the internal details of a class and controlling how its data and behaviours are accessed, and it counts as one of the main concepts of object-oriented programming.