Maison > développement back-end > tutoriel php > 面向对象PHP(一)

面向对象PHP(一)

WBOY
Libérer: 2016-07-29 09:16:20
original
1157 Les gens l'ont consulté

<code><span><span><span><?php </span>
/**
*   继承,访问控制,static(静态)关键字、重写、Final关键字、数据访问补充、接口、多态、抽象类
*/</span><span>/**
*   1.静态属性用于保存类的公有数据
*   2.静态方法里面只能访问静态属性
*   3.静态成员不需要实例化对象就可以访问
*   4.类的内部可以通过self或者static关键字访问自身静态变量
*   5.可以通过parent关键字访问父类的静态成员
*   6.可以通过类的名称在类的定义外部访问静态成员
*/</span>
header(<span>"Content-Type: text/html; charset=utf-8"</span>);<span>//去除中文乱码</span>
date_default_timezone_set(<span>"PRC"</span>);<span>//设置中国时区</span><span>/**
    * Human类定义
    */</span><span><span>class</span><span>Human</span>    {</span><span>public</span><span>$name</span>;
        <span>protected</span><span>$height</span>;<span>//本身和子类可访问</span><span>public</span><span>$weight</span>;

        <span>public</span><span>static</span><span>$staticValue</span>=<span>"我是Human类里的static成员。"</span>;

        <span>public</span><span><span>function</span><span>eat</span><span>(<span>$food</span>)</span>{</span><span>echo</span><span>$this</span>->name.<span>" is eating "</span>.<span>$food</span>.<span>"<br>"</span>;
        }
    }
    <span>/**
    * NbaPlayer类定义
    */</span><span><span>class</span><span>NbaPlayer</span><span>extends</span><span>Human</span>{</span><span>//extends:表示继承,php中extends只能跟一个类的类名(单继承原则)</span><span>//属性</span><span>public</span><span>$team</span>=<span>"PTS"</span>;
        <span>public</span><span>$playerNum</span>=<span>"1221"</span>;
        <span>private</span><span>$age</span>= <span>12</span>;

        <span>//静态属性定义</span><span>public</span><span>static</span><span>$president</span>=<span>"David"</span>;

        <span>//静态方法定义</span><span>public</span><span>static</span><span><span>function</span><span>changePresident</span><span>(<span>$newPresident</span>)</span>{</span><span>static</span>::<span>$president</span>=<span>$newPresident</span>;
            <span>//self::$president=$newPresident;//在类定义中使用静态成员,用static或self::静态成员变量</span><span>//使用parent关键字访问父类的静态成员</span><span>echo</span><span>parent</span>::<span>$staticValue</span>.<span>"<br>"</span>;
        }

        <span>//构造函数</span><span><span>function</span><span>__construct</span><span>(<span>$name</span>,<span>$weight</span>,<span>$team</span>,<span>$playerNum</span>)</span>{</span><span>echo</span><span>"执行构造函数...<br>"</span>;
            <span>$this</span>->name=<span>$name</span>;<span>//this是php里面的伪变量,自身</span><span>$this</span>->weight=<span>$weight</span>;
            <span>$this</span>->team=<span>$team</span>;
            <span>$this</span>->playerNum=<span>$playerNum</span>;
        }

        <span>//析构函数</span><span><span>function</span><span>__destruct</span><span>()</span>{</span><span>echo</span><span>"执行析构函数...<br>"</span>;;
        }

        <span>//定义方法</span><span>public</span><span><span>function</span><span>run</span><span>()</span>{</span><span>echo</span><span>"Runing...\n"</span>;
        }
        <span>public</span><span><span>function</span><span>jump</span><span>()</span>{</span><span>echo</span><span>"Jumping...\n"</span>;
        }
        <span>public</span><span><span>function</span><span>dribble</span><span>()</span>{</span><span>echo</span><span>"Dribbling...\n"</span>;
        }
        <span>public</span><span><span>function</span><span>shoot</span><span>()</span>{</span><span>echo</span><span>"Shooting...\n"</span>;
        }
        <span>public</span><span><span>function</span><span>dunk</span><span>()</span>{</span><span>echo</span><span>"Dunking...\n"</span>;
        }
        <span>public</span><span><span>function</span><span>pass</span><span>()</span>{</span><span>echo</span><span>"Passing...\n"</span>;
        }

        <span>public</span><span><span>function</span><span>getAge</span><span>()</span>{</span><span>echo</span><span>$this</span>->name.<span>"今年 "</span>.<span>$this</span>->age.<span>" 岁了。<br>"</span>;
        }
    }
    <span>//类的实例化</span><span>$pzy</span>=<span>new</span> NbaPlayer(<span>"彭中耀"</span>,<span>"182cm"</span>,<span>"75kg"</span>,<span>"PTS"</span>,<span>"1221"</span>);
    <span>$pts</span>=<span>new</span> NbaPlayer(<span>"彭小耀"</span>,<span>"128cm"</span>,<span>"15kg"</span>,<span>"PTS"</span>,<span>"1221"</span>);
    <span>echo</span><span>$pzy</span>->name.<span>"\n"</span>;
    <span>echo</span><span>$pzy</span>->eat(<span>"大西瓜"</span>);
    <span>//echo $pzy->age;</span><span>$pzy</span>->getAge();

    <span>// echo $pzy->name."的联盟总裁是".$pzy->president;//x不能访问静态成员</span><span>// echo $pts->name."的联盟总裁是".$pts->president;</span><span>//类定义外访问静态成员:类名::静态成员变量</span><span>echo</span><span>"前任是"</span>.NbaPlayer::<span>$president</span>.<span>"<br>"</span>;
    NbaPlayer::changePresident(<span>"彭大耀"</span>);
    <span>echo</span><span>"现任是"</span>.NbaPlayer::<span>$president</span>.<span>"<br>"</span>;
<span>?></span></span></span></code>
Copier après la connexion

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了面向对象PHP(一),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal