Blogger Information
Blog 41
fans 2
comment 1
visits 26148
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0502作业
郭恒的博客
Original
1012 people have browsed it

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/6 0006
 * Time: 14:33
 */

class friend
{
    //类属性
    private $name = '朋友1';
    private $sex = '男';
    private $age = '18';

    //类方法
//    public function getinfo($name = '', $age = 0)
//    {
//        //对象属性的初始化,引用类成员变量要是用为变量$this,当前类实例对象
//        //->是对象访问符
//        $this->name = empty($name) ? $this->name : $name;
//        $this->age = ($age==0) ? $this->$age : $age;
//        return '姓名:'.$this->name.',年龄:'.$this->age.'<br>';
//    }
//类的构造方法在实例化中自动调用
//构造方法也是构造器;对象属性的初始化;
    public function __construct($name, $sex, $age)
    {
        $this->name = $name;
        $this->sex = $sex;
        $this->age = $age;
    }
    //查询器
    //双下划綫开始的叫魔术方法
    public function __get($name)
    {
        // TODO: Implement __get() method.
        return $this->$name;
    }

    /**
     * @return string
     */
    public function getSex($mima = '')
    {
        $msg = '非法访问';
        if (!empty($mima) && $mima != '123') {
            $msg = $this->sex;
        }
        return $msg;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function getAge()
    {
        return $this->age;
    }
    //设置器
    //双下划綫为魔术方法
    public function __set($name, $value)
    {
        if ($name == 'age') {
            if (in_array($value, range(14, 89))) {
                $this->$name = $value;
            }
        }
        // TODO: Implement __set() method.
        $this->$name = $value;
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @param string $age
     */
    public function setAge($age)
    {
        if (in_array($age, range(14, 49))) {
            $this->age = $age;
        }

    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/6 0006
 * Time: 14:50
 */
require './class/friend.php';
//$fi = new friend();
//var_dump($fi);
//$fi->name = 'haha';
//$fi->age = '19';
//$fi->sex = '男';
//echo $fi->name;
//echo $fi->getinfo('你好',72);

$gh = new friend('郭恒', '共和国', 22);
//echo $gh->getSex(123);
//echo $gh->getSex(1);
//echo $gh->setName('但是');

//$gh->setAge('10');
// $gh->setAge('20');
//echo $gh->getAge();

//echo $gh->name;
$gh->age = 24;
echo $gh->age;

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
  • 2018-03-16 15:12:44