Correction status:qualified
Teacher's comments:案例写得不错
1.命名空间:类似于文件目录,不同空间可以存放相同的类
// 空间名称 namespace music; // 该空间下的类 class first { public function info(){ $name = '《风车》'; $author = '柴樱'; return '本周音乐榜第一'.$name.'--'.$author; } } $obj1 = new first; echo $obj1->info(); echo '<hr>'; namespace movie; class first { public function info(){ $name = '《哪吒之魔童降世》'; $author = '饺子'; return '本周电影榜榜第一'.$name.'--'.$author; } } $obj2 = new first; echo $obj2->info(); echo '<hr>'; namespace tv; class first { public function info(){ $name = '《长安十二时辰》'; $author = '曹盾'; return '本周电视剧榜第一'.$name.'--'.$author; } } $obj3 = new first; echo $obj3->info(); echo '<hr>';
点击 "运行实例" 按钮查看在线实例
2.类,相当于模板,可以多处调用,子类可以继承父类的全部属性与方法
namespace student; class info{ public $name; public $grade; // __construct构造函数 public function __construct($name = '无名氏',$grade = '你猜呀!',$class = '天才班'){ // $this当前 $this->name = $name; $this->grade = $grade; $this->class = $class; } public function name(){ return $this->name; } public function grade(){ return $this->grade; } public function class(){ return $this->class; } } class honor extends info{ public function title(){ $grade = parent::grade(); if($grade>=95){ return '<span style="color:#ff2525;">【学神】</span>'; }else if(95>$grade && $grade>=90){ return '<span style="color:#ff6c02;">【学霸】</span>'; }else if(90>$grade && $grade>=80){ return '<span style="color:#ffd400;">【优秀】</span>'; }else if(80>$grade && $grade>=60){ return '<span style="color:#7ab900;">【合格】</span>'; }else{ return '<span style="color:#999;">【渣渣】</span>'; } } public function stu(){ return $this->title().'姓名:'.$this->name.' -- 成绩:'.$this->grade.' -- 班级:'.$this->class."<span></span>"; } } $honor = new honor('诸葛亮','99','高三一班'); echo $honor->stu(); echo '<hr>'; $honor1 = new honor('刘备','92','高三一班'); echo $honor1->stu(); echo '<hr>'; $honor2 = new honor('关羽','89','高三二班'); echo $honor2->stu(); echo '<hr>'; $honor3 = new honor('张飞','76','高三二班'); echo $honor3->stu(); echo '<hr>'; $honor4 = new honor('路人甲','55','高三三班'); echo $honor4->stu(); echo '<hr>';
点击 "运行实例" 按钮查看在线实例