PHP编程语言手册三:PHP中的面向对象

2019-03-1419:26:22后端程序开发Comments1,444 views字数 2006阅读模式
1.类的定义和构造函数+析构函数
PHP编程语言手册三:PHP中的面向对象
---->[obj/Shape.php]----------------
<?php
namespace toly1994;

class Shape{
    public function __construct(){
        echo "Shape构造函数";
    }
    
    function __destruct(){
        echo "Shape析构函数";
    }
}

---->[obj/Client.php]----------------
<?php
include './Shape.php';//引入文件
use toly1994\Shape;

$shape = new Shape();
复制代码

2.类的封装(成员变量,成员方法)
---->[obj/Shape.php]----------------
<?php
namespace toly1994;
class Shape{
    private $name;
    public function __construct($name){
        echo "Shape构造函数<br/>";
        $this->name = $name;
    }
    public function getName(){
        return $this->name;
    }
    public function setName($name){
        $this->name = $name;
    }
    public function draw(){
        echo "绘制$this->name <br/>";
    }
    function __destruct(){
        echo "Shape析构函数";
    }
}

|-- 使用 --------------------------
$shape = new Shape("Shape");
$shape->draw();//绘制Shape
$shape->setName("四维空间<br/>");
echo $shape->getName();//四维空间 
复制代码

3.类的继承
---->[obj/Point.php]----------------
<?php
namespace toly1994;
class Point extends Shape{
    public $x;
    public $y;
}

|-- 使用 --------------------------
$point = new Point("二维点");
$point->draw();//绘制二维点
echo $point->getName();//二维点
$point->x=20;//二维点
echo $point->x;//20
复制代码

4.类的多态
---->[obj/Circle.php]----------------
<?php
namespace toly1994;
class Circle extends Shape{
    private $radius;
    public function getRadius(){
        return $this->radius;
    }

    public function setRadius($radius){
        $this->radius = $radius;
    }
    public function draw(){
        echo "Draw in Circle<br/>";
    }
}

---->[obj/Point.php]----------------
<?php
namespace toly1994;
class Point extends Shape{
    public $x;
    public $y;
    public function draw(){
        echo "Draw in Point<br/>";
    }
}

|-- 使用 --------------------------
$point=new Point("");
doDraw($point);//Draw in Point
$circle=new Circle("");
doDraw($circle);//Draw in Circle

function doDraw(Shape $shape){
    $shape->draw();
}
复制代码

5.接口及抽象类
抽象类---->[obj/Shape.php]----------------
abstract class Shape{
    ...
    //抽象方法
    abstract protected function draw();
}

接口---->[obj/Drawable.php]----------------
<?php
namespace toly1994;

interface Drawable{
    public function draw();
}

|-- 实现接口 implements 关键字-----------
include './Drawable.php';//引入文件
abstract class Shape implements Drawable

作者:张风捷特烈
链接:https://juejin.im/post/5c8a19d75188257dd56e7d91
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9977.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9977.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/9977.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定