[PHP] php类的构造方法 →→→→→进入此内容的聊天室

来自 , 2021-03-02, 写在 PHP, 查看 140 次.
URL http://www.code666.cn/view/b1c00bcd
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  4. <title>构造方法</title>
  5. </head>
  6.  
  7. <body>
  8. <?php
  9. class MyObject{
  10.         public $object_name;                                                            //图书名称
  11.         public $object_price;                                                                   //图书价格
  12.         public $object_num;                                                                     //图书数量
  13.         public $object_agio;                                                                    //图书折扣
  14. /*  构造方法  */
  15. function __construct($name,$price,$num,$agio){                  //通过参数给成员变量赋值
  16.                 $this -> object_name = $name;
  17.                 $this -> object_price = $price;
  18.                 $this -> object_num = $num;
  19.                 $this -> object_agio = $agio;
  20.         }
  21. /*  *********  */
  22.         function setObjectName($name){                                          //声明方法setObjectName()
  23.                 $this -> object_name = $name;                                           //设置成员变量值
  24.         }
  25.         function getObjectName(){                                                       //声明方法getObjectName()
  26.                 return $this -> object_name;
  27.         }
  28. }
  29. $c_book = new MyObject('Western-style clothes',1500,5,8);               //实例化对象
  30. echo $c_book -> getObjectName();                                                        //调用方法getObjectName
  31. ?>
  32. </body>
  33. </html>
  34.  

回复 "php类的构造方法"

这儿你可以回复上面这条便签

captcha