面向对象 访问类型的控制

来源:互联网 发布:淘宝图片空间在哪里 编辑:程序博客网 时间:2024/05/16 12:10
<?phpheader("Content-Type: text/html; charset=utf-8");class person{public $name;private $age;protected $sex;public function __construct($name,$age,$sex){$this -> name = $name;$this -> age = $age;$this -> sex = $sex;}public function p1(){echo 'p1';}private function p2(){echo 'p2';}protected function p3(){echo "p3";}/* * 内部访问 *  * 公有的、私有的、受保护的成员方法或属性可以在内部直接访问 */public function text1(){echo $this -> name;echo $this -> age; echo $this -> sex;$this -> p1();$this -> p2(); $this -> p3();}}class student extends person{/* * 继承 *  * 公有的、受保护的成员方法或属性可以在外部直接访问 * * 私有的成员方法或属性不可以在外部直接访问 */public function text(){echo $this -> name;//echo $this -> age; //不能输出echo $this -> sex;$this -> p1();//$this -> p2(); //不能输出$this -> p3();}}$person = new person('娜美',20,'女');/* * 公有的成员方法或属性可以在外部直接访问 *  * 私有的、受保护的成员方法或属性不可以在外部直接访问 */echo $person -> name;$person -> p1();$person -> text1();echo "<hr/>";$student = new student('路飞',19,'男');$student -> text();?>

原创粉丝点击