php面向對象三,繼承父類extends

<!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 Human{
        private $height=170;
         public function show()
            {
                echo $this->height;
            }
        }
        /**
        * 繼承Human父類
        */
        class Student extends Human
        {   
            private $height=180;
            
            public function show()
            {   //調用父類的方法
                parent::show();
                echo $this->height;
            }
        }
        $st=new Student();
        $st->show();
        ?>
    </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>構造方法的繼承</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
    <body>
        <?php 
        /**
        * 
        */
        class Human
        {
            
            function __construct()
            {
                echo "helloworld";
            }
        }
        /**
        * 構造函數也是能夠被繼承的
        */
        class Student extends Human
        {
            
            function __construct()
            {
                echo "hellonihao ";
            }
        }
        $s=new Student();
        ?>
    </body>
</html>
private 只能在內部類中訪問,protected能夠被子類內部訪問。
<!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 
         // private 私有,只能被內部調用。
        //protected能夠被繼承的子類調用
        /**public private  protected
    內部類   Y      Y        Y
    子類     Y      N        Y
    外部類   Y       N       N
        * 
        */
        class Human
        {
           private $a="1";
           protected $b="2";
           public $c="3";
         public function talk()
           {
               echo $this->a,',',$this->b,',',$this->c;
           }
        }
        class student extends Human{
            public function say()
            {   
                // echo $this->a,'<br/>';private只能在本類內調用
                echo $this->b,',',$this->c;
            }
        }
        $s=new student();
        $s->say();
        ?>
    </body>
</html>
相關文章
相關標籤/搜索