Blogger Information
Blog 28
fans 1
comment 0
visits 17296
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
2019.6.14作业
关超的博客
Original
690 people have browsed it

<?php

// 关键词: 访问控制符, public, protected, private

// 1. public: 默认, 类内,类外,子类都可见
// 2. protected: 类内, 子类可见, 类外不可见
// 3. private: 类内可见, 子类, 类外不可见

class Demo1{
   public $name ;
   protected $dept;
   private $salary;

   public function __construct($name,$dept,$salary)
   {
       $this->name=$name;
       $this->dept=$dept;
       $salary->salary=$salary;
   }


   public function getDept(){
       return $this->dept ==='技术部'?$this->dept:'无权查看';
   }

   public function getSalary(){
       return $this->dept==='财务部'?$this->salary:'无权查看';
   }
}


$obj = new Demo1('超', '技术部', 9999);
$obj1 = new Demo1('高', '财务部', 6666);

echo $obj->name, '<br>';
echo $obj->dept, '<br>';//外部不能访问
echo $obj->salary,'<br>';//外部不能访问

echo $obj->getPosition(), '<br>';
echo $obj1->getPosition(), '<br>';

echo $obj->getSalary(), '<br>';
echo $obj1->getSalary(), '<br>';


class Demo2 extends Demo1{
   public function salary(){
       return $this->salary;
   }
}

$obj3 = new Demo2('超','技术部',8888);
echo $obj3->salary();//子类不允许访问private变量

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post