• About_类与对象03


    php中的static

    1:属于静态变量:;

    2:是全部类的属性;

    3:调用静态变量要用::(两个冒号)。

    eg:1

     1 <html>
     2 <head>
     3     <title>static测试</title>
     4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
     5 </head>
     6 <body>
     7 <?PHP
     8 class Person{
     9 
    10     static $type = "人物";
    11     public $name = "张三";
    12     public $age = 18;
    13 
    14     public function intro(){
    15         echo Person::$type."<br/>".$this->name."<br/>".$this->age."<br/>";
    16     }
    17 }
    18 
    19 $p1 = new Person();
    20 
    21 $p1->intro();
    22 ?>
    23 </body>
    24 </html>

    eg:2

     1 <html>
     2 <head>
     3     <title>static测试</title>
     4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
     5 </head>
     6 <body>
     7 <?PHP
     8 class Person{
     9 
    10     static $type = "人物";
    11     public $name = "张三";
    12     public $age = 18;
    13 
    14     public function intro(){
    15         echo Person::$type."<br/>".$this->name."<br/>".$this->age."<br/>";
    16     }
    17 }
    18 
    19 $p1 = new Person();
    20 Person::$type = "张三不是人!";
    21 
    22 $p1->intro();
    23 ?>
    24 </body>
    25 </html>

    eg:3

    class类的继承

     1 <html>
     2 <head>
     3     <title>class类的继承</title>
     4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
     5 </head>
     6 <body>
     7 <?PHP
     8 class Person{
     9 
    10     static $type = "人物";
    11     public $name = "张三";
    12     public $age = 18;
    13 
    14     public function intro_Person(){
    15         echo $this->name."<br/>".$this->age."<br/>";
    16     }
    17 }
    18 
    19 class Teacher extends Person{
    20 
    21     public function intro_Teacher(){
    22         echo $this->age;
    23     }
    24 
    25 }
    26 
    27 class Students extends Teacher{
    28 
    29     public function intro_Students(){
    30         echo $this->age;
    31     }
    32 
    33 }
    34 
    35 $p1 = new Students();
    36 
    37 $p1->intro_Students();
    38 ?>
    39 </body>
    40 </html>
  • 相关阅读:
    html5学习笔记2——新元素
    html5学习笔记——基础
    html学习笔记之2——多媒体
    Python调试打印错误信息
    Python随机字符串验证码
    js传递数组
    js上传图片并预览
    JS获取当前日期、比较日期大小
    nrm管理npm源
    使用Git Subtree在多个项目中共用同一个子项目
  • 原文地址:https://www.cnblogs.com/a-moemiss/p/3746627.html
Copyright © 2020-2023  润新知