From 8872d1a19868429408b250eb9f87c2b4d4305c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E9=9D=92=E5=87=A4?= <2658978250@qq.com> Date: Tue, 17 May 2022 21:08:54 +0800 Subject: [PATCH] tijiao --- ...42\345\220\221\345\257\271\350\261\241.md" | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 "\346\233\276\351\235\222\345\207\244/20220517-PHP\351\235\242\345\220\221\345\257\271\350\261\241.md" diff --git "a/\346\233\276\351\235\222\345\207\244/20220517-PHP\351\235\242\345\220\221\345\257\271\350\261\241.md" "b/\346\233\276\351\235\222\345\207\244/20220517-PHP\351\235\242\345\220\221\345\257\271\350\261\241.md" new file mode 100644 index 0000000..45c428a --- /dev/null +++ "b/\346\233\276\351\235\222\345\207\244/20220517-PHP\351\235\242\345\220\221\345\257\271\350\261\241.md" @@ -0,0 +1,114 @@ +1. 写一段代码,定义一个汽车类,有品牌与价格两种属性。并为类实例化对象,为对象的属性赋值并引用。 + + ```php + class Car + { + private $brand;//品牌 + private $price;//价格 + + function __construct($brand,$price){ + $this->brand=$brand; + $this->price=$price; + } + + /** + * @return mixed + */ + public function getBrand() + { + return $this->brand; + } + + /** + * @param mixed $brand + */ + public function setBrand($brand) + { + $this->brand = $brand; + } + + /** + * @return mixed + */ + public function getPrice() + { + return $this->price; + } + + /** + * @param mixed $price + */ + public function setPrice($price) + { + $this->price = $price; + } + function show(){ + echo "这是价格为".$this->price."的".$this->brand; + } + } + + $car =new Car("宝马",5000000); + $car->show(); + echo "
"; + + ``` + +2. 在上例的基础上为汽车类定义一个子类--跑车类。为子类实例化对象并访问父类的属性。 + + ```php + class Sportscar extends Car { + + function __construct($brand, $price) + { + parent::__construct($brand, $price); + } + + function show1(){ + echo ",它的颜色为白色。"; + } + + + + } + + $a = new Sportscar("比亚迪","200000"); + $a->show(); + $a->show1(); + echo "
"; + ``` + +3. 定义一个类,分别定义3个公共的属性和方法,3个受保护的属性和方法,3个私有属性和方法。 + + ```php + class a{ + + // 3个公共的属性和方法 + public $a; + public $b; + public $c; + public function a(){} + public function b(){} + public function c(){} + + // 3个受保护的属性和方法 + protected $a1; + protected $b1; + protected $c1; + protected function a1(){} + protected function b1(){} + protected function c1(){} + + + // 3个私有属性和方法 + private $a2; + private $b2; + private $c2; + private function a2(){} + private function b2(){} + private function c3(){} + + + } + + ``` + -- Gitee