您当前的位置: 首页 >  php

暂无认证

  • 2浏览

    0关注

    94648博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

PHP面向对象篇 7 静态成员(系列篇)

发布时间:2022-01-30 01:01:07 ,浏览量:2

 public $name; public $sex; public $iq=10; const BIRTHPLACE='地球'; static public $counter=0;//静态属性,它是公共的资源和具体的实例没有关系 //构造函数 public function __construct($name,$sex){ self::$counter++; $this->name=$name; $this->sex=$sex; } protected function chew($food){ echo "
		

{$food}已经被咀嚼完成!

"; } public function eat($food){ $this->chew($food); echo "

{$this->name}正在吃{$food}!

"; } static public function hello(){ echo '

您好!我是来自'.self::BIRTHPLACE.'的人类

'; } } class Student extends Humanity { const BIRTHPLACE='火星'; public $studentId; public function test($subject){ echo "

{$this->name}正在考{$subject}!

"; } public function eat($food){ $this->chew($food); echo "

{$this->name}正在快速的吃{$food}!

"; } } new Humanity('',''); new Humanity('',''); new Humanity('',''); new Humanity('',''); new Humanity('',''); new Humanity('',''); echo Humanity::$counter; Humanity::hello();

在这里插入图片描述 解析: 注意:静态成员里面不能使用$this不然会报错 问题:子类怎么访问父类的方法变量常量:

parent:: 

怎么在类内部访问自己的方法变量常量:

self:: 
关注
打赏
1655516835
查看更多评论
立即登录/注册

微信扫码登录

0.0490s