به شکل زیر توجه کنید.این تصویر دارای اشکال مختلفی است که هرکدام دارای فرمول خاص خود در محاسبه مساحت و محیط هستند.
این تفاوت فرمول باعث می شود که در هر شکل متد خود را با توجه به فرمول آن بارنویسی و طراحی کنیم.
اما دو خصوصیت بین همه آنها مشترک است و آن نام شکل و رنگ شکل است.
این تصویر گویای حالت polymorphism است.
برای شروع و تعریف خصوصیات نام شکل و رنگ از کلاس abstract به نام Shape استفاده می کنیم.همچنین متدی به نام getInfo ایجاد می کنیم که وظیفه آن بازگشت اطلاعات شکل است.
مثال:
1 2 3 4 5 6 7 8 9 10 |
<?php abstract class Shape{ public $name; public $color; public function getInfo() { return "name of shape is:".$this->name." and color is:".$this->color; } } ?> |
برای فرمول ها نیز متد مشترک getEnviroment و getAre را بدون بدنه در داخل کلاس CallMethods بصورت interface تعریف می کنیم و در کلاس های مخصوص که ارث بری می کنند آنها را بازنویسی می کنیم.
مثال:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php abstract class Shape{ public $name; public $color; public function getInfo(){ return "name of shape is:".$this->name." and color is:".$this->color; } } interface CallMethods{ public function getEnviroment(); public function getAre(); } ?> |
حال برای هر شکل کلاس خود را ایجاد و از کلاس abstract و interface ارث بری می کنیم.
کلاس مربوط به مستطیل:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php abstract class Shape{ public $name; public $color; public function getInfo(){ return "name of shape is:".$this->name." and color is:".$this->color; } } interface CallMethods{ public function getEnviroment(); public function getAre(); } class Rectangle extends Shape implements CallMethods{ } ?> |
حال هر شکل دارای خصوصیات مخصوص خود است برای مثال مستطیل دارای طول و عرض است پس در کلاس خودش این خصوصیات را تعریف می کنیم.
مثال:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php abstract class Shape{ public $name; public $color; public function getInfo(){ return "name of shape is:".$this->name." and color is:".$this->color; } } interface CallMethods{ public function getEnviroment(); public function getAre(); } class Rectangle extends Shape implements CallMethods{ public $length = 0; public $width = 0; } ?> |
حال تابع سازنده را در شکل ایجاد می کنیم.این تابع سازنده تنظیم کننده طول و عرض و رنگ و نام شکل است.
مثال:
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 |
<?php abstract class Shape{ public $name; public $color; public function getInfo(){ return "name of shape is:".$this->name." and color is:".$this->color; } } interface CallMethods{ public function getEnviroment(); public function getAre(); } class Rectangle extends Shape implements CallMethods{ public $length = 0; public $width = 0; public function __construct($l = 0,$w = 0,$c = ""){ $this->length = $l; $this->width = $w; $this->color = $c; $this->name = "rectangle"; } } ?> |
حال متدهای محاسبه مساحت و محیط را درون کلاس مستطیل بازنویسی می کنیم.
مثال:
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 |
<?php abstract class Shape{ public $name; public $color; public function getInfo(){ return "name of shape is:".$this->name." and color is:".$this->color; } } interface CallMethods{ public function getEnviroment(); public function getAre(); } class Rectangle extends Shape implements CallMethods{ public $length = 0; public $width = 0; public function __construct($l = 0,$w = 0,$c = ""){ $this->length = $l; $this->width = $w; $this->color = $c; $this->name = "rectangle"; } public function getAre() { return $this->width * $this->length; } public function getEnviroment() { return ($this->width + $this->length)*2; } } ?> |
طراحی کلاس دایره:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php class Cricle extends Shape implements CallMethods{ public $raduis = 0; public function __construct($r = 0,$c = ""){ $this->raduis = $r; $this->color = $c; $this->name = "Cricle"; } public function getAre() { return pi() * pow($this->raduis,2); } public function getEnviroment() { return pi() * ($this->raduis * 2); } } ?> |
طراحی کلاس مربع:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php class Square extends Shape implements CallMethods{ public $border = 0; public function __construct($b = 0,$c = ""){ $this->border = $b; $this->color = $c; $this->name = "Square"; } public function getAre() { return pow($this->border , 2); } public function getEnviroment() { return $this->border * 4; } } ?> |
طراحی کلاس مثلث:
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 |
<?php class Triangle extends Shape implements CallMethods{ public $border = 0; public $base = 0; public $height = 0; public function __construct($b = 0,$g = 0,$h = 0,$c = ""){ $this->border = $b; $this->base = $g; $this->height = $h; $this->color = $c; $this->name = "Triangle"; } public function getAre() { return ($this->base * $this->height) / 2; } public function getEnviroment() { return ($this->border * 3); } } ?> |
استفاده از کلاس های ساخته شده و ساخت شی
در اینجا پس از ساخت کلاس های اشکال مختلف تصمیم به ساخت شی برای هریک می کنیم.
ساخت شی مستطیل:
1 2 3 4 5 6 7 8 9 10 11 |
<?php $rectangle = new Rectangle(15,8,"red"); echo "Area: ".$rectangle->getArea()."<br>"; echo "Enviroment: ".$rectangle->getEnviroment()."<br>";; echo $rectangle->getInfo(); ?> /* Area: 120 Enviroment: 46 name of shape is:rectangle and color is:red */ |
ساخت شی دایره:
1 2 3 4 5 6 7 8 9 10 11 |
<?php $cricle = new Cricle(15,"green"); echo "Area: ".$cricle->getArea()."<br>"; echo "Enviroment: ".$cricle->getEnviroment()."<br>";; echo $cricle->getInfo()."<br><br>"; ?> /* Area: 706.8583470577 Enviroment: 94.247779607694 name of shape is:Cricle and color is:green */ |
ساخت شی مربع:
1 2 3 4 5 6 7 8 9 10 11 |
<?php $square = new Square(10,"yellow"); echo "Area: ".$square->getArea()."<br>"; echo "Enviroment: ".$square->getEnviroment()."<br>";; echo $square->getInfo()."<br><br>"; ?> /* Area: 100 Enviroment: 40 name of shape is:Square and color is:yellow */ |
ساخت شی مثلث:
1 2 3 4 5 6 7 8 9 10 11 |
<?php $triangle = new Triangle(3, 10,5, "red"); echo "Area: ".$triangle->getArea()."<br>"; echo "Enviroment: ".$triangle->getEnviroment()."<br>";; echo $triangle->getInfo()."<br><br>"; ?> /* Area: 25 Enviroment: 9 name of shape is:Triangle and color is:red */ |
مثال نهایی پیاده سازی کلاس با روش interface, abstract و final:
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 113 114 115 116 117 |
<?php abstract class Shape{ public $name; public $color; public function getInfo(){ return "name of shape is:".$this->name." and color is:".$this->color; } } interface CallMethods{ public function getEnviroment(); public function getArea(); } class Rectangle extends Shape implements CallMethods{ public $length = 0; public $width = 0; public function __construct($l = 0,$w = 0,$c = ""){ $this->length = $l; $this->width = $w; $this->color = $c; $this->name = "rectangle"; } public function getArea() { return $this->width * $this->length; } public function getEnviroment() { return ($this->width + $this->length)*2; } } class Cricle extends Shape implements CallMethods{ public $raduis = 0; public function __construct($r = 0,$c = ""){ $this->raduis = $r; $this->color = $c; $this->name = "Cricle"; } public function getArea() { return pi() * pow($this->raduis,2); } public function getEnviroment() { return pi() * ($this->raduis * 2); } } class Square extends Shape implements CallMethods{ public $border = 0; public function __construct($b = 0,$c = ""){ $this->border = $b; $this->color = $c; $this->name = "Square"; } public function getArea() { return pow($this->border , 2); } public function getEnviroment() { return $this->border * 4; } } class Triangle extends Shape implements CallMethods{ public $border = 0; public $base = 0; public $height = 0; public function __construct($b = 0,$g = 0,$h = 0,$c = ""){ $this->border = $b; $this->base = $g; $this->height = $h; $this->color = $c; $this->name = "Triangle"; } public function getArea() { return ($this->base * $this->height) / 2; } public function getEnviroment() { return ($this->border * 3); } } $rectangle = new Rectangle(15,8,"red"); echo "Area: ".$rectangle->getArea()."<br>"; echo "Enviroment: ".$rectangle->getEnviroment()."<br>";; echo $rectangle->getInfo()."<br><br>"; $cricle = new Cricle(15,"green"); echo "Area: ".$cricle->getArea()."<br>"; echo "Enviroment: ".$cricle->getEnviroment()."<br>";; echo $cricle->getInfo()."<br><br>"; $square = new Square(10,"yellow"); echo "Area: ".$square->getArea()."<br>"; echo "Enviroment: ".$square->getEnviroment()."<br>";; echo $square->getInfo()."<br><br>"; $triangle = new Triangle(3, 10,5, "red"); echo "Area: ".$triangle->getArea()."<br>"; echo "Enviroment: ".$triangle->getEnviroment()."<br>";; echo $triangle->getInfo()."<br><br>"; ?> |