从子构造函数调用父构造函数具有“ this关键字”

时间:2019-05-02 17:16:20

标签: php oop

可以解释发生此行时此代码中发生的情况   $ b-> printInfo(); 父级拥有“此关键字”,且“名称为私有,则不会继承” $ name”

<?php
  class A {
    private $Name;
    static $count1 = 0;

    function A($N) {
      $this->name = $N;
      SELF::$count1++;
    }
    function printInfo() {
      echo "My name is " . $this->Name . "<br>";
    }
  }

  class B extends A {
    private $Id;
    static $count2 = 0;
    function B($n, $i) {
      $this->Id = $i;
      parent::A($n);
      SELF::$count2++;
    }
    function printInfo() {
      parent::printInfo();
      echo "My ID is " . $this->Id . "<br>";
    }
  }

  $a = new A("Khalid");
  $b = new B("Sameer", 20161003);
  $b->printInfo();
  echo B::$count1;
  echo '<br>';
  echo $b::$count2;
 ?>

0 个答案:

没有答案
相关问题