Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:继承是类复用的重要手段, 也是构建大型项目的基础
extends
关键字继承父类
//父类
class Father{
//公开属性
public $name = '张三';
public $age = 40;
//受保护属性
protected $like = '摄影';
//私有属性
private $money = 12000;
//公开方法
public function fat1(){
return "姓名:{$this->name}<br>年龄:{$this->age}<br>爱好:{$this->like}";
}
public function fat2(){
return "姓名:{$this->name}<br>年龄:{$this->age}<br>爱好:{$this->like}<br>工资:{$this->money}";
}
}
//Body子类 继承(extends) Father父类
class Body extends Father {
}
$father = new Father();
echo $father->fat1();
echo '<hr>';
$body = new Body();
//通过子类在外部访问父类中的公开属性
echo $body->name;
echo $body->age;
echo '<hr>';
//通过子类访问父类中的公开方法
echo $body->fat1();
echo '<hr>';
echo $body->fat2();
class Father{
public $name = '张三';
public $age = 40;
protected $like = '摄影';
private $money = 12000;
public function fat1(){
return "姓名:{$this->name}<br>年龄:{$this->age}<br>爱好:{$this->like}";
}
public function fat2(){
return "姓名:{$this->name}<br>年龄:{$this->age}<br>爱好:{$this->like}<br>工资:{$this->money}";
}
}
class Body extends Father {
//重写父类属性
protected $like = '钓鱼';
//重写父类方法
public function fat1(){
return parent::fat1().'<br>最近又喜欢上钓鱼了~~!';
}
public function fat2(){
//重写父类方法不能调用父类的私有属性
return "姓名:{$this->name}<br>年龄:{$this->age}<br>爱好:{$this->like}<br>工资:{$this->money}";
}
}
$father = new Father();
echo $father->fat2();
echo '<hr>';
$body = new Body();
//访问子类重写方法
echo $body->fat1();
echo '<hr>';
echo $body->fat2();
class Father{
public $name = '张三';
public $age = 40;
protected $like = '摄影';
private $money = 12000;
public function fat1(){
return "姓名:{$this->name}<br>年龄:{$this->age}<br>爱好:{$this->like}";
}
}
class Body extends Father {
//扩展属性
protected $profession = '医生';
//重写父类方法
public function fat1(){
return parent::fat1().'<br>最近又喜欢上钓鱼了~~!';
}
//扩展方法
public function fat3(){
return "姓名:{$this->name}<br>年龄:{$this->age}<br>职业:{$this->profession}";
}
}
$father = new Father();
echo $father->fat1();
echo '<hr>';
$body = new Body();
echo $body->fat1();
echo '<hr>';
echo $body->fat3();
abstract
关键字来定义抽象类
// 方法一:
// 设计类
abstract class Father{
protected $name = '张三';
protected $age = 40;
protected $like = '摄影';
// 只要一个类中有一个抽象方法, 那么这个类就是抽象类
// 抽象方法:没有方法体
abstract protected function fat1();
}
//工作类
class Body extends Father {
private $money = 12000;
// 必须将抽象类中的抽象方法实现
public function fat1(){
return "姓名:{$this->name}<br>年龄:{$this->age}<br>爱好:{$this->like}<br>工资:{$this->money}";
}
}
$body = new Body();
echo $body->fat1();
// 方法二;
// 设计类
abstract class Father{
protected $name = '张三';
protected $age = 40;
protected $like = '摄影';
protected function fat1(){
return "姓名:{$this->name}<br>年龄:{$this->age}<br>爱好:{$this->like}";
}
// 只要一个类中有一个抽象方法, 那么这个类就是抽象类
// 抽象方法:没有方法体
abstract protected function fat2();
}
// 工作类不一定是可以被实例化的普通类,也可是一个抽象类
abstract class Body extends Father {
private $money = 12000;
// 必须将抽象类中的抽象方法实现
protected function fat2(){
return parent::fat1() ."<br>工资:{$this->money}";
}
}
// 最终工作类: 允许实例化
class Bodys extends Body {
public function fat2(){
return parent::fat2();
}
}
$bodys = new Bodys();
echo $bodys->fat2();
interface
// 1、单接口
interface iFather{
// 接口常量
const NATION = '中国';
// 接口方法
public static function fat1();
}
// 工作类实现接口
class Body implements iFather
{
protected static $name = '张三';
// 接口中的抽象方法,必须在工作类实现
public static function fat1()
{
return self::$name . ' 的国籍是: ' . iFather::NATION;
}
}
echo Body::fat1();