查看文章 |
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 |
最近读者: