Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:一个扩展类中不一定会将三大功能全具备的, 但遇到的要知道它在做什么
<?php
class StuInfo
{
protected $name = 'angle';
protected $age = 32;
protected $sex = '女';
protected static $school = '人民大学';
public function printInfo()
{
echo '姓名:',$this ->name,'<br>';
echo '年龄:',$this ->age,'<br>';
echo '性别:',$this ->sex,'<br>';
}
public static function printSchool()
{
echo '学校名称:',self::$school,'<br>','<hr>';
}
}
//类的继承:子类继承父类的所有属性和方法
class ClassInfo extends StuInfo
{
//子类可以对父类的属性进行扩展
protected $class = '05-01';
//子类可以对你类的方法进行重写
public function printInfo()
{
parent::printInfo();
echo '所在班级:',$this->class,'<br>';
parent::printSchool();
}
//子类也可以对父类的方法进行扩展
public function editInfo($name,$age,$sex,$class)
{
$this ->name = $name;
$this ->age = $age;
$this ->sex = $sex;
$this ->class = $class;
}
}
$stu1 = new ClassInfo();
$stu1 ->printInfo();
$stu2 = new ClassInfo();
$stu2 ->editInfo('hugn',31,'男','05-02');
$stu2 ->printInfo();
?>
<?php
//抽象类不能被实例化,只能被继承,抽象类的作用,完成部分设计与实现分离
abstract class StuInfo
{
protected $name = 'angle';
protected $age = 32;
protected $sex = '女';
protected static $school = '人民大学';
public function printInfo()
{
echo '姓名:',$this ->name,'<br>';
echo '年龄:',$this ->age,'<br>';
echo '性别:',$this ->sex,'<br>';
}
public static function printSchool()
{
echo '学校名称:',self::$school,'<br>';
}
}
//抽象类不能被实例,只能被继承
abstract class departInfo extends StuInfo
{
protected $department = '计算机系';
public function printInfo()
{
parent::printInfo();
echo '所在系:',$this -> department,'<br>';
}
}
//普能类可以被实例化,可以继承抽象类
class ClassInfo extends departInfo
{
protected $class = '05-01';
public function printInfo()
{
parent::printInfo();
echo '所在班级:',$this ->class,'<br>';
parent::printSchool();
echo '<hr>';
}
public function editInfo($name,$age,$sex,$department = '计算机系',$class = '05-01')
{
$this ->name = $name;
$this ->age = $age;
$this ->sex = $sex;
$this ->department = $department;
$this ->class = $class;
}
public static function editSchool($school)
{
self::$school = $school;
}
}
$stu1 = new ClassInfo();
$stu1 ->printInfo();
$stu2 = new ClassInfo();
$stu2 ->editInfo('hugn',31,'男');
$stu2 ->printInfo();
$stu3 = new ClassInfo();
$stu3 ->editInfo('lucy',35,'女','工商管理','06-02');
$stu3 ->editSchool('清华大学');
$stu3 ->printInfo();
?>
<?php
//接口的定义与类的定义相类似
interface iStuInfo
{
const CITY = '北京';
public function printInfo();
public function editInfo($name,$age,$sex,$department = '计算机系',$class = '05-01');
public function printCity();
}
class StuInfo implements iStuInfo
{
protected $name = 'angle';
protected $age = 32;
protected $sex = '女';
protected $class = '05-01';
protected $department = '计算机系';
protected static $school = '人民大学';
public function printInfo()
{
echo '姓名:',$this ->name,'<br>';
echo '年龄:',$this ->age,'<br>';
echo '性别:',$this ->sex,'<br>';
echo '班级:',$this ->class,'<br>';
echo '所在系:',$this ->department,'<br>';
echo '学校名称:',self::$school,'<br>';
}
public function editInfo($name,$age,$sex,$department = '计算机系',$class = '05-01')
{
$this ->name = $name;
$this ->age = $age;
$this ->sex = $sex;
$this ->department = $department;
$this ->class = $class;
}
public function printCity()
{
echo '所在城市:',iStuInfo::CITY,'<br>';
echo '<hr>';
}
}
$stu1 = new StuInfo();
$stu1 ->printInfo();
$stu1 ->printCity();
$stu2 = new StuInfo();
$stu2 ->editInfo('hugn',31,'男');
$stu2 ->printInfo();
$stu2 ->printCity();
?>