鎶借薄绫籥bstract class
abstract class 涓嶈兘琚洿鎺ュ疄渚嬪寲锛屾娊璞$被涓畾涔夊瓙绫婚渶瑕佺殑鏂规硶銆
涓涓畝鍗曞垪瀛愰氫織璇存槑涓嬶細褰撲綘鍙戠幇浣犵殑寰堝绫婚噷闈㈢敤寰堝鏂规硶浣犱笉鏂殑鍦ㄩ噸澶嶅啓锛岄偅浣犲氨鍙互鑰冭檻浣跨敤鎶借薄绫讳簡锛屼綘鍙兘浼氳鈥滄垜涓嶆槸鍙互閲嶅啓涓涓被姣忎釜鍏叡绫绘垜涓疄渚嬪寲涓涓繖涓叕鍏辩被锛岃皟鐢ㄧ浉鍚岀殑鏂规硶灏卞彲浠ヤ簡鈥濓紝杩欓噷鏄彲浠ワ紝瀹為檯涓婃娊璞$被鍋氱殑宸ヤ綔涔熷氨鏄繖涓紝涓嶈繃浠栫渷鍘讳簡浣犲疄渚嬪寲鐨勮繖涓楠わ紝璁╀綘灏卞儚鐩存帴璋冪敤PHP鎶借薄绫籥bstract鏂规硶涓鏍锋柟渚匡紝鑰屼笖浣犺繕鍙互閲嶈浇杩欎釜鏂规硶銆
abstract class shopWriter{
public function writer();
}
class XmlWriter extends shopWriter{
public function writer(){
...瀹炵幇鍏蜂綋鐨剎ml鏂规硶
}
}
class jsonWriter extends shopWriter{
public function writer(){
...瀹炵幇鍏蜂綋鐨刯son鏂规硶
}
}
浠ヤ笂妗堜緥锛屾垜浠垱寤轰簡涓や釜绫伙紝姣忎釜绫婚兘鏈夎嚜宸辩殑write()鏂规硶瀹炵幇銆傜涓涓緭鍑篨ML锛岀浜屼釜杈撳嚭json銆
鎺ュ彛 interface
鎺ュ彛绫绘彁渚涗簡鍏蜂綋瀹炵幇鐨勬爣鍑嗭紝鑰宨nterface鍒欐槸绾补鐨勬ā鏉裤傛帴鍙e彲鍖呭惈鐔熸倝鍜屾柟娉曠敵鏄庯紝浣嗘槸鏂规硶浣撲负绌
閫氫織璁诧細褰撴湁寰堝浜轰竴璧峰紑鍙戜竴涓」鐩椂锛屽彲鑳介兘浼氬幓璋冪敤鍒汉鍐欑殑涓浜涚被锛岄偅浣犲氨浼氶棶锛屾垜鎬庝箞鐭ラ亾浠栫殑鏌愪釜鍔熻兘鐨勫疄鐜版柟娉曟槸鎬庝箞鍛藉悕鐨勫憿锛岃繖涓椂鍊檖hp鎺ュ彛绫诲氨璧峰埌浣滅敤浜嗭紝褰撴垜浠畾涔変簡涓涓帴鍙g被鏃讹紝瀹冮噷闈㈢殑鏂瑰紡鏄笅闈㈢殑瀛愮被蹇呴』瀹炵幇鐨
瀹氫箟涓涓帴鍙o細
interface chargeable{
public function getPrice();
}
瀹炵幇chargeable鎺ュ彛
class ShopProduct implements chargeable{
return $this->price ;
}
缁撹锛
鎶借薄绫诲氨鏄竴涓被鐨勬湇鍔℃彁渚涘晢锛屾嫢鏈変紬澶氭湇鍔★紝浣犱笉鐢ㄥ繀椤荤敤锛屽綋闇瑕佺殑鏃跺欎綘鏉ョ敤灏卞彲浠ワ紝濡傛灉浣犺寰椾笉鎻愪緵鏈嶅姟涓嶆弧鎰忥紝浣犺繕鍙互鑷繁鏉ュ仛銆
鎺ュ彛绫诲氨鏄竴涓被鐨勯瀵艰咃紝鎸囨槑鏂瑰悜锛屽瓙绫诲繀椤诲畬鎴愬畠鎸囧畾鏂规硶銆
瀹冧滑鐨勫尯鍒細
1.瀹氫箟鎶借薄绫荤敤abstract,鎺ュ彛鐢╥nterface ,缁ф壙鎶借薄绫荤敤extends,缁ф壙鎺ュ彛鐢╩plements
2.鎶借薄绫昏窡绫讳竴鏍蜂笉鑳藉閲嶇户鎵,浣嗘槸鎺ュ彛鍙互澶氶噸缁ф壙,鐢","闅斿紑
3.鎶借薄绫讳笉涓瀹氬彧鍖呭惈鎶借薄鏂规硶,鍙互瀹氫箟瀹屾暣鐨勬柟娉,鎺ュ彛涓嶈兘鍖呭惈浠讳綍瀹屾暣瀹氫箟鏂规硶
4.鎶借薄绫讳腑鐨勬柟娉曚笉蹇呭叏閮ㄩ噸杞,浣嗘槸鎺ュ彛鏂规硶蹇呴』澹版槑鎴栬呴噸杞
浠ヤ笂灏辨槸PHP闈㈠悜瀵硅薄瀛︿範涔嬩笁:鎶借薄绫诲拰鎺ュ彛绫荤殑瀹為檯浣滅敤鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒