Blogger Information
Blog 14
fans 0
comment 1
visits 6528
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
9月3日作业
狼图腾的博客
Original
568 people have browsed it

实例

<meta charset="utf-8">
<?php
//类的声明与实例化
class demo1
{

}
//可以添加属性和方法
$demo1 = new Demo1();
$demo1->name = '胡老师';
$demo1->sex = '男';
$demo1->hello = function (){
    return '自定义方法';
};
echo $demo1->name,':',$demo1->sex,'<br>';
echo '<hr>';
echo call_user_func($demo1->hello);
//对象的封装、继承、多态
class Demo2
{
  public $name;
  public $salary = 5000;
  protected $sex = 1;
  private $age = 33;
    public function getSex()
    {
        return ($this->sex ==0) ? '男' : '女';
    }
    public function getAge()
    {
        return ($this->sex ==0) ?$this->age : '保密';
    }
}
$demo2 = new Demo2;
echo $demo2->salary,'<hr>';
echo $demo2->getSex();
echo $demo2->getAge();

实例

<meta charset="UTF-8">
<?php
//类常量初始化,属性重载
class Demo3
{
    const SITE_NAME = 'php中文网';
    //    声明三个私用属性
    private $name ;
    private $course ;
    private $grade ;
    public function __construct($name,$course,$grade)
    {
        $this->name = $name;
        $this->course = $course;
        $this->grade = $grade;
    }
    public function show()
    {
        return $this->name. '的' .$this->course. '课程成绩:' .$this->grade. '分!';
    }
    public function __get($name)
    {
        if($name == 'grade'){
            return $name. '不许查看';
        }
    }
    public function __set($name,$value)
    {
        if ($name == 'grade') {
            return $name . '不许';
        }
        $this->$name = $value;
    }
}
echo Demo3::SITE_NAME;
echo '<hr>';
$demo3 = new Demo3('pig','php','90');
var_dump($demo3);
echo '<hr>';
echo $demo3->show();
echo $demo3->name;
echo $demo3->grade;
echo '<hr>';
$demo3->course = 'css';
echo '课程是:',$demo3->course;

运行实例 »

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

运行实例 »

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


Correction status:qualified

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