zoukankan      html  css  js  c++  java
  • php子类中如何调用父类中的变量和方法

    <?php
    class A{
        public $a1='a1';
        protected $a2='a2';
        function test(){
               echo "hello!<hr/>";
        }
    }
    class B extends A{//若A类和B类不在同一文件中 请包含后(include)再操作
        public $a1='b1';
        function test2(){
                $this->test();
                  parent::test();//子类调用父类方法
        }
        function test()
        {   
            echo $this->a1.',';
            echo $this->a2.',';
            echo "b2_test_hello<hr/>";
        }
    }
    $a = new B();
    $a->test();//b1,a2,b2_test_hello
    $a->test2();//b1,a2,b2_test_hello//hello!


    方法的调用:$this->方法名();如果子类中有该方法则调用的是子类中的方法,若没有则是调用父类中的

              parent::则始终调用的是父类中的方法。

    变量的调用:$this->变量名;如果子类中有该变量则调用的是子类中的,若没有则调用的是父类中的

  • 相关阅读:
    NO.2
    【转载】初始化顺序
    Java中的容器
    primer看完了
    NO.1
    转 Python爬虫入门二之爬虫基础了解
    转 Python爬虫入门一之综述
    hdu 5691 Sitting in Line
    51nod 1043 幸运号码
    51nod 1624 取余最长路
  • 原文地址:https://www.cnblogs.com/liangdong/p/10373423.html
Copyright © 2011-2022 走看看