Blogger Information
Blog 15
fans 0
comment 0
visits 8397
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
补0812:类
我们的关系如此狭窄
Original
613 people have browsed it
/*
 * oop基础:请举例实例演绎以下难点
 * 1. 类(对象抽象化的结果)与对象 (类实例化结果)
 * 2. 构造方法
 * 3. 对象成员之间的内部访问 $this
 * 4. private仅限本类中使用 protected本类中,子类中使用
 * 5. 类的自动加载 spl_autoload_register
 * 6. 静态成员的访问 类的引用self::
 * 7. 类的继承 扩展 父类方法(魔术方法,普通方法)的重写 parent:: 调用父类成员
 */
class B{
    //public  protected  private   修饰符  分别是公用的  受保护的  保密的
    public $name;
    public $age;
    public $height;
    public $like;
    public $simple = 'test';
    private $weight = '55';
    public static $num = '123456';
    protected $tes = 'www';
    public $color ='red';
    //构造方法
    public function __construct()
    {
        $this->weight='65';
    }

    public function make_(){
        //对象成员之间的内部访问 $this
        echo  '名字:'.$this->name.'<br>';
        echo  '年龄:'.$this->age.'<br>';
        echo  '身高:'.$this->height.'<br>';
        echo  '喜好:'.$this->like.'<br>';
        echo  '体重:'.$this->weight.'<br>';
        echo  '幸运色:'.$this->color.'<br>';
    }
    public function echo_weight(){
        echo $this->weight; //继承的类不能调用  报错
    }
    // 静态成员的访问 类的引用self::
    public function echo_static()
    {
        echo self::$num;
        echo "<br>";
    }


}


//类(对象抽象化的结果)与对象 (类实例化结果)
$person = new B;
//类的继承
class C extends B{
    public function test_weight(){

        echo $this->weight; //继承的类调不出来类属性

    }
    public function test_pro(){
        echo $this->tes; //继承的类不能调用  报错
    }
    //类的扩展
    public function __construct()
    {
        parent::__construct();
        $this->color = 'blue';
    }
    public function echo_weight()
    {
        echo 'wwwwwwwwwwwwwwwwwwwwwwwww'; //类方法重写覆盖  类原先输出的是weight  这边重写输出字符串wwwwwwwwwwwwwwwwwwwwwwwww
        echo "<br>";
    }

}



$person->echo_weight();//用类本身调用   65

$C =new C;
$C->test_weight(); //继承的类不能调用  报错
echo "<br>";
$C->test_pro(); //继承的类不能调用  报错
echo "<br>";
// 静态成员的访问 类的引用self::
echo $person::$num;
echo "<br>";
$person->echo_static();
echo "<br>";

$person->name = 'ych';
$person->height = '165';
$person->age = '26';
$person->like = 'play';
$person->make_();
echo "____________________________________";
//类的继承 C类本身没有make_方法 继承于B类
$C->make_();

echo $person->simple;
echo "<br>";
$C->echo_weight();
 //类的自动加载 spl_autoload_register
<?php


class A
{
    public function ec_(){
        echo '类A';
    }

}
<?php
function auto_($class){
    $file = $class.'.php';
    if(is_file($file)){
        require_once($file);
    }
}
//类的自动加载 spl_autoload_register
spl_autoload_register('auto_');
$obj = new A;
$obj->ec_();


Correcting teacher:PHPzPHPz

Correction status:qualified

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