<?php
class os{
public $a = 1; // 公有
private $b =2; // 私有
protected $c = 3; // 受保护
public function eat(){
echo "public func\n";
}
private function eat_1(){
echo "private func\n";
}
protected function eat_2(){
echo "protected func\n";
}
function get(){
echo $this->a,"\n",$this->b,"\n",$this->c,"\n";
$this->eat();
$this->eat_1();
$this->eat_2();
}
}
$g = new os();
$g->a;
$g->get();
//$g->b; // 不能访问私有属性
//$g->c; // 不能访问受保护属性
$g->eat();
//$g->eat_1();// 不能访问私有方法
//$g->eat_2();// 不能访问受保护方法
class os_1 extends os{
}
$f = new os_1();
$f->a;
$f->get();
//$g->b; // 不能访问私有属性
//$g->c; // 不能访问受保护属性
$g->eat();
//$g->eat_1();// 不能访问私有方法
//$g->eat_2();// 不能访问受保护方法
?>
php基础语法10--面向对象的访问控制
最新推荐文章于 2022-04-14 17:52:27 发布