批改状态:未批改
老师批语:
引用代码:
<?php
spl_autoload_register(function($className){
require './'.$className.'.php';
});
$two = new two('张三','18', '男',['打游戏','学习'],'学生');
$two = new two('李四','18', '男',['看电视','打球'],'学生');
echo '姓名: '.$two->names.'<br>';
echo '年龄: '.$two->sex.'<br>';
echo '性别: '.$two->age.'<br>';
echo '爱好:'.$two->grade.'<br>';
echo '工作:'.$two->room.'<br>';
echo $two->shenfen().'<br>';
echo $two->game().'<br>';父类代码
<?php
class one
{
protected $names;
protected $sex;
protected $age;
public function __construct($names,$sex,$age)
{
$this->names = $name;
$this->sex = $sex;
$this->age = $age;
}
public function shenfen()
{
return '好学生';
}
}子类代码
<?php
class two extends one
{
public function __get($name)
{
return $this->$name;
}
private $grade = true;
private $rome = true;
public function __construct($names,$sex,$age,$grade,$rome)
{
parent::__construct($names,$sex,$age);
$this->grade = $grade;
$this->rome = $rome;
}
public function game()
{
return '玩游戏';
}
public function shenfen()
{
return parent::shenfen().',热爱学习';
}
}了解了子类与父类的关系,子类的功能是用来扩展或重载父类的某些功能,对父类属性进行扩展,增加新的特征
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号