首页 >后端开发 >php教程 > php策略模式的有关问题~下~多谢 帮改下下面的代码

php策略模式的有关问题~下~多谢 帮改下下面的代码

WBOY
WBOY原创
2016-06-13 13:35:20878浏览

php策略模式的问题~~~高手指点下~~谢谢 帮改下下面的代码
我把学武术 和使用武器做成了接口,行为做成了类 但怎么用都是很乱,,高手指点下。完了立刻给分

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php abstract class person{
    
    abstract public  function speak();
    public $a;
    public $wq;
    public function studywx($wx){
        $a=new $wx;
        return $a;
    }
    public function usewq($wq){
        $a=new $wq;
        return $wq;
    }
}
class chperson extends  person{
    public function speak(){
        echo "中国人在说话,各路让行!~";
    }
}
class amperson extends  person{
    public function speak(){
        echo "美国人在说,叽叽喳喳!~";
    }
}
class enperson extends  person{
    public function speak(){
        echo "英国人在叫,汪汪汪~";
    }
}
interface istudy{
    public function studywx();
    public function studyyy();
}
class studyw implements istudy{
    public function studywx(){
        echo "学习武术";
    }
}
class studtq implements istudy{
    public function studywx(){
        echo "学习泰拳";
    }
}
interface iwq{
    public function usewq();
}
class jian implements iwq{
    public function usewq(){
            echo "用剑"
        }

}
class qiang implements iwq{
    public function usewq(){
            echo "用洋枪"
        }

}
class xiaoqiang implements iwq{
    public function usewq(){
            echo "用小米加布枪"
        }

}
$chperson=new chperson();
$chperson->studywx(studtq);
$chperson->usewq(xiaoqiang);
$chperson->studywx(studtq);
$amperson=new amperson();
$amperson->studywx(studyw);
$amperson->usewq(xiaoqiang);
$amperson->studywx(qiang);


?>


------解决方案--------------------
稍微简化了你的例子,看注释应该清晰多了吧
PHP code
<?php abstract class person{
    abstract public function speak();
    public $a;
    public $wq;
    public function studywx($wx){
         $wx->studywx();
    }
    public function usewq($wq){
        $wq->usewq();
    }
}
class chperson extends  person{
    public function speak(){
        echo "中国人在说话,各路让行!~";
    }
}
class amperson extends  person{
    public function speak(){
        echo "美国人在说,叽叽喳喳!~";
    }
}

interface istudy{
    public function studywx();
}
class studyw implements istudy{
    public function studywx(){
        echo "如来神掌";
    }
}
class studtq implements istudy{
    public function studywx(){
        echo "葵花宝典";
    }
}

interface iwq{
    public function usewq();
}
class jian implements iwq{
    public function usewq(){
        echo "倚天剑";
    }
}
class qiang implements iwq{
    public function usewq(){
        echo "洋枪";
    }
}

$chiness=new chperson();//上来个中国人
$chiness->studywx(new studyw()); //给本如来神掌学
$chiness->usewq(new jian());//给他把倚天剑

$american=new amperson();//上来个美国人
$american->studywx(new studtq());//给本葵花宝典学
$american->usewq(new qiang());//给他个破洋枪 <div class="clear">
                 
              
              
        
            </div>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn