zoukankan      html  css  js  c++  java
  • php类与对象综合案例

    <?php
    //类常量的定义与访问
    class Stu
    {
        private $name;
        private $sex;
        private $age;
    
        private static $count = 0;
        const COUNTY = '我们共同的家园---中国';
    
        public function __construct($name2,$sex2,$age2)
        {
            $this->name = $name2;
            $this->sex = $sex2;
            $this->age = $age2;
            self::$count++;//总数加加
           $this->info();
        }
        public function info()
        {
            $str = '<h2>'.self::COUNTY.'</h2>';
            $str .= "我的姓名是".$this->name.'<br>';
            $str .= "我的性别是".$this->sex.'<br>';
            $str .= "我的年龄是".$this->age.'<br>';
            $str .= $this->name.'加入中国,总共有'.self::$count.'个人';
            echo $str;
        }
        public function __destruct()
        {
            self::$count--;//总数减减
        }
    }
    
    $obj = new Stu('明哥哥','男',26);
    $obj2 = new Stu('明哥哥2','男',26);
    unset($obj2);
    $obj3 = new Stu('明哥哥3','男',26); $obj4 = new Stu('明哥哥4','男',26);

  • 相关阅读:
    Go
    Go
    Go
    Go
    Go
    Go
    爬虫常用相关库
    Go
    python基础第7天(day19)
    python基础第五天(day17)元组,集合,字符串操作 字符编码:
  • 原文地址:https://www.cnblogs.com/xm666/p/11255391.html
Copyright © 2011-2022 走看看