这样就算会了PHP么?-11

时间:2021-02-22 03:39:40

PHP中关于类的基本内容练习:

<?php
    class SportObject{

        public $name;
        public $height;
        public $avirdupois;

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

        function beatBasketball() {

            if ($this->height > 180 and $this->avoirdupois <= 100) {
                return $this->name.", 符合打篮球的要求。";
            } else {
                return $this->name.", 不符合打篮球的要求。";
            }
        }
        function showMe(){
            echo '这句话不会显示。';
        }
        function __destruct() {
            echo "<p><b>对象被销毁,调用析构函数。</b></p>";
        }
    }

    class BeatBasketBall extends SportObject{
        public $age;

        function __construct($name, $height, $age) {
            $this->name = $name;
            $this->height = $height;
            $this->age = $age;
        }
        function showMe() {
            if ($this->age > 18) {
                return $this->name.",符合打篮球的要求.";
            } else {
                return $this->name.",不符合打篮球的要求.";
            }
        }
    }

    class WeightLifting extends SportObject {
        function showMe() {
            if ( $this->avoirdupois < 85) {
                return $this->name.",符合举重.";
            } else {
                return $this->name.",不符合举重.";
            }
        }
    }

    $sport1 = new SportObject('流星', '185', '80');
    echo $sport1->beatBasketball();
    echo "<br>";
    $sport2 = new SportObject('天飞', '185', '80');
    echo $sport2->beatBasketball();
    echo "<br>";
    $Basketball = new BeatBasketBall('明日','190', '68');
    $weightlifting = new WeightLifting('科技', '185', '80', '20','男');
    echo "<br>";
    echo $Basketball->showMe();
    echo "<br>";
    echo $weightlifting->showMe();
    echo "<br>";

    class BookObject{
        const BOOT_TYPE = '计算机图书';
        public $object_name;

        function setObjectName($name) {
            $this->object_name = $name;
        }
        function getObjectName(){
            return $this->object_name;
        }
    }
    $c_book = new BookObject();
    $c_book->setObjectName("PHP类");
    #echo BookObject::BOOK_TYPE."->";
    echo $c_book->getObjectName();

    class C {
        function __call($name, $num) {
            echo "方法名称: ".$name."<p></p>";
            echo "参数存在的个数: ".count($num)."<p></p>";
            if (count($num) == 1) {
                echo $this->list1($a);
            }
            if (count($num) == 2) {
                echo $this->list2($a, $b);
            }
        }
        public function list1($a) {
            return "this is function list1";
        }
        public function list2($a, $b) {
            return "this is function list2";
        }
    }
    echo "<br>";
    $a = new C;
    $a->listshow(1, 2);
    echo "<br>";
    $b = new C;
    $b->listshow(1);

?>

这样就算会了PHP么?-11