در دنیای واقعی، اشیای مشابه (مانند خودروها) با وجود تفاوت در مدل و رنگ، ویژگیها و رفتارهای مشترکی دارند. این موضوع به کدام مفهوم در برنامهنویسی مرتبط است؟
چرا با افزایش حجم برنامهها، نگهداری و اصلاح آنها دشوارتر میشود؟ چگونه میتوان ساختار برنامه را بهگونهای طراحی کرد که تغییرات، کمترین تأثیر را بر سایر بخشها داشته باشند؟
اگر بخواهید در یک بازی رایانهای، چند شخصیت با رفتارهای مشابه و ویژگیهای متفاوت طراحی کنید، استفاده از چه روشی در برنامهنویسی مناسبتر است؟
به چه دلیل در طراحی بسیاری از نرمافزارها و برنامههای بزرگ، از شیوه برنامهنویسی شیءگرا استفاده میشود؟
«کلاس» در برنامهنویسی به چه چیزی شباهت بیشتری دارد: نقشه یک ساختمان یا خود ساختمان؟
در پایان از هنرجو انتظار میرود
کلاسهای پایتون را با ویژگیها و متدهای مناسب، همراه با تعیین مقادیر پیشفرض و مدیریت دسترسی (public, protected, private) طراحی و پیادهسازی کند.
از کلاسها نمونه (Instance) بسازد و مقادیر و ویژگیهای این نمونهها را در طول اجرای برنامه به طور مؤثر تغییر دهد و مدیریت کند.
با استفاده از ارثبری برای ایجاد سلسله مراتب کلاسها، فراخوانی متدهای والد و بازنویسی آنها در کلاسهای فرزند مطابق با نیازهای برنامه اقدام نماید.
با استفاده از اصول OOP، کدی ماژولار، قابل فهم و با قابلیت استفاده مجدد (reusable) بنویسد که از تکرار کد جلوگیری کند.
درک اولیهای از نحوه عملکرد متاکلاسها و نقش آنها در تعریف و ساختار کلاسها پیدا کند.
مسائل برنامهنویسی ساده تا متوسط را با استفاده از برنامهنویسی شیءگرا تحلیل کرده و راهحلهای مبتنی بر شیء طراحی و پیادهسازی کند.
استاندارد عملکرد
با استفاده از مفاهیم برنامهنویسی شیءگرا در زبان پایتون، مسائل ساده و کاربردی را مدلسازی کند، کلاس مناسب طراحی نماید و با ایجاد و بهکارگیری نمونهها، برنامهای خوانا، قابل اجرا و قابل توسعه تولید کند.
صفحه ۲۳۵
برنامهنویسی شیءگرا (Object-Oriented Programming)
برنامهنویسی شیءگرا روشی از برنامهنویسی است که در آن، اجزای برنامه بهصورت موجودیتهایی شبیه به دنیای واقعی مدلسازی میشوند. در این روش، بهجای آنکه فقط به دستورات و توابع فکر کنید، ابتدا به چیزهایی فکرمیکنید که در دنیای واقعی وجود دارند، ویژگی دارند و میتوانند کاری انجام دهند.
شکل ۱
به این چیزها در برنامهنویسی، شیء (Object) و به این شیوۀ برنامهنویسی، شیءگرایی یا به اختصار (O.O.P.) گفته میشود.
در این رویکرد، هدف اصلی، بازآفرینی منطق و ساختار دنیای واقعی در محیط برنامهنویسی است. برای درک بهتر این مفهوم، میتوان به بازیهای استراتژیک رایج همچون Age of Empires یا Clash of Clans اشاره کرد. در این بازیها، شخصیتها و موجودیتها به گروههای گوناگون تقسیم میشوند؛ از کارگران و سربازان گرفته تا وسایل نقلیه و ماشینآلات جنگی. برنامهنویسی شیءگرا با معرفی مفهومی به نام کلاس (Class)، به شما کمک میکند موجودیتهای مشابه را در قالب یک گروه قرار دهید. به این ترتیب، میتوانید هر گروه را بهصورت یک کلاس در نظر بگیرید و ویژگیها و کارهای مربوط به آن را در برنامه مشخص کنید.
شکل ۲
فعلاً از دنیای بازیها فاصله گرفته و یک مثال کاملاً واقعی بررسی میشود. یک تلفن همراه قدیمی را در نظر بگیرید. این تلفن همراه صرفاً یک وسیلۀ ساده نیست، بلکه دارای مجموعهای از ویژگیهاست و قادر است وظایف و عملیات مختلفی را انجام دهد.
شکل ۳
برای مثال هر تلفن همراه این ویژگیها را دارد:
دارای رنگ است.
دارای برند است.
دارای مدل است.
میزان شارژ باتری آن در هر لحظه مشخص است.
صفحه ۲۳۶
اگر دو گوشی هممدل داشته باشید، ویژگیهای آنها مشابه است، اما میزان باتری یا رنگ میتواند متفاوت باشد. هرکدام از این گوشیها یک شیء جداگانه محسوب میشوند. به مشخصاتی که یک شیء دارد، ویژگی (Attribute) گفته میشود. این ویژگیها مشخص میکنند که هر شیء چه چیزهایی دارد.
همچنین هر تلفن همراه کارهای مختلفی میتواند انجام دهد:
روشن و خاموش شود.
تماس بگیرد.
پیام ارسال کند.
متد
کارهایی که یک شیء میتواند انجام دهد، متد (Method) نامیده میشود. در حقیقت متدها مانند دستورالعملهایی هستند که به شیء میگویند چه کاری انجام دهد.
صفحه ۲۳۷
در دنیای واقعی، این تلفن همراه یک «شیء» میباشد و در برنامهنویسی شیءگرا نیز دقیقاً با همین نگاه، اشیا مدلسازی میشوند.
شیء
شیء (Object) به هر چیزی گفته میشود که:
دارای ویژگی (Attribute) باشد.
بتواند کار یا کارهایی (Method) انجام دهد.
نمونه (Instance)
در پایتون، شیءهایی که با استفاده از یک کلاس ساخته میشوند، نمونه (Instance) آن کلاس نامیده میشوند. بنابراین یک تلفن همراه مشخص (مثلاً موبایل شخصی شما)، یک نمونه (Instance) از کلاس تلفن همراه محسوب میشود.
تعریف کلاس (Class)
حال اگر بخواهید چندین تلفن همراه مختلف بسازید، نوشتن ویژگیها و متدها برای هر کدام بهصورت جداگانه کار درستی نیست.
برای این کار، به یک قالب کلی نیاز داریم که مشخص کند:
هر تلفن همراه چه ویژگیهایی دارد.
چه کارهایی میتواند انجام دهد.
به این قالب کلی، کلاس (Class) گفته میشود.
کلاس مانند نقشۀ ساخت یک تلفن همراه است و هر تلفن همراهی که ساخته میشود، در واقع یک نمونه (Instance) از آن کلاس خواهد بود.
جدول ۱
اصطلاحات
معنی
کلاس (Class)
قالب کلی یا نقشه ساخت
نمونه (Instance)
موجود ساختهشده از روی یک کلاس
ویژگی (Attribute)
چه چیزی دارد؟
متد (Method)
چه کاری انجام میدهد؟
Page 234
Learning unit 1
Have you ever thought about this
In the real world, similar objects (such as cars) have common properties and behaviours despite differences in model and colour. Which concept in programming is this related to?
Why does maintaining and fixing programs become harder as their size grows? How can the structure of a program be designed so that changes have the least effect on other parts?
If you want to design several characters with similar behaviours and different properties in a computer game, which programming method is more suitable?
For what reason is the object-oriented programming style used in designing many pieces of software and big programs?
What is a "class" in programming more like: the plan of a building or the building itself?
By the end, the student is expected to
Design and implement Python classes with suitable properties and methods, together with setting default values and managing access (public, protected, private).
Create instances from classes and effectively change and manage the values and properties of these instances while the program runs.
Using inheritance, create class hierarchies, call parent methods and override them in the child classes according to the needs of the program.
Using the principles of OOP, write modular, understandable and reusable code that prevents repeating code.
Gain a basic understanding of how metaclasses work and their role in defining and structuring classes.
Analyse simple to medium programming problems using object-oriented programming and design and implement object-based solutions.
Performance standard
Using the concepts of object-oriented programming in Python, model simple and practical problems, design a suitable class and, by creating and using instances, produce a readable, runnable and extensible program.
Page 235
Object-Oriented Programming
Object-oriented programming is a way of programming in which the parts of the program are modelled as entities like the real world. In this method, instead of thinking only of statements and functions, you first think of things that exist in the real world, have properties and can do something.
Figure 1
These things are called objects in programming, and this style of programming is called object-orientation, or O.O.P. for short.
In this approach the main goal is to recreate the logic and structure of the real world in the programming environment. To understand this concept better, we can mention common strategy games such as Age of Empires or Clash of Clans. In these games, characters and entities are divided into various groups; from workers and soldiers to vehicles and war machines. By introducing a concept called class, object-oriented programming helps you put similar entities in one group. In this way you can consider each group as a class and specify the properties and jobs related to it in the program.
Figure 2
For now, let us move away from the world of games and look at a completely real example. Consider an old mobile phone. This mobile phone is not merely a simple device, but has a set of properties and can do different tasks and operations.
Figure 3
For example every mobile phone has these properties:
It has a colour.
It has a brand.
It has a model.
The level of its battery charge is known at every moment.
Page 236
If you have two phones of the same model, their properties are similar, but the battery level or the colour can differ. Each of these phones counts as a separate object. The characteristics an object has are called properties (attributes). These properties specify what each object has.
Also each mobile phone can do different things:
Turn on and off.
Make a call.
Send a message.
Method
The things an object can do are called methods. In fact methods are like instructions that tell the object what to do.
Page 237
In the real world this mobile phone is an "object", and in object-oriented programming too, objects are modelled with exactly this view.
Object
An object is anything that:
has properties (Attributes).
can do one or more jobs (Methods).
Instance
In Python, objects that are made using a class are called instances of that class. So a specific mobile phone (for example your own mobile) counts as an instance of the mobile phone class.
Defining a class
Now if you want to make several different mobile phones, it is not right to write the properties and methods separately for each one.
For this we need a general template that specifies:
what properties each mobile phone has.
what jobs it can do.
This general template is called a class.
A class is like the plan for building a mobile phone, and every mobile phone that is made is in fact an instance of that class.