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

搜索
博主信息
博文 21
粉絲 0
評論 0
訪問量 29564
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
抽象類和接口的定義和使用-------2019.10.09
刂艸亻的博客
原創(chuàng)
898人瀏覽過

抽象類的定義和使用

php抽象類定義關(guān)鍵字 abstract

抽象方法是沒有方法體的方法,沒有方法體是指方法聲明時沒有花括號以及其中的內(nèi)容,而是聲明方法時直接在方法名后加上分號結(jié)束,聲明抽象方法要使用abstract關(guān)鍵字修飾。聲明抽象方法格式:abstract function();

抽象類中的方法不能自己去實(shí)現(xiàn),需要靠子類中去實(shí)現(xiàn)
1. 抽象類不能實(shí)例化
2. 抽象類中定義的抽象方法必須在子類中實(shí)現(xiàn)

實(shí)例

<?php
//抽象類
namespace _1009;
//php抽象類定義關(guān)鍵字 abstract
abstract class Person1
{

    //抽象類中的屬性
    protected $name;
//抽象方法是沒有方法體的方法,沒有方法體是指方法聲明時沒有花括號以及其中的內(nèi)容,而是聲明方法時直接在方法名后加上分號結(jié)束,聲明抽象方法要使用abstract關(guān)鍵字修飾。聲明抽象方法格式:abstract function();
// 簽名, 抽象方法
    public function __construct($name = '雄')
    {
        $this->name = $name;
    }
    abstract public function getName();

    abstract public function setName($value);
}
//抽象類中的方法不能自己去實(shí)現(xiàn),需要靠子類中去實(shí)現(xiàn)
//1. 抽象類不能實(shí)例化
//2. 抽象類中定義的抽象方法必須在子類中實(shí)現(xiàn)
class Stus extends Person1
{

    public function getName()
    {
        return $this->name;
    }
    public function setName($value)
    {
        $this->name = $value;
    }
}

$stu = new Stus('熊貓');
echo $stu->getName();
echo '<hr>';
$stu->setName('北極熊');
echo $stu->getName();

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

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

接口的定義和使用

定義接口類 ,關(guān)鍵字 interface

實(shí)現(xiàn)接口 ,關(guān)鍵字 implements

實(shí)例

<?php
namespace _1009;

//定義接口類 ,關(guān)鍵字 interface
interface Book
{
    //設(shè)置書名
    public function bookName($bookName);
    //設(shè)置作者
    public function bookZuoZhe($bookZuoZhe);

}

//實(shí)現(xiàn)接口 ,關(guān)鍵字 implements
class Read implements Book
{
    public $bookName;
    public $bookZuoZhe;

    //實(shí)現(xiàn)接口里面的方法
    public function bookName($bookName)
    {
        $this->bookName = $bookName;
    }

    //實(shí)現(xiàn)接口里面的方法
    public function bookZuoZhe($bookZuoZhe)
    {
        $this->bookZuoZhe = $bookZuoZhe;
    }

    public function getXinxi()
    {
        return '《'.$this->bookName.'》'.'作者:'.$this->bookZuoZhe;
    }
}

//客戶端代碼調(diào)用
$obj = new Read();
$obj->bookName('三體');
$obj->bookZuoZhe('劉念慈');
echo $obj->getXinxi();

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

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

使用接口--實(shí)例演示數(shù)據(jù)庫的curd操作

實(shí)例

<?php
namespace _1009;

//接口實(shí)戰(zhàn)
interface iCurd
{
    //增加數(shù)據(jù)
    public function create($data);

    //查詢數(shù)據(jù)
    public function read();

    //更新數(shù)據(jù)
    public function update($data,$where);

    //刪除數(shù)據(jù)
    public function delete($where);

}

//創(chuàng)建工作類,實(shí)現(xiàn)接口方法
class Db implements iCurd
{
    //數(shù)據(jù)庫的連接對象
    protected $pdo = null;

    //表名
    protected $table;

    //構(gòu)造方法連接數(shù)據(jù)庫
    public function __construct($dsn, $user, $pwd, $table = 'staff')
    {
        //保存實(shí)例化后的pdo對象
        $this->pdo = new \PDO($dsn, $user, $pwd);
        //設(shè)置表名
        $this->table = $table;

    }

    //添加數(shù)據(jù)
    public function create($data)
    {
        // TODO: Implement create() method.
        // 字段列表
        $fields = ' (name, age, sex,position, mobile, hiredate) ';
        // 值列表
        $values = ' (:name, :age, :sex, :position, :mobile, :hiredate) ';
        //創(chuàng)建sql語句
        $sql = 'INSERT INTO '.$this->table . $fields.' VALUES '.$values;

        //預(yù)處理語句
        $stmt = $this->pdo->prepare($sql);

        //執(zhí)行,綁定參數(shù)
        $stmt->execute($data);
        //返回結(jié)果
        return [
            //返回受影響行數(shù)
            'count' => $stmt->rowCount(),
            //返回主鍵ID
            'id' => $this->pdo->lastInsertId()
        ];
    }

    //查詢數(shù)據(jù)
    public function read($fields = '*',$where='',$limit='0,5')
    {
        //設(shè)置條件
        $where = empty($where) ? '' : ' WHERE ' . $where;

        // 設(shè)置顯示數(shù)量
        $limit = ' LIMIT ' . $limit;

//        拼接sql語句
        $sql = 'SELECT '.$fields . ' FROM '.$this->table.$where.$limit;
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

    //更新數(shù)據(jù)
    public function update($data,$where)
    {
        // 字段列表
        $keys = array_keys($data);
        $set = '';
        foreach ($keys as $value) {
            $set .= $value . ' = :' . $value. ', ';
        }
        $set = rtrim($set, ', ');
        //創(chuàng)建sql語句
        $sql = 'UPDATE '.$this->table . ' SET '.$set.' WHERE '.$where;

        //預(yù)處理語句
        $stmt = $this->pdo->prepare($sql);

        //執(zhí)行,綁定參數(shù)
        $stmt->execute($data);
        //返回結(jié)果
        return [
            //返回受影響行數(shù)
            'count' => $stmt->rowCount(),
            //返回主鍵ID
            'id' => $this->pdo->lastInsertId()
        ];
    }

    //刪除數(shù)據(jù)
    public function delete($where)
    {
        $sql = 'DELETE FROM '.$this->table.' WHERE '.$where;
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute();

        return $stmt->rowCount();
    }
}
$dsn = 'mysql:host=127.0.0.1;dbname=chen';
$user = 'root';
$password = 'root';
$obj= new Db($dsn,$user,$password);
//$data = [
//    'name' => '萬劍一',
//    'age' => 29,
//    'sex' => 1,
//    'position' => '祖?zhèn)鞅瘎∧?#39;,
//    'mobile' => '1389998899',
//    'hiredate' => time()
//];
//$res = $obj->create($data);
//echo '成功的新增了: '. $res['count'].' 條記錄,新增的記錄的主鍵ID是: ' . $res['id'];
//查詢
foreach($obj->read() as $value){
    print_r($value); echo '<br>';
}
echo '<hr>';
//修改
//$data = [
//    'name' => '張小凡',
//    'position' => '悲劇男二號'
//];
//print_r($obj->update($data,'staff_id = 16'));

//刪除
//echo $obj->delete('staff_id = 16');

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

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


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

老師批語:總結(jié)的到位
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

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

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