首页 > Web开发 > 详细

PHP class 继承

时间:2014-01-25 08:46:23      阅读:467      评论:0      收藏:0      [点我收藏+]

1.执行结果

成员名

成员修饰符

方法名

方法修饰符

执行结果

name

private

setName

private

在构造函数函数中执行父类私有方法,子类未能覆盖private成员变量和方法,修改父类的私有变量

age

private

setAge

public

修饰符是public,子类方法覆盖父类方法,修改子类成员变量。但在父类构造其中执行$this->age只能取到父类的age

local

public

setLocal

public

子类的方法和成员变量均覆盖了父类对应方法和成员变量

hoby

public

setLocal

private

成员变量被覆盖,当是方法未覆盖

2.相关代码

ParentClass.php

bubuko.com,布布扣
<?php
class ParentClass {
    private $name;
    private $age;
    public $local;
    public $hoby;
    public function __construct() {
        print "execute parent construct()<br/>";
        $this->setName ();
        echo ("name=" . $this->name . "<br/>");
        
        $this->setAge ();
        echo ("age=" . $this->age . "<br/>");
        
        $this->setLocal ();
        echo ("local=" . $this->local . "<br/>");
        
        $this->setHoby ();
        echo ("hoby=" . $this->hoby . "<br/>");
        $this->__toString();
    }
    private function setName() {
        $this->name = "parentName";
        print "parent setName()\n ";
    }
    public function setAge() {
        $this->age = 50;
        print "parent setAge()\n";
    }
    public function setLocal() {
        $this->local = "parent local";
        print "parent setLocal()\n";
    }
    private function setHoby() {
        $this->hoby = "parent hoby";
        print "parent setHoby()\n";
    }
    
    public function __toString() {
        echo ("Parent[name=" . $this->name . ", age=" . $this->age . ", local=" . $this->local . ", hoby=" . $this->hoby . "]<br>");
    }
}

?>
bubuko.com,布布扣
ChildClass.php
bubuko.com,布布扣
<?php
require_once ‘ParentClass.php‘;
class ChildClass extends ParentClass {
    private $name;
    private $age;
    public $local;
    public $hoby;
    public function __construct() {
        print "execute child construct()<br/>";
        parent::__construct ();
        echo $this->__toString();
    }
    private function setName() {
        $this->name = "childName";
        print "child setName()\n";
    }
    public function setAge() {
        $this->age = 20;
        print "child setAge()\n";
    }
    public function setLocal() {
        $this->local = "child local";
        print "child setLocal()\n";
    }
    private function setHoby() {
        $this->hoby = "child hoby";
        print "child setHoby()\n";
    }
    public function __toString() {
        echo ("Child[name=" . $this->name . ", age=" . $this->age . ", local=" . $this->local . ", hoby=" . $this->hoby . "]<br/>");
    }
}

$child = new ChildClass ();
?>
bubuko.com,布布扣

3.执行输出

execute child construct()
execute parent construct()
parent setName() name=parentName
child setAge() age=
child setLocal() local=child local
parent setHoby() hoby=parent hoby
Child[name=, age=20, local=child local, hoby=parent hoby]
Child[name=, age=20, local=child local, hoby=parent hoby]

PHP class 继承

原文:http://www.cnblogs.com/i-bugs/p/3532643.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!