类成员的访问与封装技术课程作业,麻烦老师给优化下代码!

Original 2018-10-30 21:44:27 142
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();


Release Notes

Popular Entries