Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
oop基础:请举例实例演绎以下难点
//如何给 protected private 属性赋值
// __construct()魔术方法 也叫构造函数、构造器:类实例化一次就被自动调用一次
public function __construct($name,$height,$team,$playNum,$weight){
/**
* 1.初始化类成员,让类、实例化的状态稳定下来
* 2、给对象属性进行初始化赋值
* 3、可以给私有成员,爱保护的成员初始化赋值
*/
$this->name = $name;
$this->height = $height;
$this->team = $team;
$this->playNum = $playNum;
$this->weight = $weight;
}
public function jog(){
//$this 特殊的对象引用:代表对象
return "$this->name is jogging ,whose playNum is $this->playNum<br>";
}
public function shoot(){
return “$this->name is shooting ,weighting $this->weight<br>“;
}
}
$jordan = new Player(‘Jordan’,’203cm’,’Bulk’,6,’80kg’);
//echo $jordan->weight.’<br>‘;
echo $jordan->shoot();
?>
2. 构造方法
```php
public function __construct($name,$price)
{
$this->name = $name;
$this->price = $price;
}
对象成员之间的内部访问 $this
public function show(){
return "{$this->name}的单价为{$this->price}元";
}
private仅限本类中使用 protected本类中,子类中使用
class User{
public static $name =’胡歌’;
protected $_config =[
'auth_on' =>'true',
'auth_type'=> 1
];
public static $nation = 'China';
private $salary;
static $count = 0;
public function __construct($name,$salary){
//静态成员与类的实例无关,不能用$this访问,用self::类的引用
// 访问静态成员
self::$name = $name;
self::$count++;
$this->salary =$salary;
}
类的自动加载 spl_autoload_register
<?php
/**
spl_autoload_register(function($className)){
require $className.’.php’;
}
}
//静态成员与类的实例无关,不能用$this访问,用self::类的引用
// 访问静态成员
self::$name = $name;
self::$count++;
$this->salary =$salary;
}
//parent关键字,调用父类中的成员方法
parent::__construct($name,$price);
$this->num = $num;