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

php继承多态

2017-05-09 15:09 239 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>

<?php

/*
class Ren
{
public $name;

function Say()
{
echo $this->name."正在讲话";
}
}

class China extends Ren
{
//子类对父类的方法进行重写
function Say()
{
parent::Say();
echo "你好";
}

function Run()
{
echo $this->name."正在跑步";
}
}

class America extends Ren
{
//子类对父类的方法进行重写
function Say()
{
echo "hello";
}
}*/

/*$c = new China();
$c->Say();
$a = new America();
$a->Say();
var_dump($c);
*/

//overload 重载
//可以使类里面的某个方法产生多种效果,根据传入的参数不同,可以执行不同的逻辑
//也是多态的一种,编译多态
/*  class Test
{
public string show()
{
return "0参";
}
public string show(string s)
{
return "1参";
}
public string show(string s,string a)
{
return "2参";
}
}

Test t = new Test();

t.show("a","b");
*/

//父类
/*  class Ren
{
public virtual string Say()
{
return "说话";
}
}

//子类
class China : Ren
{
public override string Say()
{
return "你好";
}
}
//子类
class America : Ren
{
public override string Say()
{
return "hello";
}
}

//父类引用r指向子类实例
Ren r = new America();
MessageBox.Show(r.Say());

r = new China();
MessageBox.Show(r.Say());

//子类的对象可以代替父类的对象
America a = new America();
MessageBox.Show(Eat(a));
//怪兽吃人,需要一个人的参数
public string Eat(Ren r)
{
return "这个人味道不错!";
}
*/

?>

</body>
</html>


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