类的封装作业

Original 2018-11-01 22:08:26 144
abstract:class Student{ public $name; public $age; protected $sex; private $score; const XUEJI = '20180808'; public function __construct($name,$age,$sex,$score){ $this ->name = $name; $this -&g


class Student

{

public $name;

public $age;

protected $sex;

private $score;

const XUEJI = '20180808';


public function __construct($name,$age,$sex,$score){


$this ->name = $name;

$this ->age = $age;

$this ->sex = $sex;

$this ->score = $score;


}


public function getSex(){

switch($this ->sex){

case 1:

$sex = '男';

break;

case 2:

$sex = '女';

break;

default;

$sex = '保密';

}

return $sex;

}


public function getScore(){

$f = $this -> score;

if($f >= 80){

$score = "优";

}else if($f < 80 && $f >=60){

$score = "良";

}else{

$score = "差";

}

return $score;

}




}



$stu = new Student('李二狗','18','1','59');


echo '姓名:'.$stu->name.' 年龄:'.$stu -> age.' 性别:'.$stu ->getSex().' 班级:'.Student::XUEJI." 成绩:".$stu -> getScore();



Correcting teacher:天蓬老师Correction time:2018-11-01 22:53:51
Teacher's summary:代码是正确的,不过有一个建议,类的构造方法的参数,大多具有默认值,以防止调用者没有传递参数,而导致实例化失败

Release Notes

Popular Entries