• 周六. 7 月 27th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

public、protect、private在父类子类中使用

admin

11 月 28, 2021

先贴出一张,直观的、估计大家都见过的关于public、protect、private的范围图

作用域
当前类
同一package
子孙类
其他package
public
    T
         T
    T
     T
protect
    T
         T
    T
     F
private
    T
         F
    F
     F

                              T : true    F : false

现在我就挑一个测试和验证一下,其他的都是根据上表可以推出来
这三个中,我觉得private算是相对较为复杂的,所以就选private吧!
1、子类不能继承和(直接)访问父类的私有属性和方法,
            a,如果企图在子类中修改父类的私有属性 $this->variable = val ;
      

 1   <?php
 2         class test {
 3              private $variable = 1;
 4              public function setVal($param) {
 5                   $this->variable = $param;
 6              }
 7              public function getVal() {
 8                   return $this->variable;
 9              }
10              private function output() {
11                   echo 1;
12              }
13         }
14     class test2 extends test {
15          public function __construct(){
16           $this->variable =2;
17          }
18     }
19     $obj = new test2();
20     print_r($obj);
21     echo '<br />';
22     echo $obj->variable;
23     //$obj->output();
24     echo '<br />';
25     echo $obj->getVal();
26     echo '<br />';
27     $obj->setVal(3);
28     echo $obj->getVal();
29     echo '<br />';
30     print_r($obj);
31     }
32 ?>                                

输出:

   test2 Object ( [variable:test:private] => 1 [variable] => 2 ) 

  2

  1

  3

  test2 Object ( [variable:test:private] => 3 [variable] => 2 )

可以看到,私有属性不能直接修改和覆盖,如果这样写只是给子类定义了一个属性,程序不会报错,只有通过父类给出的接口方法来设置父类的私有属性。
            b,或者企图覆盖父类的的私有属性 private variable = val;
            程序会报错,例如:
 1  <?php
 2                        class test {
 3  private $variable = 1;
 4  public function setVal($param) {
 5   $this->variable = $param;
 6  }
 7  public function getVal() {
 8   return $this->variable;
 9  }
10  private function output() {
11   echo 1;
12  }
13 }
14 class test2 extends test {
15  public function __construct(){
16   //$this->variable =2;
17   private $variable = 2;
18  }
19 }
20 $obj = new test2();
21 print_r($obj);
22 echo '<br />';
23 echo $obj->variable;
24 //$obj->output();
25 echo '<br />';
26 echo $obj->getVal();
27 echo '<br />';
28 $obj->setVal(3);
29 echo $obj->getVal();
30 echo '<br />';
31 print_r($obj);
32                     ?>
   报错:
                    Parse error: syntax error, unexpected T_PRIVATE in D:WWWsmarty_3index.php on line 19
2、如果子类需要修改父类中的私有属性,必须在父类中提供修改的接口,也就是修改熟悉的公共方法
  

    <?php
                        class test {
 private $variable = 1;
 public function setVal($param) {
  $this->variable = $param;
 }
 public function getVal() {
  return $this->variable;
 }
 private function output() {
  echo 1;
 }
}
class test2 extends test {
 public function __construct(){
  $this->variable =2;
 }
}
$obj = new test2();
print_r($obj);

$obj->setVal(3);
echo $obj->getVal();
echo '<br />';
print_r($obj);
}
                    ?>

  上班零时整理,结果还华丽丽的被领导看到了,尴尬死我了,格式没太顾得上,哎、、、、

 
 
 
 
 
 

发表回复