PHP面向对象篇6 范围解析操作符::(系列篇)
        【摘要】 
                    
                        
                    
                    <?php
header('Content-Type:text/html;charset=utf-8');
class Humanity {
	public $name;
	public $sex;...
    
    
    
    <?php
header('Content-Type:text/html;charset=utf-8');
class Humanity {
	public $name;
	public $sex;
	public $iq=10;
	const BIRTHPLACE='地球';
	//构造函数
	public function __construct($name,$sex){
		$this->name=$name;
		$this->sex=$sex;
	}
	protected function chew($food){
		echo "<p>{$food}已经被咀嚼完成!</p>";
	}
	public function eat($food){
		$this->chew($food);
		echo "<p>{$this->name}正在吃{$food}!</p>";
	}
	public function hello(){
		echo '<p>您好!我是来自'.self::BIRTHPLACE.'的人类</p>';
	}
}
class Student extends Humanity {
	const BIRTHPLACE='火星';
	public $studentId;
	public function test($subject){
		echo "<p>{$this->name}正在考{$subject}!</p>";
	}
	public function eat($food){
		$this->chew($food);
		echo "<p>{$this->name}正在快速的吃{$food}!</p>";
	}
	public function hello(){
		parent::chew('铅笔');//没有必要通过这种方式来访问,因为他以及被你继承过来了!
		parent::hello();
		echo '<p>您好!我是来自'.self::BIRTHPLACE.'人类中的学生</p>';
	}
}
echo '<p>'.Humanity::BIRTHPLACE.'</p>';
echo '<p>'.Student::BIRTHPLACE.'</p>';
//$hanMM=new Humanity('韩梅梅','女');
//$hanMM->hello();
$liLei=new Student('李雷','男');
$liLei->hello();
echo $liLei::BIRTHPLACE;//可以,但是我们极度不建议你通过具体的实例去访问 类常量
解析:
 **范围解析操作符::
 作用:
 一、访问类里面的静态成员
二、访问类里面的常量
 类内部如果访问?
 self::类常量名称
	parent::类常量名称
类外部如果访问?
	类名称::类常量名称
	注意点:需要访问那个类里面的常量就用哪个类的名称就可以了
三、在子类里面访问父类中的方法(被重写了的方法)
 parent::方法
 我们没有必要在子类里面通过parent去访问父类中 没有被 重写 的方法!
 注意:类常量可以在子类中重写去定义、但是不能直接修改其值!**
 效果:
 
文章来源: blog.csdn.net,作者:贵哥的编程之路(热爱分享),版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_37805832/article/details/122749648
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)