面向对象 构造方法 初始化对象

来源:互联网 发布:社会工程学数据库 编辑:程序博客网 时间:2024/06/06 03:05
<?php  header("Content-Type: text/html; charset=utf-8");class person{  public $name;  public $age;  public $sex;  /*   * 构造方法 __construct() 是在实例化对象时被自动调用   * 用途:可以用于初始化程序(可以给成员属性赋值,也可以调用成员方法)   * 语法: [修饰符] function __construct(参数....){ 初始化流程 }   *    * 可以有默认值 $s = 男   */  public function __construct($n,$a,$s="男"){  $this -> name =$n;  $this -> age = $a;  $this -> sex = $s;  $this -> say();}  public function say(){  echo "名字:{$this -> name},年龄:{$this -> age},性别:{$this -> sex}";}}//实例化对象时 要按构造方法参数 去传对应的值$person = new person("玉",17,"女");echo $person -> name;echo $person -> age;echo $person -> sex;echo $person -> say();  ?>
原创粉丝点击