php面向對象四。多態靜態方法和屬性

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>多態</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
    <body>
       <?php 
       class xs {
        public function show($g){
            $g->display();
        }
    }
        class dd {
            public function display(){
                echo "red show";
            }
       }
       class ff{
        public function display(){
            echo "blue show";
        }
       }
       //多態的使用
       $a=new dd();
       $b=new ff();
       $red=new xs();
       $red->show($a);
       $red->show($b);
       ?>
    </body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>靜態屬性和靜態方法 static</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
    <body>
        <?php 
        /**
        * 注意靜態方法存放在類當中,所以無需
        *類在內存中只有一個,靜態屬性只有一個
        */
        class Human 
        {
            public  static  $a=1;
            public function chang()
            {
                return Human::$a=9;

            }
        }
        echo Human::$a.'<br/>';
        $a=new Human();
        $b=new Human();
        echo $a->chang().'<br/>';
        echo $b->chang().'<br/>';

        /**
        *普通方法須要綁定$this,而靜態方法不須要this
        *不用聲明對象,能夠直接調用靜態方法
        *
        ***/
        class People
        {
            
          static public function cry()
          {
              echo "5555";
          }
        }
        People::cry();
            ?>
    </body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>靜態屬性和靜態方法 static</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
    <body>
        <?php 

        /**
        *總結方法用self::$a 和parent::$b來表示子類和父類
        *
        ***/
        class People
        {
            
          static public $m=1;
        }
       class P extends People{
        static public $n=2;
        public function getA(){
            echo self::$n;
        }
        public function getB(){
            echo parent::$m;
        }
       }
       $w=new P();
       $w->getA();
       $w->getB();
            ?>
    </body>
</html>
相關文章
相關標籤/搜索