{"id":523,"date":"2010-09-27T18:13:30","date_gmt":"2010-09-27T18:13:30","guid":{"rendered":""},"modified":"2010-09-27T18:13:30","modified_gmt":"2010-09-27T18:13:30","slug":"523","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=523","title":{"rendered":"\u7406\u89e3PHP\u91cc\u9762\u7684\u62bd\u8c61\u7c7b\u30101\u3011"},"content":{"rendered":"<p>\u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u901a\u8fc7\u7c7b\u7684\u5206\u5c42\u7ed3\u6784\u6784\u5efa\u8d77\u6765. \u5728\u5355\u91cd\u7ee7\u627f\u8bed\u8a00\u5982PHP\u4e2d, \u7c7b\u7684\u7ee7\u627f\u662f\u6811\u72b6\u7684. \u4e00\u4e2a\u6839\u7c7b\u6709\u4e00\u4e2a\u6216\u66f4\u591a\u7684\u5b50\u7c7b,\u518d\u4ece\u6bcf\u4e2a\u5b50\u7c7b\u7ee7\u627f\u51fa\u4e00\u4e2a\u6216\u66f4\u591a\u4e0b\u4e00\u7ea7\u5b50\u7c7b. \u5f53\u7136,\u53ef\u80fd\u5b58\u5728\u591a\u4e2a\u6839\u7c7b,\u7528\u6765\u5b9e\u73b0\u4e0d\u540c\u7684\u529f\u80fd. \u5728\u4e00\u4e2a\u826f\u597d\u8bbe\u8ba1\u7684\u4f53\u7cfb\u4e2d,\u6bcf\u4e2a\u6839\u7c7b\u90fd\u5e94\u8be5\u6709\u4e00\u4e2a\u6709\u7528\u7684\u63a5\u53e3, \u53ef\u4ee5\u88ab\u5e94\u7528\u4ee3\u7801\u6240\u4f7f\u7528. \u5982\u679c\u6211\u4eec\u7684\u5e94\u7528\u4ee3\u7801\u88ab\u8bbe\u8ba1\u6210\u4e0e\u6839\u7c7b\u4e00\u8d77\u5de5\u4f5c,\u90a3\u4e48\u5b83\u4e5f\u53ef\u4ee5\u548c\u4efb\u4f55\u4e00\u4e2a\u4ece\u6839\u7c7b\u7ee7\u627f\u51fa\u6765\u7684\u5b50\u7c7b\u5408\u4f5c. <\/p>\n<p>\u62bd\u8c61\u65b9\u6cd5\u662f\u5c31\u50cf\u5b50\u7c7b\u4e2d\u4e00\u822c\u7684\u65b9\u6cd5\u7684\u5360\u4f4d\u7b26(\u5360\u4e2a\u5730\u65b9\u4f46\u4e0d\u8d77\u4f5c\u7528),\u5b83\u4e0e\u4e00\u822c\u65b9\u6cd5\u4e0d\u540c\u2014\u6ca1\u6709\u4efb\u4f55\u4ee3\u7801. \u5982\u679c\u7c7b\u4e2d\u5b58\u5728\u4e00\u4e2a\u6216\u66f4\u591a\u62bd\u8c61\u65b9\u6cd5, \u90a3\u4e48\u8fd9\u4e2a\u7c7b\u5c31\u6210\u4e86\u62bd\u8c61\u7c7b. \u4f60\u4e0d\u80fd\u5b9e\u4f8b\u5316\u62bd\u8c61\u7c7b. \u4f60\u5fc5\u987b\u7ee7\u627f\u5b83\u4eec,\u7136\u540e\u5b9e\u4f8b\u5316\u5b50\u7c7b. \u4f60\u4e5f\u53ef\u4ee5\u628a\u62bd\u8c61\u7c7b\u770b\u6210\u662f\u5b50\u7c7b\u7684\u4e00\u4e2a\u6a21\u677f. <\/p>\n<p>\u5982\u679c\u4f60\u8986\u5199\u6240\u6709\u7684\u62bd\u8c61\u65b9\u6cd5, \u5b50\u7c7b\u5c31\u53d8\u6210\u4e00\u4e2a\u666e\u901a\u7684\u7c7b. \u5982\u679c\u6ca1\u6709\u8986\u5199\u6240\u6709\u65b9\u6cd5, \u5b50\u7c7b\u4ecd\u662f\u62bd\u8c61\u7684. \u5982\u679c\u4e00\u4e2a\u7c7b\u4e2d\u4e2d\u5305\u542b\u6709\u62bd\u8c61\u65b9\u6cd5(\u54ea\u6015\u53ea\u6709\u4e00\u4e2a), \u4f60\u5fc5\u987b\u58f0\u660e\u8fd9\u4e2a\u7c7b\u662f\u62bd\u8c61\u7684, \u5728class\u5173\u952e\u5b57\u524d\u52a0\u4e0aabstract. <\/p>\n<p>\u58f0\u660e\u62bd\u8c61\u65b9\u6cd5\u7684\u8bed\u6cd5\u4e0e\u58f0\u660e\u4e00\u822c\u65b9\u6cd5\u4e0d\u540c. \u62bd\u8c61\u65b9\u6cd5\u7684\u6ca1\u6709\u50cf\u4e00\u822c\u65b9\u6cd5\u90a3\u6837\u5305\u542b\u5728\u5927\u62ec\u53f7{}\u4e2d\u7684\u4e3b\u4f53\u90e8\u4efd,\u5e76\u7528\u5206\u53f7;\u6765\u7ed3\u675f. <\/p>\n<p>\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u6587\u4ef6\u4e2d, \u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u542b\u6709getArea\u65b9\u6cd5\u7684\u7c7bShape. \u4f46\u7531\u4e8e\u4e0d\u77e5\u9053\u5f62\u72b6\u4e0d\u53ef\u80fd\u786e\u5b9a\u56fe\u5f62\u7684\u9762\u79ef,\u7684\u786e\u6211\u4eec\u58f0\u660e\u4e86getArea\u65b9\u6cd5\u4e3a\u62bd\u8c61\u65b9\u6cd5. \u4f60\u4e0d\u80fd\u5b9e\u4f8b\u5316\u4e00\u4e2aShape\u5bf9\u8c61,\u4f46\u4f60\u53ef\u4ee5\u7ee7\u627f\u5b83\u6216\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u5b83, \u5c31\u50cf\u4f8b6.13\u4e2d\u90a3\u6837. <\/p>\n<p>\u5982\u679c\u4f60\u5efa\u7acb\u4e86\u4e00\u4e2a\u53ea\u6709\u62bd\u8c61\u65b9\u6cd5\u7684\u7c7b,\u4f60\u5c31\u5b9a\u4e49\u4e86\u4e00\u4e2a\u63a5\u53e3(interface). \u4e3a\u4e86\u8bf4\u660e\u8fd9\u79cd\u60c5\u51b5, PHP\u4e2d\u6709interface \u548cimplements\u5173\u952e\u5b57. \u4f60\u53ef\u4ee5\u7528interface\u6765\u4ee3\u66ff\u62bd\u8c61\u7c7b, \u7528implements\u6765\u4ee3\u66ffextends\u6765\u8bf4\u660e\u4f60\u7684\u7c7b\u5b9a\u4e49\u6216\u4f7f\u7528\u4e00\u4e2a\u63a5\u53e3. \u4f8b\u5982, \u4f60\u53ef\u4ee5\u5199\u4e00\u4e2amyClass implements myIterface. \u8fd9\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f9d\u4e2a\u4eba\u504f\u7231\u6765\u9009\u62e9. <\/p>\n<p>\/*\u6ce8: <br \/>\u4e24\u79cd\u65b9\u6cd5\u5373\u6307: <br \/>1. abstract class aaa{} (\u6ce8\u610faaa\u4e2d\u53ea\u6709\u62bd\u8c61\u65b9\u6cd5,\u6ca1\u6709\u4e00\u822c\u65b9\u6cd5) <br \/>class bbb extends aaa{} (\u5728bbb\u4e2d\u8986\u5199aaa\u4e2d\u7684\u62bd\u8c61\u65b9\u6cd5) <br \/>2. interface aaa{} <br \/>class bbb implements aaa{} (\u5728bbb\u4e2d\u8986\u5199aaa\u4e2d\u7684\u62bd\u8c61\u65b9\u6cd5) <br \/>*\/ <\/p>\n<p><code><br \/>&lt;?php <\/p>\n<p>\/\/abstract root class \u62bd\u8c61\u6839\u7c7b <br \/>abstract class Shape <br \/>{ <br \/> abstract function getArea(); \/\/\u5b9a\u4e49\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5 <br \/>} <\/p>\n<p>\/\/abstract child class \u62bd\u8c61\u5b50\u7c7b <br \/>abstract class Polygon extends Shape \/\/\u591a\u8fb9\u5f62 <br \/>{ <br \/> abstract function getNumberOfSides(); <br \/>} <\/p>\n<p>\/\/concrete class \u5b9e\u4f53\u7c7b \u4e09\u89d2\u5f62\u7c7b <br \/>class Triangle extends Polygon <br \/>{ <br \/> public $base; <br \/> public $height; <\/p>\n<p> public function getArea() \/\/\u8986\u5199\u8ba1\u7b97\u9762\u79ef\u65b9\u6cd5 <br \/> { <br \/>&nbsp;&nbsp;return(($this-&gt;base * $this-&gt;height)\/2); <br \/> } <\/p>\n<p> public function getNumberOfSides() \/\/\u8986\u5199\u8fb9\u6570\u7edf\u8ba1\u65b9\u6cd5 <br \/> { <br \/>&nbsp;&nbsp;return(3); <br \/> } <br \/>} <\/p>\n<p>\/\/concrete class \u5b9e\u4f53\u7c7b\u56db\u8fb9\u5f62 <br \/>class Rectangle extends Polygon <br \/>{ <br \/> public $width; <br \/> public $height; <\/p>\n<p> public function getArea() <br \/> { <br \/>&nbsp;&nbsp;return($this-&gt;width * $this-&gt;height); <br \/> } <\/p>\n<p> public function getNumberOfSides() <br \/> { <br \/>&nbsp;&nbsp;return(4);<br \/> } <br \/>} <\/p>\n<p>\/\/concrete class \u5b9e\u4f53\u7c7b \u5706\u5f62 <br \/>class Circle extends Shape <br \/>{ <br \/> public $radius; <br \/> public function getArea() <br \/> { <br \/>&nbsp;&nbsp;return(pi() * $this-&gt;radius * $this-&gt;radius); <br \/> } <br \/>} <\/p>\n<p>\/\/concrete root class \u5b9a\u4e49\u4e00\u4e2a\u989c\u8272\u7c7b <br \/>class Color <br \/>{ <br \/>public $name; <br \/>} <\/p>\n<p>$myCollection = array(); \/\/\u5efa\u7acb\u5f62\u72b6\u7684\u96c6\u5408,\u653e\u5165\u6570\u7ec4 <\/p>\n<p>\/\/make a rectangle <br \/>$r = new Rectangle; <br \/>$r-&gt;width = 5; <br \/>$r-&gt;height = 7; <br \/>$myCollection[] = $r; <br \/>unset($r); <\/p>\n<p>\/\/make a triangle <br \/>$t = new Triangle; <br \/>$t-&gt;base = 4; <br \/>$t-&gt;height = 5; <br \/>$myCollection[] = $t; <br \/>unset($t); <\/p>\n<p>\/\/make a circle <br \/>$c = new Circle; <br \/>$c-&gt;radius = 3; <br \/>$myCollection[] = $c; <br \/>unset($c); <\/p>\n<p>\/\/make a color <br \/>$c = new Color; <br \/>$c-&gt;name = \"blue\"; <br \/>$myCollection[] = $c; <br \/>unset($c); <\/p>\n<p>foreach($myCollection as $s) <br \/>{ <br \/> if($s instanceof Shape)&nbsp;&nbsp;print(\"Area: \".$s-&gt;getArea().\"&lt;br&gt;<br \/>\n\");&nbsp;&nbsp;\/\/\u5982\u679c$s\u662fShape\u7c7b\u7684\u5b9e\u4f8b <br \/> if($s instanceof Polygon) print(\"Sides: \".$s-&gt;getNumberOfSides().\"&lt;br&gt;<br \/>\n\"); <br \/> if($s instanceof Color) print(\"Color:\".$s-&gt;name.\"&lt;br&gt;<br \/>\n\"); <br \/> print(\"&lt;br&gt;<br \/>\n\"); <br \/>} <\/p>\n<p>?&gt; <\/p>\n<p><\/code><br \/>\u8fd0\u884c\u7ed3\u679c: <\/p>\n<p>Area: 35<br \/>Sides: 4<\/p>\n<p>Area: 10<br \/>Sides: 3<\/p>\n<p>Area: 28.274333882308<\/p>\n<p>Color:blue<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u901a\u8fc7\u7c7b\u7684\u5206\u5c42\u7ed3\u6784\u6784\u5efa\u8d77\u6765. \u5728\u5355\u91cd\u7ee7\u627f\u8bed\u8a00\u5982PHP\u4e2d, &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-523","post","type-post","status-publish","format-standard","hentry","category-Php"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=523"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/523\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}