Heim  >  Artikel  >  Backend-Entwicklung  >  javascript设计模式 PHP面向接口编程 耦合设计模式 简单范例

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

WBOY
WBOYOriginal
2016-07-29 08:44:391387Durchsuche

复制代码 代码如下:


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->
$this->two=$y;
echo "面向对象程序设计——接口


Class test初始化:
属性\$  属性\$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);
?>

以上就介绍了javascript设计模式 PHP面向接口编程 耦合设计模式 简单范例,包括了javascript设计模式方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn