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>
相关文章
相关标签/搜索