亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 9
粉絲 0
評(píng)論 0
訪問(wèn)量 9515
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
抽象類(lèi)的繼承與接口--2019年10月9日
cat的博客
原創(chuàng)
795人瀏覽過(guò)

一、抽象類(lèi)的繼承

實(shí)例

<?php
namespace _1009;

abstract class Animal {
	public $name;
	public $color;
	public $weight;
	public $width;
	public $height;
	
	public function __construct ($name, $color, $weight, $width, $height) {
		$this->name = $name;
		$this->color = $color;
		$this->weight = $weight;
		$this->width = $width;
		$this->height = $height;
	}
	abstract public function eat();
	abstract public function drink();
	abstract public function run();
}

class Cat extends Animal {
	public function __construct ($name='小花貓', $color='白色', $weight='10kg', $width='20公分', $height='10公分') {
		parent::__construct($name, $color, $weight, $width, $height);
		$this->name   = $name;
		$this->color  = $color;
		$this->weight = $weight;
		$this->width  = $width;
		$this->height = $height;
	}
	public function eat () {
		echo 'This is eat';
	}
	
	public function drink () {
		echo 'This is drink';
	}
	public function run () {
		echo 'This is run';
	}
	public function getInfo () {
		echo '我的名字是'.$this->name.', 顏色:'.$this->color.' 體重:'.$this->weight.' 寬度:'.$this->width.' 身高'.$this->height;
	}
}
$obj = new Cat();
$obj->getInfo();

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

二、接口實(shí)現(xiàn)案例

實(shí)例

<?php

interface iCurd {
	//添加
	public function add($data);
	
	//刪除
	public function del($where);
	
	//修改
	public function update($data, $where);
	
	//查詢(xún)
	public function select();
	
	//獲取get post 請(qǐng)求參數(shù)
	public function input($str);
}

//數(shù)據(jù)庫(kù)類(lèi)
class Db implements iCurd {
	//數(shù)據(jù)庫(kù)連接對(duì)象
	protected $pdo = null;
	//連接的數(shù)據(jù)表
	protected $table;
	
	//請(qǐng)求類(lèi)型
	protected $type;
	//請(qǐng)求參數(shù)
	protected $param;
	
	public function __construct($dsn, $username, $password, $table='staff'){
		$this->pdo = new \PDO($dsn, $username, $password);
		$this->table = $table;
	}
	//添加
	public function add($data){
		//字段
		$fields = ' (name, age, sex, position, mobile, hiredate) ';
		//值
		$values = ' (:name, :age, :sex, :position, :mobile, :hiredate) ';
		$sql = "insert into ".$this->table.$fields.'values'.$values;
		$stmt = $this->pdo->prepare($sql);
		$stmt->execute($data);
		
		return [
			'count' => $stmt->rowCount(),
			'id' => $this->pdo->lastInsertId()
		];
	}
	//刪除
	public function del($where){
		$sql = 'delete from '.$this->table.' where '.$where;
		$stmt = $this->pdo->prepare($sql);
		$stmt->execute(); 
		
		return $stmt->rowCount();
	}
	//修改
	public function update($data, $where){
		$keys = array_keys($data);
		$set = '';
		foreach($keys as $value){
			$set .= $value.'='.':'.$value.',';
		}
		$set = rtrim($set, ',');
		$sql = 'update '.$this->table.' set '.$set.' where '.$where;
		$stmt = $this->pdo->prepare($sql);
		$stmt->execute();
		
		return $stmt->rowCount();
	}
	//查詢(xún)
	public function select($fields='*', $where='', $limit='0, 5'){
		$where = empty($where) ? '' : ' where '.$where;
		$limit = ' limit '.$limit;
		$sql = 'select '.$fields.' from '.$this->table.$where.$limit;
		$stmt = $this->pdo->prepare($sql);
		$stmt->execute();
		
		return $stmt->fetchAll(\PDO::FETCHA_ASSOC);
	}
	
	//字符串處理
	public function setStr($str){
		$str_arr = explode('.' ,strtolower(trim($str)));
		$this->type  = $str_arr[0];
		$this->param = $str_arr[1];
	}
	
	//獲取請(qǐng)求參數(shù)
	public function input($str){
		$this->setStr($str);
		
		if($this->type=='get'){
			return $_GET["$this->param"];
		}
		if($this->type=='post'){
			return $_POST["$this->param"];
		}
	}
}

//數(shù)據(jù)庫(kù)實(shí)例
$dsn = "mysql:localhost=127.0.0.1;dbname=phpcncms";
$username = 'root';
$password = '';
$obj = new Db($dsn, $username, $password);

$id =  $obj->input('get.id');

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

總結(jié):

1 在繼承抽象類(lèi)時(shí),子類(lèi)中要手動(dòng)實(shí)現(xiàn)構(gòu)造方法,因?yàn)樽宇?lèi)不會(huì)自動(dòng)繼承抽象類(lèi)的構(gòu)造方法

2 接口要使用interface關(guān)鍵字,接口中方法不能有實(shí)體,即抽象方法。在實(shí)現(xiàn)接口時(shí)使用implements關(guān)鍵字,接口中的抽象方法都要被實(shí)現(xiàn)。

批改狀態(tài):合格

老師批語(yǔ):接口與抽象類(lèi)很像, 使用的時(shí)候, 大多數(shù)情況下, 的確是可以互換的, 所以一定要多多留意
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)