这篇文章主要为大家详细介绍了一步一步学习PHP(6) 面向对象,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编小韵来看看吧!
但是我们知道,面向对象有三大特征:继承,多态和封装。代码如下:
class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
}
class Student extends People
{
private $grade;
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
}
代码如下:
class Student extends People
{
public function GetName()
{
return "kym";
}
private $grade;
public function SayHello()
{
echo("Good Morning,".self::GetName());
//echo("Good Morning,".$this->GetName());
}
}
代码如下:
<?php
abstract class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
abstract function SayHello();
}
class Student extends People
{
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
}
$s=new Student();
$s->SetName("kym");
$s->SayHello();
?>
代码如下:
<?php
abstract class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
abstract function SayHello();
}
interface IRun
{
function Run();
}
class Student extends People implements IRun
{
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
public function Run()
{
echo("两条腿跑");
}
}
$s=new Student();
$s->SetName("kym");
$s->SayHello();
$s->Run();
?>
代码如下:
<?php
class Person
{
private $name;
private $age;
public function Person($name,$age)
{
$this->name=$name;
$this->age=$age;
}
public function SayHello()
{
echo("Hello,My name is ".$this->name.".I'm ".$this->age);
}
}
$p=new Person("kym",22);
$p->SayHello();
?>
代码如下:
<?php
class Person
{
private $name;
private $age;
public function Person($name,$age)
{
$this->name=$name;
$this->age=$age;
}
public function SayHello()
{
echo("Hello,My name is ".$this->name.".I'm ".$this->age);
}
}
class Student extends Person
{
private $score;
public function Student($name,$age,$score)
{
$this->Person($name,$age);
$this->score=$score;
}
public function Introduce()
{
parent::SayHello();
echo(".In this exam,I got ".$this->score);
}
}
$s=new Student("kym",22,120);
$s->Introduce();
?>
代码如下:
class Student extends Person
{
private $score;
public function Student($name,$age,$score)
{
$this->Person($name,$age);
$this->score=$score;
}
public function Introduce()
{
parent::SayHello();
echo(".In this exam,I got ".$this->score);
}
function __destruct()
{
echo("我要被卸载了");
}
}
本文来自:http://www.q1010.com/173/13806-0.html
注:关于一步一步学习PHP(6) 面向对象的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。