php面向对象四。多态静态方法和属性
2016-05-01 00:00
731 查看
<!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>
相关文章推荐
- PHP getenv函数
- phpStudy 配置 SSL 证书
- ContentProvider
- 解决getOutputStream() has already been called for this response
- php下使用curl进行多种数据编码方式的POST请求
- PHP学习笔记(一)数组
- php创建锁机制
- PHP生成xml的方法
- PHP session锁
- PHP获取用户真实IP
- 构建自己的PHP框架--构建缓存组件(2)
- sublime text 配置php调试环境(快捷键)
- php判断图片的格式
- 2008 R2 创建FTP 一不小心踩到巨坑
- 【php】socket编程基础知识
- php中构造函数和析构函数
- PHP基础数组操作(二)
- thinkPHP的<eq>标签
- php生成印章 代码片段
- Win10 取消开机密码 (netplwiz没用)