批改状态:合格
老师批语:
代码如下
<?php
class Person
{
protected $name;
public function __construct($name='张三')
{
$this->name = $name;
}
public function study($course='语文')
{
return $this->name .'学习'.$course.'很努力';
}
}
if (!trait_exists('Course')) {
trait Course
{
public $friend = '李四';
public function sport($name='打篮球')
{
$this->name.'和'.$this->friend.'在'.$name;
}
public function hobby($name)
{
return $this->name.'爱好'.$name;
}
public function study($course = '英语')
{
return $this->name.'学习'.$course.'学不会';
}
}
}
class Student extends Person
{
use Course;
}
class Worker
{
use Course;
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function __get($name)
{
return $this->$name;
}
}
$student = new Student();
echo $student->study().'<br>';
echo $student->hobby('打篮球').'<br>';
$worker = new Worker('小五');
echo $worker->name.'<br>';
echo $worker->hobby('打游戏');运行结果![1525934829465047.png 9VOB0MN]6K[JY~BIOSSQGG8.png](https://img.php.cn//upload/image/255/513/287/1525934829465047.png)
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号