Blogger Information
Blog 36
fans 0
comment 1
visits 28100
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
父类与子类,
其琛的博客
Original
854 people have browsed it

引用代码:

<?php
spl_autoload_register(function($className){
    require './'.$className.'.php'; 
});
$two = new two('张三','18', '男',['打游戏','学习'],'学生');
$two = new two('李四','18', '男',['看电视','打球'],'学生');
echo '姓名: '.$two->names.'<br>'; 
echo '年龄: '.$two->sex.'<br>'; 
echo '性别: '.$two->age.'<br>';
echo '爱好:'.$two->grade.'<br>';
echo '工作:'.$two->room.'<br>';

echo $two->shenfen().'<br>'; 
echo $two->game().'<br>';

父类代码

<?php
class one
{
    protected $names;
    protected $sex;
    protected $age;
    public function __construct($names,$sex,$age)
    {
        $this->names = $name;
        $this->sex = $sex;
        $this->age = $age;
    }
    
    public function shenfen()
    {
        return '好学生';
    }
}

子类代码

<?php
class two extends one
{
    public function __get($name)
    {
        return $this->$name;
    }
    private $grade = true;
    private $rome = true; 
    public function __construct($names,$sex,$age,$grade,$rome)
    {
        parent::__construct($names,$sex,$age);
        
        $this->grade = $grade;
        $this->rome = $rome;
    }
    public function game()
    {
        return '玩游戏';
    }
    public function shenfen()
    {
        return parent::shenfen().',热爱学习';
    }
    
    
    
}

了解了子类与父类的关系,子类的功能是用来扩展或重载父类的某些功能,对父类属性进行扩展,增加新的特征

Correction status:Uncorrected

Teacher's comments:
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