Interface在面向对像中的使用

<?php
/**
 * User: Damon
 * Date: 2020/2/11
 * Time: 15:20
 */
interface UsbPlayer
{
    function push();
    function pop();
    function play();
    function stop();
}


class Computer implements UsbPlayer {

    function push()
    {
        // TODO: Implement push() method.
        echo 'Computer push a usb driver.';
    }

    function pop()
    {
        // TODO: Implement pop() method.
        echo 'usb driver popped from Computer.';
    }

    function play()
    {
        echo 'Computer\'s music player running...';
    }

    function stop(){
        echo 'Computer\'s music player stopping...';
    }
}


class Phone implements UsbPlayer{

    function push()
    {
        echo 'Phone push a usb driver';
        // TODO: Implement push() method.
    }

    function pop()
    {
        echo 'usb driver popped from Phone.';
        // TODO: Implement pop() method.
    }

    function play()
    {
        echo 'Phone\'s music player running...';
    }

    function stop()
    {
        echo 'Phone\'s music player stopping...';
    }
}


function playMusic(UsbPlayer $player){
    $player->push();
    $player->play();
}


function stopMusic(UsbPlayer $player){
    $player->stop();
    $player->pop();
}

playMusic(new Computer());

stopMusic(new Phone());
0

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据