作业一

题目

代码

<?php
class Car{
    private $type;
    private $color;
    public function __construct($type,$color){
        $this->type=$type;
        $this->color=$color;
    }
    public function __toString(){
        return $this->type;
    }
    public function __set($property,$val){
        $this->$property=$val;
    }
    public function __get($property){
        if (isset($this->$property)){
            return $this->$property;
        }else{
            echo "不存在属性";
            return null;
        }
    }
    public function getColor(){
        return $this->color;
    }
}
class Bus extends Car{
    private $quantity;
    public function __construct($type,$color,$quantity){
        $this->quantity=$quantity;
        parent::__construct($type,$color);
    }
    public function __toString(){
        return $this->quantity;
    }
    public function __get($property){
        if (isset($this->$property)){
            return $this->$property;
        }else{
            echo "不存在属性";
            return null;
        }
    }
}
$taxi=new Car("","blue");
$taxi->type='taxi';
echo $taxi->type.' '.$taxi->getColor();
echo '<br>';
$bus=new Bus('bus','red',10);
echo $bus->__toString().' '.$bus->quantity;

结果

作业二

题目

代码

<?php
final class Circle{
    private $radius;
    public function __construct($radius) {
        $this->radius=$radius;
    }
    public function getPerimeter(){
        return 2*$this->radius*pi();
    }
    public function getArea(){
        return $this->radius*$this->radius*pi();
    }
}
class Student{
    private $num;
    private $name;
    private $sex;
    private $class;
    public function __construct($num,$name,$sex,$class) {
        $this->num=$num;
        $this->name=$name;
        $this->sex=$sex;
        $this->class=$class;
    }
    public function __set($pro, $value) {
        $this->$pro=$value;
    }
    public function __get($pro) {
        return $this->$pro;
    }
    public function __unset($pro) {
        echo 'unset()删除私有成员自动调用'.'<br>';
        unset($this->$pro);
    }
    public function __isset($pro) {
        echo 'isset()测定私有成员自动调用'.'<br>';
        return isset($this->$pro);
    }
    public function getInfo(){
        echo '学号:'.$this->num.' 名字:'.$this->name.' 性别:'.$this->sex.' 班级:'.$this->class;
    }
    public function __call($method, $arguments) {
        echo '方法不存在 ';
    }
    protected function say(){
        echo 'hello';
    }
}
interface User{
    const MAX_LEVEL=100;
    function discount($ori);
}
class Users{
    public $level;
    public function __construct($level) {
        $this->level=$level;
    }
}
class normalUser extends Users implements User{
    function discount($ori) {
        return (self::MAX_LEVEL-0.1*$this->level)/self::MAX_LEVEL*0.9*$ori;
    }
}
class vipUser extends Users implements User {
    function discount($ori) {
        return (self::MAX_LEVEL-0.3*$this->level)/self::MAX_LEVEL*0.7*$ori;
    }
}
class innerUser extends Users implements User {
    function discount($ori) {
        return (self::MAX_LEVEL-0.5*$this->level)/self::MAX_LEVEL*0.5*$ori;
    }
}
function sale($user,$mon){
    echo $user->discount($mon).'<br>';
}
$circle1=new Circle(20);
echo '周长为:'.$circle1->getPerimeter().' 面积为:'.$circle1->getArea();
echo '<br>';
$jin=new Student('01','jin','男','3');
$jin->getInfo();
echo '<br>';
echo var_dump(isset($jin->name)).'<br>';
echo $jin->name.' <br>';
unset($jin->name);
echo var_dump(isset($jin->name)).'<br>';
$jin->say();
echo '<br><br>';
$normal=new normalUser(10);
sale($normal,500);
$vip=new normalUser(25);
sale($vip,500);
$inner=new innerUser(50);
sale($inner,500);

结果

Last modification:July 12th, 2020 at 01:08 am