علاقمندان به زبان های برنامه نویسی روزتون بخیر. با آموزش زبان برنامه نویسی پاسکال (Pascal) از وب سایت آموزشی پی وی لرن همراهتون خواهیم بود. در این بخش از مبحث کلاس ها در زبان پاسکال (Pascal) با Constructors و Destructors کلاس پاسکال و وراثت آشنا خواهیم شد.
سازنده ها روش های خاصی هستند که هر زمان که یک شی ایجاد شود به طور خودکار خوانده می شوند. بنابراین ما با آغاز کردن بسیاری از موارد از طریق توابع سازنده ، از این رفتار کاملاً استفاده می کنیم.
پاسکال برای تعریف سازنده یک تابع ویژه به نام ()create ایجاد می کند. می توانید به همان اندازه آرگومان های مورد نظر خود را به تابع سازنده منتقل کنید.
به عنوان مثال ، یک سازنده برای کلاسی به نام Books ایجاد می کنیم و قیمت و عنوان (price و title) آن را در زمان ایجاد شیء مقدار دهی می کنیم.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | program classExample; {$MODE OBJFPC} //directive to be used for creating classes {$M+} //directive that allows class constructors and destructors type Books = Class private title : String; price: real; public constructor Create(t : String; p: real); //default constructor procedure setTitle(t : String); //sets title for a book function getTitle() : String; //retrieves title procedure setPrice(p : real); //sets price for a book function getPrice() : real; //retrieves price procedure Display(); // display details of a book end; var physics, chemistry, maths: Books; //default constructor constructor Books.Create(t : String; p: real); begin title := t; price := p; end; procedure Books.setTitle(t : String); //sets title for a book begin title := t; end; function Books.getTitle() : String; //retrieves title begin getTitle := title; end; procedure Books.setPrice(p : real); //sets price for a book begin price := p; end; function Books.getPrice() : real; //retrieves price begin getPrice:= price; end; procedure Books.Display(); begin writeln('Title: ', title); writeln('Price: ', price:5:2); end; begin physics := Books.Create('Physics for High School', 10); chemistry := Books.Create('Advanced Chemistry', 15); maths := Books.Create('Algebra', 7); physics.Display; chemistry.Display; maths.Display; end. |
هنگامی که کد فوق کامپایل و اجرا شد ، نتیجه زیر را ایجاد می کند.
1 2 3 4 5 6 | Title: Physics for High School Price: 10 Title: Advanced Chemistry Price: 15 Title: Algebra Price: 7 |
همچون سازنده implicit (ضمنی) بنام create، یک متد تخریب کننده (Destructor) نیز وجود دارد که با استفاده از آن می توانید تمام منابع مورد استفاده در کلاس را آزاد کنید.
تعاریف کلاس پاسکال می تواند اختیاری از تعریف کلاس والدین باشد. سینتکس به شرح زیر است.
1 2 3 4 | type childClas-identifier = class(baseClass-identifier) < members > end; |
مثال زیر یک کلاس novels را ارائه می دهد ، که از کلاس Books به ارث می برد و قابلیت های بیش تری را بر اساس نیاز اضافه می کند.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | program inheritanceExample; {$MODE OBJFPC} //directive to be used for creating classes {$M+} //directive that allows class constructors and destructors type Books = Class protected title : String; price: real; public constructor Create(t : String; p: real); //default constructor procedure setTitle(t : String); //sets title for a book function getTitle() : String; //retrieves title procedure setPrice(p : real); //sets price for a book function getPrice() : real; //retrieves price procedure Display(); virtual; // display details of a book end; (* Creating a derived class *) type Novels = Class(Books) private author: String; public constructor Create(t: String); overload; constructor Create(a: String; t: String; p: real); overload; procedure setAuthor(a: String); // sets author for a book function getAuthor(): String; // retrieves author name procedure Display(); override; end; var n1, n2: Novels; //default constructor constructor Books.Create(t : String; p: real); begin title := t; price := p; end; procedure Books.setTitle(t : String); //sets title for a book begin title := t; end; function Books.getTitle() : String; //retrieves title begin getTitle := title; end; procedure Books.setPrice(p : real); //sets price for a book begin price := p; end; function Books.getPrice() : real; //retrieves price begin getPrice:= price; end; procedure Books.Display(); begin writeln('Title: ', title); writeln('Price: ', price); end; (* Now the derived class methods *) constructor Novels.Create(t: String); begin inherited Create(t, 0.0); author:= ' '; end; constructor Novels.Create(a: String; t: String; p: real); begin inherited Create(t, p); author:= a; end; procedure Novels.setAuthor(a : String); //sets author for a book begin author := a; end; function Novels.getAuthor() : String; //retrieves author begin getAuthor := author; end; procedure Novels.Display(); begin writeln('Title: ', title); writeln('Price: ', price:5:2); writeln('Author: ', author); end; begin n1 := Novels.Create('Gone with the Wind'); n2 := Novels.Create('Ayn Rand','Atlas Shrugged', 467.75); n1.setAuthor('Margaret Mitchell'); n1.setPrice(375.99); n1.Display; n2.Display; end. |
هنگامی که کد فوق کامپایل و اجرا شد ، نتیجه زیر را ایجاد می کند.
1 2 3 4 5 6 | Title: Gone with the Wind Price: 375.99 Author: Margaret Mitchell Title: Atlas Shrugged Price: 467.75 Author: Ayn Rand |
به نکات مهم زیر را توجه کنید.
اعضای کلاس Books دید protected دارند.
کلاس Novels دارای دو سازنده است ، بنابراین از اپراتور اضافه بار (overload) برای عملکرد سر بار استفاده می شود.
رویه Books.Display به صورت مجازی اعلام شده است ، به طوری که همین روش از کلاس Novels می تواند آن را نادیده بگیرد.
سازنده Novels.Create با استفاده از کلمه کلیدی inherited ، سازنده کلاس پایه را فراخوانی می کند.
در این بخش از مبحث کلاس ها در زبان پاسکال (Pascal) با Constructors و Destructors کلاس پاسکال و وراثت در پاسکال آشنا شدیم. در بخش بعد نیز موضوع کلاس ها در پاسکال را ادامه خواهیم داد. با ما همراه باشید.