批改状态:未批改
老师批语:
通过创建父类的变量和方法,和继承父类的方法的调用来了解类的使用
父类:People.php
<?php
class People {
protected $name;
protected $sex;
protected $age;
public function __construct($name='小明', $sex='男', $age=18) {
$this->name = $name;
$this->sex = $sex;
$this->age = $age;
}
//创建公共方法
public function __get($name) {
return $this->$name;
}
public function __set($name, $value) {
$this->$name = $value;
}
public static function tell(){
return '交流';
}
public static function help(){
return '帮助他人';
}
}点击 "运行实例" 按钮查看在线实例
子类 Student
<?php
/**
* 学生
*/
require 'People.php';
class Student extends People{
//put your code here
public static function study(){
return '我能够学习';
}
}点击 "运行实例" 按钮查看在线实例
demo.php脚本
<?php
//类的自动加载
spl_autoload_register(function($className){ require './class/'.$className.'.php';});
$one = new Student('欣欣','女','17');
echo $one->help(),'<br>';
echo $one->tell(),'<br>';
echo $one->study(),'<br>';
echo People::tell(),'<br>';
echo Student::study(),'<br>';
echo $one->age;
echo $one->name;
echo $one->sex;点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号