Blogger Information
Blog 33
fans 0
comment 0
visits 24376
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类的实例化与属性重载 20180903 23:00
EmonXu的博客
Original
646 people have browsed it

下面通过一个案例,演示以下四点:

1: 类声明与类的实例化;
2: 类常量与类属性的重载;
3: 类的继承与方法重写;
4: 类中静态成员的声明与访问

实例

<?php

//编程1: 类声明与类的实例化;
//编程2: 类常量与类属性的重载;
//编程3: 类的继承与方法重写;
//编程4: 类中静态成员的声明与访问

class Animal{

    const SHOP_NAME='小皮宠物店';

    private $name;
    private $price;

    public function __construct($name,$price)
    {
        $this->name=$name;
        $this->price=$price;
    }

    public function __get($name)
    {
        // TODO: Implement __get() method.
        if($name=='price'){
            return '¥'.$this->price;
        }else {
            return $this->$name;
        }
    }



    public function __set($name, $value)
    {
        // TODO: Implement __set() method.
        $this->$name=$value;
    }
}

$dog=new Animal('博美',3000);

echo '欢迎来到',Animal::SHOP_NAME,'<hr>',$dog->name,'的价格是',$dog->price;


class Animal_count extends Animal{
    private $sale_count;

    public function __construct($name, $price,$sale_count=0)
    {
        parent::__construct($name, $price);
        $this->sale_count=$sale_count;
    }

    public function __get($name)
    {
        if ($name=='sale_count'){
           return $this->sale_count;
        }else{
        return parent::__get($name); // TODO: Change the autogenerated stub
    }}

}

$dog_sales =new Animal_count('博美',3000);
echo '<br>';

//echo var_export($dog_sales);
echo '定价太高,',$dog_sales->name,'的销量是',$dog_sales->sale_count,'<hr>';



echo '明日特价打折至:',$dog->price='1500','<br>';

class Animal_Sales extends Animal{
    public static $sale_count;

    public function __construct($name, $price,$sale_count=0)
    {
        parent::__construct($name, $price);
        self::$sale_count++;
    }

    public function __get($name)
    {
        if ($name=='sale_count'){
            return self::$sale_count;
        }else{
            return parent::__get($name); // TODO: Change the autogenerated stub
        }}

}

$dog_sales =new Animal_Sales('博美',3000);
echo '成功卖出!',$dog_sales->name,'的销量是',$dog_sales::$sale_count,'<hr>';
$dog_sales1 =new Animal_Sales('博美',3000);
echo '成功卖出!',$dog_sales1->name,'的销量是',$dog_sales1::$sale_count,'<hr>';
$dog_sales2 =new Animal_Sales('博美',3000);
echo '成功卖出!',$dog_sales2->name,'的销量是',$dog_sales2::$sale_count,'<hr>';
$dog_sales3 =new Animal_Sales('博美',3000);
echo '成功卖出!',$dog_sales3->name,'的销量是',$dog_sales3::$sale_count,'<hr>';
$dog_sales4 =new Animal_Sales('博美',3000);
echo '成功卖出!',$dog_sales4->name,'的销量是',$dog_sales4::$sale_count,'<hr>';

运行实例 »

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


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