码迷,mamicode.com
首页 > Web开发 > 详细

PHP面向对象程序设计之继承性

时间:2017-05-30 19:32:02      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:var_dump   种类   logs   程序   访问   bsp   const   extends   php   

一、类继承的应用
<?php

class A {
    public $name = "zhangsan";
    public $age = 20;

    public function say() {
        return $this -> name;
    }
}

class B extends A {
}

class C extends A {
}

$b = new B ();
var_dump ( $b );
echo $b -> say ();
echo "<hr />";

$c = new C ();
var_dump ( $c );
echo $c -> say ();
?>

<?php

class person{
    public $name;
    public $age;
    public $sex;
    
    public function __construct($name,$age,$sex){
        $this -> name = $name;
        $this -> age = $age;
        $this -> sex = $sex;
    }
    
    public function say(){
        echo "say...";
    }
    
    public function eat(){
        echo "eat...";
    }
    
    public function run(){
        echo "run...";
    }
}

class teacher extends person{
    public function teach(){
        echo "teach...";
    }
}

class student extends person{
    public function learn(){
        echo "learn...";
    }
}

$teacher = new teacher("zhangsan",30,‘nan‘);
$teacher -> say();
$teacher -> teach();
echo "<hr>";

$student = new student("lisi",18,‘nv‘);
$student -> run();
$student -> learn();
?>


二、访问类型的控制
<?php
//访问类的控制的三种类型
class person {
    public $name;
    private $age;
    protected $sex;

    public function __construct($name, $age, $sex) {
        $this -> name = $name;
        $this -> age = $age;
        $this -> sex = $sex;
    }

    public function p1() {
        echo "p1";
    }

    private function p2() {
        echo "p2";
    }

    protected function p3() {
        echo "p3";
    }
    
    //1.在类中调用
    public function test1(){
        echo $this -> name;
        echo $this -> age;
        echo $this -> sex;
    }
}

//2.在子类中调用
class student extends person {

    public function test() {
        echo $this -> name;
        echo $this -> age;
        echo $this -> sex;
    }
}

//3.在类外调用
$person = new person ( "zhangsan", 18, ‘nan‘ );
echo $person -> name;
echo $person -> age;
echo $person -> sex;
?>

三、子类中重载父类的方法
<?php

class person{
    public $name;
    public $age;
    public $sex;
    
    public function __construct($name,$age,$sex){
        $this -> name = $name;
        $this -> age = $age;
        $this -> sex = $sex;
    }
    
    public function say(){
        echo "My name is {$this -> name}, my age is {$this -> age}, my sex is {$this -> sex}";
    }
}

class teacher extends person{
    public $teach;
    
    public function __construct($name,$age,$sex,$teach){
//         $this -> name = $name;
//         $this -> age = $age;
//         $this -> sex = $sex;
        parent::__construct($name,$age,$sex); //这句代码代替上面的三句
        $this -> teach = $teach;
    }
    
    //声明一个同名的方法,就可以重写
    public function say(){
        parent::say();
        echo ", my teach is {$this -> teach}";
        
    }
}

class student extends person{
    public $school;
    
    public function __construct($name,$age,$sex,$school){
        $this -> name = $name;
        $this -> age = $age;
        $this -> sex = $sex;
        $this -> school = $school;
    }
    
    public function say(){
        //echo "My name is {$this -> name}, my age is {$this -> age}, my sex is {$this -> sex}, my school is {$this -> school}";
        
        parent::say();
        echo ", my school is {$this -> school}";
    }    
}

$teacher = new teacher(‘zhangsan‘,30,‘nan‘,‘shuxue‘);
$teacher -> say();

echo "<hr>";
$student = new student(‘lisi‘,18,‘nv‘,‘beida‘);
$student -> say();
?>

 

PHP面向对象程序设计之继承性

标签:var_dump   种类   logs   程序   访问   bsp   const   extends   php   

原文地址:http://www.cnblogs.com/htmlphp/p/6920740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!