百度首页 | 百度空间
 
查看文章
 
PHP5 Interface Polymorphisn
2007-03-10 06:15 A.M.

PHP5 接口多态


<?php

/**
 * Shape Interface
 * 
 * @version 1.0
 * @copyright 
 */
interface Shape
{
    public function draw();
}

/**
 * Triangle 
 * 
 * @uses Shape
 * @version 1.0
 * @copyright
 */
class Triangle implements Shape
{   
    /**
     * draw 
     * 
     * @access public
     * @return void
     */
    public function draw()
    {
        print "Triangle::draw()\n";
    }
}

/**
 * Rectangle 
 * 
 * @uses Shape
 * @version 1.0
 * @copyright
 */
class Rectangle implements Shape
{
    /**
     * draw 
     * 
     * @access public
     * @return void
     */
    public function draw()
    {
        print "Rectangle::draw()\n";
    }
}

/**
 * Test Polymorphisn
 * 
 * @version 1.0
 * @copyright
 */
class Test
{
    public function drawNow(Shape $shape)
    {
        $shape->draw();
    }
}

$test = new Test();
$test->drawNow(new Triangle());
$test->drawNow(new Rectangle());

/* vim: set expandtab tabstop=4 shiftwidth=4: */




Auto posted by Wicklow

类别:默认分类 | 添加到搜藏 | 浏览() | 评论 (1)
 
最近读者:
 
网友评论:
1
2007-03-10 12:45 P.M.
学习下!
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu