您的位置:首页 > 编程语言 > PHP开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: