zoukankan      html  css  js  c++  java
  • php 实现Iterator 接口

    <?php

    class MyIterator implements Iterator{


    private $var = array();

    public function __construct($array){

    if(is_array($array)){

    $this->var = $array;

    }

    }

    public function rewind(){

    echo "reminding<br>";

    reset($this->var);

    }
    public function current(){

    $var = current($this->var);

    echo "current :$var<br>";

    return $var;

    }

    public function key(){

    $var = key($this->var);

    echo "key::$var<br>";

    return $var;


    }

    public function next(){

    $var = next($this->var);
    echo "next:$var<br>";

    return $var;

    }

    public function valid(){

    $var = $this->current() !== false;

    echo "valid:{$var}<br>";

    return $var;


    }


    }
    $value = array(1,2,3);

    $it = new MyIterator($value);

    foreach($it as $a=> $b){

    print "$a:$b<br>";
    }

    <?php

    class MyIterator implements Iterator{


    private $var = array();

    public function __construct($array){

    if(is_array($array)){

    $this->var = $array;

    }

    }

    public function rewind(){

    echo "reminding<br>";

    reset($this->var);

    }
    public function current(){

    $var = current($this->var);

    echo "current :$var<br>";

    return $var;

    }

    public function key(){

    $var = key($this->var);

    echo "key::$var<br>";

    return $var;


    }

    public function next(){

    $var = next($this->var);
    echo "next:$var<br>";

    return $var;

    }

    public function valid(){

    $var = $this->current() !== false;

    echo "valid:{$var}<br>";

    return $var;


    }


    }
    $value = array(1,2,3);

    $it = new MyIterator($value);

    foreach($it as $a=> $b){

    print "$a:$b<br>";
    }


    ?>


    ?>

  • 相关阅读:
    isequal 和startswith 使用
    UVa10340
    UVa1368
    UVa455
    UVa1225
    UVa1586
    UVa 1585
    UVa10082
    UVa272
    NYOJ1
  • 原文地址:https://www.cnblogs.com/paulversion/p/8947972.html
Copyright © 2011-2022 走看看