php面向对象相关技术

step1 一个经典类的设计和实例化

<?php
class mycoach
{
  public $_name='';
  public $_age='';
  public $_expert=array();
  public function __construct($name,$age,array $expert)

       {
    $this->_name=$name;
    $this->_age=$age;
    $this->_expert=implode("、",$expert);
  }
  public function self_introduce()
  {
    echo sprintf("大家好!我是%s,职业拳手,今年%s,我会%s,快来找我训练吧%s",$this->_name,$this->_age,$this->_expert,"<br/>");
  }
}

$cpc = new mycoach('陈培昌',21,array("散打","泰拳"));
$cpc->self_introduce();
$cj = new mycoach('程劲',19,array('散打','泰拳','巴西柔术'));
$cj->self_introduce();
?>

关键点一:函数传参的时候如果是个数组要声明数据类型为array :public function __construct($name,$age,array $expert)

关键点二:要获取数组里的值,要使用implode方法把数组炸开implode("、",$expert),第一个参数是指明用什么符号分割这些值

结果:

类公有变量访问方式 $this->变量名 = xxxx;

类常量的访问方式 类名::常量名

猜你喜欢

转载自www.cnblogs.com/saintdingspage/p/9958885.html