>백엔드 개발 >PHP 튜토리얼 >php策略模式的有关问题~下~多谢 帮改下下面的代码

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

WBOY
WBOY원래의
2016-06-13 10:17:53897검색

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

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?phpabstract 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
<?phpabstract 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으로 문의하세요.