PHP面向对象编程(imooc)代码合集(二)

class04 类的继承

 

<?php
date_default_timezone_set("PRC");
/**
 * 继承
 * 1. 定义人类
 * 2. 让NbaPlayer继承人类
 * 3. PHP中类不允许同时继承多个父类,也就是extends后面只能跟一个父类名称,这个特性被称为PHP的单继承特性
 */
class Human{
    public $name;
    public $height;
    public $weight;
    public function eat($food){
        echo $this->name . "'s eating ". $food. "\n";
    }
}

// extends关键字用于说明该类继承自某个父类
class NbaPlayer extends Human
{
    // 类的属性的定义
    public $team="Bull";
    public $playerNumber="23";

    private $age="40"; // private 类型的属性不能被对象外部访问,但是可以在对象内部使用

    // 默认的构造函数,在对象被实例化的时候自动调用
    /*function __construct() {
       print "In NbaPlayer constructor\n";
    }*/

    // 构造函数通常用于初始化对象的属性值
    function __construct($name, $height, $weight, $team, $playerNumber) {
        print $name . ";" . $height . ";" . $weight . ";" . $team . ";" . $playerNumber."\n";
       $this->name = $name; // $this是php里面的伪变量,表示对象自身
       $this->height = $height; // 通过$this可以设置对象的属性值
       $this->weight = $weight;
       $this->team = $team;
       $this->playerNumber = $playerNumber;
    }

    // 析构函数,用于清理程序中使用的系统资源,比如释放打开的文件等等
    // 析构函数在该对象不会再被使用的情况下自动调用
    function __destruct() {
       print "Destroying " . $this->name . "\n";
    }

    // 类的方法的定义
    public function run() {
        echo "Running\n";
    }

    public function jump(){
        echo "Jumping\n";
    }
    public function dribble(){
        echo "Dribbling\n";
    }
    public function shoot(){
        echo "Shooting\n";
    }
    public function dunk(){
        echo "Dunking\n";
    }
    public function pass(){
        echo "Passing\n";
    }
}

/**
 * 1. 类实例化为对象时使用new关键字,new之后紧跟类的名称和一对括号。
 * 2. 使用对象可以像使用其他值一样进行赋值操作
 */
$jordan = new NbaPlayer("Jordan", "198cm", "98kg", "Bull", "23");
// 访问对象的属性使用的语法是->符号,后面跟着属性的名称
echo $jordan->name."\n";
// 调用对象的某个方法使用的语法是->符号,后面跟着方法的名称和一对括号
$jordan->run();
$jordan->pass();
$jordan->eat("apple"); // 只要是Human类的子类的对象,就可以调用eat方法

?>

 

 

class05

这一小节将会详尽介绍public、protected和private成员变量的访问规则;

简单来说,就是

 * 1. public的类成员可以被自身、子类和其他类访问
 * 2. protected的类成员只能被自身和子类访问
 * 3. private的类成员只能被自身访问
<?php
date_default_timezone_set("PRC");
/**
 * 访问控制
 * 1. public的类成员可以被自身、子类和其他类访问
 * 2. protected的类成员只能被自身和子类访问
 * 3. private的类成员只能被自身访问
 */
class Human{
    public $name;
    protected $height; // 只有自身和子类可以访问到
    public $weight;
    private $isHungry=true; // 只有自身可以访问到

    public function eat($food){
        echo $this->name . "'s eating ". $food. "\n";
    }

    public function info(){
        print "HUMAN: " . $this->name . ";" . $this->height . ";" . $this->weight . ";" . $this->isHungry ."\n";
    }
}

// extends关键字用于说明该类继承自某个父类
class NbaPlayer extends Human
{
    // 类的属性的定义
    public $team="Bull";
    public $playerNumber="23";

    private $age="40"; // private 类型的属性不能被对象外部访问,但是可以在对象内部使用

    // 构造函数通常用于初始化对象的属性值
    function __construct($name, $height, $weight, $team, $playerNumber) {
       print $name . ";" . $height . ";" . $weight . ";" . $team . ";" . $playerNumber."\n";
       $this->name = $name; // $this是php里面的伪变量,表示对象自身
       $this->height = $height; // 通过$this可以设置对象的属性值
       $this->weight = $weight;
       $this->team = $team;
       $this->playerNumber = $playerNumber;
       echo $this->isHungry."\n";
    }
    // 析构函数,用于清理程序中使用的系统资源,比如释放打开的文件等等
    // 析构函数在该对象不会再被使用的情况下自动调用
    function __destruct() {
       print "Destroying " . $this->name . "\n";
    }

    // 类的方法的定义
    public function run() {
        echo "Running\n";
    }

    public function jump(){
        echo "Jumping\n";
    }
    public function dribble(){
        echo "Dribbling\n";
    }
    public function shoot(){
        echo "Shooting\n";
    }
    public function dunk(){
        echo "Dunking\n";
    }
    public function pass(){
        echo "Passing\n";
    }
    public function getAge(){
        echo $this->name . "'s age is " . ($this->age - 2) . "\n";
    }
}

$jordan = new NbaPlayer("Jordan", "198cm", "98kg", "Bull", "23");
// step1, 证明public和protected的属性可以被子类和自身访问
$jordan->info();

// step2,证明public的属性可以被其他类访问
echo $jordan->getAge()."\n";

// step3,证明private的属性只能被自身访问
// 把这行代码添加到构造函数试试
echo $this->isHungry."\n";
?>

 

class06

static(成员)用于保存类的公有数据,类内部,可以通过self::  或 static::关键字访问自身的静态成员;调用父类的静态成员可以通过parent::关键字调用,这里的::符号,是对类的成员直接调用,相比之下,访对象的属性,用的是->符号
<?php
date_default_timezone_set("PRC");
/**
 * 静态成员
 * 1. 静态属性用于保存类的公有数据
 * 2. 静态方法里面只能访问静态属性
 * 3. 静态成员不需要实例化对象就可以访问
 * 4. 类内部,可以通过self或者static关键字访问自身的静态成员
 * 5. 可以通过parent关键字访问父类的静态成员
 * 6. 可以通过类名称在外部访问类的静态成员
 */
class Human{
    public $name;
    protected $height; // 只有自身和子类可以访问到
    public $weight;
    private $isHungry=true; // 只有自身可以访问到

    public static $sValue="Static Value in Human";

    public function eat($food){
        echo $this->name . "'s eating ". $food. "\n";
    }

    public function info(){
        print "HUMAN: " . $this->name . ";" . $this->height . ";" . $this->weight . ";" . $this->isHungry ."\n";
    }
}

// extends关键字用于说明该类继承自某个父类
class NbaPlayer extends Human
{
    // 类的属性的定义
    public $team="Bull";
    public $playerNumber="23";

    private $age="40"; // private 类型的属性不能被对象外部访问,但是可以在对象内部使用

    public static $president="David Stern";

    public static function changePresident($newPrsdt){
        static::$president = $newPrsdt; // self用于表示当前类,"::"操作符用于访问类的静态成员
        // static关键字也可以用于访问当前类的静态成员
        // echo $this->age . "\n"; // 不能在静态方法中使用this伪变量,也不能用对象的->方式调用静态成员
        echo parent::$sValue . "\n"; // parent用于表示父类,可以用于访问父类的静态成员
    }

    // 构造函数通常用于初始化对象的属性值
    function __construct($name, $height, $weight, $team, $playerNumber) {
       print $name . ";" . $height . ";" . $weight . ";" . $team . ";" . $playerNumber."\n";
       $this->name = $name; // $this是php里面的伪变量,表示对象自身
       $this->height = $height; // 通过$this可以设置对象的属性值
       $this->weight = $weight;
       $this->team = $team;
       $this->playerNumber = $playerNumber;
       echo $this->isHungry."\n";
    }
    // 析构函数,用于清理程序中使用的系统资源,比如释放打开的文件等等
    // 析构函数在该对象不会再被使用的情况下自动调用
    function __destruct() {
       print "Destroying " . $this->name . "\n";
    }

    // 类的方法的定义
    public function run() {
        echo "Running\n";
    }

    public function jump(){
        echo "Jumping\n";
    }
    public function dribble(){
        echo "Dribbling\n";
    }
    public function shoot(){
        echo "Shooting\n";
    }
    public function dunk(){
        echo "Dunking\n";
    }
    public function pass(){
        echo "Passing\n";
    }
    public function getAge(){
        echo $this->name . "'s age is " . ($this->age - 2) . "\n";
    }
}

// 类名加“::”可以访问类的静态成员
// 静态成员不需要实例化就可以访问
echo "The president is ". NbaPlayer::$president. "\n";
NbaPlayer::changePresident("Adam Silver");
echo "The president is changed to ". NbaPlayer::$president. "\n";
?>

 

posted @ 2015-12-05 09:14  constructora  阅读(165)  评论(0编辑  收藏  举报