Home  >  Article  >  Backend Development  >  PHP面向接口编程 耦合设计模式 简单范例_PHP

PHP面向接口编程 耦合设计模式 简单范例_PHP

WBOY
WBOYOriginal
2016-06-01 12:17:11883browse
复制代码 代码如下:
interface js{
function ys($a,$b);
}
class Af implements js{
function ys($a,$b){
return "加法运算......结果为:".($a+$b);
}
}
class Jf implements js{
function ys($a,$b){
return "减法运算......结果为:".($a-$b);
}
}
class AAf implements js{
function ys($a,$b){
return "乘法运算......结果为:".($a*$b);
}
}
class JJf implements js{
function ys($a,$b){
return "除法运算......结果为:".($a/$b);
}
}
class Modf implements js{
function ys($a,$b){
return "取模运算......结果为:".($a % $b);
}
}
class China implements js{
public $varl=null;//这里直接:public $varl = new nothingx(); 会出错。
function __construct(){
$this->varl = new nothingx();
}
function ys($a,$b){
return $this->varl->say();
}
}
/*也可以用继承的方式实现哟:
class China extends nothingx implements js{
function ys($a,$b){
return parent::say();
}
}
*/
class nothingx{
function say(){
return "我什么运算都不做...只是为了实现‘耦合设计模式'...我是出来打酱油的......";
}
}
class test{
private $one;
private $two;
public function __construct($x,$y){
$this->one=$x;
$this->two=$y;
echo "面向对象程序设计——接口
Class test初始化:
属性\$one=".$this->one."  属性\$two=".$this->two."
";
}
function display(js $a){
return "用PHP接口技术实现的运算——开始运算啦:
".$a->ys($this->one,$this->two)."
";
}
}
$t=new test(103,2);
$t1=new jf;
$t2=new China;
echo $t->display($t1);
echo $t->display($t2);
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn