About_类与对象03

php中的static:

1:属于静态变量:;

2:是全部类的属性;

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

eg:1

 <html>
<head>
<title>static测试</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<?PHP
class Person{ static $type = "人物";
public $name = "张三";
public $age = 18; public function intro(){
echo Person::$type."<br/>".$this->name."<br/>".$this->age."<br/>";
}
} $p1 = new Person(); $p1->intro();
?>
</body>
</html>

About_类与对象03

eg:2

 <html>
<head>
<title>static测试</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<?PHP
class Person{ static $type = "人物";
public $name = "张三";
public $age = 18; public function intro(){
echo Person::$type."<br/>".$this->name."<br/>".$this->age."<br/>";
}
} $p1 = new Person();
Person::$type = "张三不是人!"; $p1->intro();
?>
</body>
</html>

About_类与对象03

eg:3

class类的继承

 <html>
<head>
<title>class类的继承</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<?PHP
class Person{ static $type = "人物";
public $name = "张三";
public $age = 18; public function intro_Person(){
echo $this->name."<br/>".$this->age."<br/>";
}
} class Teacher extends Person{ public function intro_Teacher(){
echo $this->age;
} } class Students extends Teacher{ public function intro_Students(){
echo $this->age;
} } $p1 = new Students(); $p1->intro_Students();
?>
</body>
</html>
上一篇:结合JDK源码看设计模式——单例模式


下一篇:mvc EF 数据保存时,报错:”对一个或多个实体的验证失败……“之解决