<?php
class
 Person {
    // 下面是人的成员属性
    var $name; // 人的名字
    var $sex; // 人的性别
    var $age; // 人的年龄
              // 定义一个
构造方法
参数为属性姓名$name、性别$sex 和年龄$age 进行赋值
              // function construct($name="", $sex="",$age="")
    function construct($name, $sex, $age) {
        $this->name = $name;
        $this->sex = $sex;
        $this->age = $age;
    }
    // 这个人可以说话的方法, 说出自己的属性
    function say() {
        
echo
 "我的名字叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this
        ->age . "<br>";
    }
    // 对象克隆时自动调用的方法, 如果想在克隆后改变原对象的内容,需要在clone()中重写原来的属性和方法。
    function clone() {
        // $this 指的复本p2, 而$that 是指向原本p1,这样就在本方法里,改变了复本的属性。
        $this->name = "我是复制的张三$that->name";
        // $this->age = 30;
    }
}
$p1 = new Person ( "张三", "男", 20 );
$p2 = clone $p1;
$p1->say ();
$p2->say ();
?>成功运行此PHP程序后的结果如下:
我的名字叫:张三 性别:男 我的年龄是:20
我的名字叫:我是复制的张三 性别:男 我的年龄是:20
| 欢迎光临 一起源码网 (https://www.171739.xyz/) | Powered by Discuz! X3.3 |