public(类的外部、子类、内部) protected(类的外部、子类) private(类的内部

原创2019-01-16 18:40:43146
摘要:<?php/*** public(类的外部、子类、内部) protected(类的外部、子类)  private(类的内部)*/class Staff{ //public 公共,都可以访问 public $name; //protected  受保护的,外部不可以访问 protected $dept; //private:私有的,外部不可以访问 private $sala

<?php
/**
* public(类的外部、子类、内部) protected(类的外部、子类)  private(类的内部)
*/
class Staff
{
//public 公共,都可以访问
public $name;

//protected  受保护的,外部不可以访问
protected $dept;

//private:私有的,外部不可以访问
private $salary;

//构造方法
function __construct($name,$dept,$salary)
{
$this->name = $name;
$this->dept = $dept;
$this->salary = $salary;
}

//创建接口,通过外部来访问
public function getDept()
{
$res = $this->dept;
       if ($this->name == 'lisir')
        {
           $res = '保密';
       }
       return $res;
   }

   //给私有成员创建一个接口方法,外部无法查看
   public function getSalary()
   {
    $res =$this->salary;
    //通过姓名和部门来判断是否显示工资
    if($this->name== 'lisir' || $this->dept == '财务部' )
    {
    $res = '您无权查看!';
    }
    return $res;
   }
}

$staff = new Staff('lisir','财务部',8000);
// $staff = new Staff('lisir','开发部',8000);

echo '姓名:', $staff->name, '<br>';
// echo '部门是:', $staff->dept, '<br>';
echo '部门是:', $staff->getDept(), '<br>';
// echo '工资是:',$staff->salary,'<br>';

echo '工资是:',$staff->getSalary(),'<br>';

还需要好好消化,请老师审核作业,谢谢!

批改老师:天蓬老师批改时间:2019-01-17 09:03:53
老师总结:protected $dept; 外部不可以访问, 但是子类可以, 要注意

发布手记

热门词条