diff --git "a/\346\242\201\346\265\251/20220517-PHP\351\235\242\345\220\221\345\257\271\350\261\241.md" "b/\346\242\201\346\265\251/20220517-PHP\351\235\242\345\220\221\345\257\271\350\261\241.md" new file mode 100644 index 0000000000000000000000000000000000000000..642384218bd9c5aaf39d63c2da0c88e986422d54 --- /dev/null +++ "b/\346\242\201\346\265\251/20220517-PHP\351\235\242\345\220\221\345\257\271\350\261\241.md" @@ -0,0 +1,267 @@ +# PHP面向对象 + +属性区别 + +```php +class Animal{ + function eat(){} + + function play(){} + +} + + +class Cat extends Animal{ + +//加个var +var $name; +var $age; +//或 +public $name; +//或 +private $age; + +//静态属性,属于类,不属于对象,全对象都可直接用。 +static $aihao ="吃鱼" +//加修饰符来区分变量和属性 + + } +``` + +方法区别 + +```php +PHP不支持方法重载 + +function eat(){ + echo $this->name.$this->age;//重写父类方法 +} +//“->”表示调用对象 + + +function play(){ + parent->play();//调用父类 + self::sleep;//调用本类 +} + + +static function sleep(){ + +} + + + + + +//静态方法 +//静态只能用静态的,下面的name和age不是静态的,得改成静态 +static function eat(){ + echo "姓名叫".Cat::name."年龄是".Cat::age; +} + +//静态方法调用 + Cat::eat(); + +//“::”表示静态的调用 +``` + +**get、set封装** + +```php +public function getAge(){ + return $this -> age; +} + +public function setAge(){ + $this -> age = $age; +} +``` + +**构造方法** + +```php +PHP不支持方法重载,不能同时写无参和有参构造器,只能二选一 +//魔术方法,php的构造器 +function __construct($name,$age){ + $this -> name = $name; + $this -> age = $age; +} +//析构方法,当对象被销毁时执行 +function __destruct(){ + +} + + +//***销毁 +unset(<对象或者变量>); + +//***检测变量还活着没 +isset(<对象或变量>); +``` + + + +PHP创建一个对象 + +```php +$a = new Cat();//无参新建对象 + +$a -> name="小白";//赋值 +$a -> setAge(20);//用set赋值年龄 + +$b = new Cat(值1,值2,值3);//有参 + +$a -> eat();//调用方法 + +echo $a ->name.$a->getAge();//用get输出年龄 +``` + +**多态** + +```php +class Animal{ + function say() {} + +} + +class newcat extends Animal{ + function say(){ + echo "喵喵" + } +} + +class dog extends Animal{ + function say(){ + echo "汪汪"; + } +} + +//依靠方法来判断对象是什么类,来调用方法 +function work($a){ + if($a instanceof animal){ + $a->say(); + else{ + echo " 不是动物"; + + } + } +} +work(new dog);//调用方法来判断类 +``` + +# 作业 + +```php +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->Brand."她的价格是".$this->price."。"; + } + + function run(){//方法 + echo "能跑"; + } +} +class SuperCar extends Car +{ + function run(){ + echo "能跑的像飞一样快"; + } + +} +$a = new SuperCar(); +$a -> setBrand("科尼赛格"); +$a -> setPrice(2300000); +$a ->show(); +$a ->run(); +``` + +```php +//定义一个类,分别定义3个公共的属性和方法,3个受保护的属性和方法,三个私有属性和方法 + +class test{ + //公共 + public $name; + public $age; + public $sex; + + public function show(){ + echo "介绍"; + } + public function run(){ + echo "能跑"; + } + public function eat(){ + echo "能吃"; + } + //受保护 + protected $nname; + protected $aage; + protected $ssex; + + protected function sshow(){ + echo "介绍"; + } + protected function rrun(){ + echo "能跑"; + } + protected function eeat(){ + echo "能吃"; + } + //私有 + private $snname; + private $saage; + private $sssex; + + private function rsshow(){ + echo "介绍"; + } + private function rrrun(){ + echo "能跑"; + } + private function eeeat(){ + echo "能吃"; + } +} +``` +