Rumah > pembangunan bahagian belakang > tutorial php > php策略模式的有关问题~下~多谢 帮改下下面的代码

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

WBOY
Lepaskan: 2016-06-13 10:17:53
asal
901 orang telah melayarinya

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);?>
Salin selepas log masuk


------解决方案--------------------
稍微简化了你的例子,看注释应该清晰多了吧
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>
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan