
说明
1、将两个原本不相关的类结合在一起,然后利用两个类中的方法和属性,输出一份新的结果。
2、结构分为抽象类、被提炼的抽象类、实现类、具体实现类和客户端代码。
实例
/**
*颜色抽象类
*ClassColour
*/
abstractclassColour
{
/**
*@returnmixed
*/
abstractpublicfunctionrun();
}
/**
*黑色
*ClassBlack
*/
classBlackextendsColour
{
publicfunctionrun()
{
//TODO:Implementrun()method.
return'黑色';
}
}
/**
*白色
*ClassWhite
*/
classWhiteextendsColour
{
publicfunctionrun()
{
//TODO:Implementrun()method.
return'白色';
}
}
/**
*红色
*ClassRed
*/
classRedextendsColour
{
publicfunctionrun()
{
//TODO:Implementrun()method.
return'红色';
}
}
/**
*形状抽象类
*ClassShape
*/
abstractclassShape
{
/**
*颜色
*@varColour
*/
protected$colour;
/**
*Shapeconstructor.
*@paramColour$colour
*/
publicfunction__construct(Colour$colour)
{
$this->colour=$colour;
}
/**
*@returnmixed
*/
abstractpublicfunctionoperation();
}
/**
*圆形
*ClassRound
*/
classRoundextendsShape
{
/**
*@returnmixed|void
*/
publicfunctionoperation()
{
//TODO:Implementoperation()method.
echo$this->colour->run().'圆形<br>';
}
}
/**
*长方形
*ClassRectangle
*/
classRectangleextendsShape
{
/**
*@returnmixed|void
*/
publicfunctionoperation()
{
//TODO:Implementoperation()method.
echo$this->colour->run().'长方形<br>';
}
}
/**
*正方形
*ClassSquare
*/
classSquareextendsShape
{
/**
*@returnmixed|void
*/
publicfunctionoperation()
{
//TODO:Implementoperation()method.
echo$this->colour->run().'正方形<br>';
}
}
//客户端代码
//白色圆形
$whiteRound=newRound(newWhite());
$whiteRound->operation();
//黑色正方形
$blackSquare=newSquare(newBlack());
$blackSquare->operation();
//红色长方形
$redRectangle=newRectangle(newRed());
$redRectangle->operation();
//运行结果
白色圆形
黑色正方形
红色长方形原文来自:https://www.py.cn                    © 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
    

















































 
        

暂无评论内容