About_类与对象03

php中的static

1:属于静态变量:;

2:是全部类的属性;

3:调用静态变量要用::(两个冒号)。

eg:1

 1 <html>
 2 <head>
 3     <title>static测试</title>
 4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
 5 </head>
 6 <body>
 7 <?PHP
 8 class Person{
 9 
10     static $type = "人物";
11     public $name = "张三";
12     public $age = 18;
13 
14     public function intro(){
15         echo Person::$type."<br/>".$this->name."<br/>".$this->age."<br/>";
16     }
17 }
18 
19 $p1 = new Person();
20 
21 $p1->intro();
22 ?>
23 </body>
24 </html>

 

eg:2

 1 <html>
 2 <head>
 3     <title>static测试</title>
 4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
 5 </head>
 6 <body>
 7 <?PHP
 8 class Person{
 9 
10     static $type = "人物";
11     public $name = "张三";
12     public $age = 18;
13 
14     public function intro(){
15         echo Person::$type."<br/>".$this->name."<br/>".$this->age."<br/>";
16     }
17 }
18 
19 $p1 = new Person();
20 Person::$type = "张三不是人!";
21 
22 $p1->intro();
23 ?>
24 </body>
25 </html>

 

eg:3

class类的继承

 1 <html>
 2 <head>
 3     <title>class类的继承</title>
 4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
 5 </head>
 6 <body>
 7 <?PHP
 8 class Person{
 9 
10     static $type = "人物";
11     public $name = "张三";
12     public $age = 18;
13 
14     public function intro_Person(){
15         echo $this->name."<br/>".$this->age."<br/>";
16     }
17 }
18 
19 class Teacher extends Person{
20 
21     public function intro_Teacher(){
22         echo $this->age;
23     }
24 
25 }
26 
27 class Students extends Teacher{
28 
29     public function intro_Students(){
30         echo $this->age;
31     }
32 
33 }
34 
35 $p1 = new Students();
36 
37 $p1->intro_Students();
38 ?>
39 </body>
40 </html>

 

posted @ 2014-05-22 22:33  moemiss  阅读(201)  评论(0编辑  收藏  举报