时间:2021-07-01 10:21:17 帮助过:3人阅读
如果将角色、积分作为用户属性,那以后如果增加了新的元素,我不是要修改代码了吗?那样的话,用户对象就会变得好庞大。
可是如果不作为属性的话,角色、积分又确实是用户所拥有的。
你第一设计方案,本身并无不妥。只是写死了
而你的第二方案就灵活多了
至于是将用户传入角色,还是将角色传入用户,就是设计模式的选取了
?分可以分到另一? class去做。
?分?只增?改?分,不用?理??其他?料。例如
class score{ private $_oUser; public function __construct($oUser){ $this->_oUser = $oUser; } public function add(){ } public function update(){ }}
感谢两位的回复。我对面向对象的知识还是太欠缺了,正如版主所说,我正在看设计模式的文章,希望能加深我对面向对象的理解。
上班?聊借此???手???面向?象基?
userName = $userName; $this->password = $password; }}//角色?class Role{ private $userName; public $roleName; function __construct($userName,$roleName){ $this->userName = $userName; $this->roleName = $roleName; } function GetRoleUserName() { return $this->userName; }}//?分?class Score{ private $userName; private $module; public $score; function __construct($userName,$module,$score){ $this->userName = $userName; $this->module = $module; $this->score = $score; } function GetScoreUserName() { return $this->userName; } function GetScoreModule() { return $this->module; } }//用?? ?承用?基?class User extends BaseUser{ public $role; public $score; function __construct($userName,$password,$roleName,$score){ parent::__construct($userName, $password); $this->role = new Role($userName,$roleName); $this->score = new Score($userName,"module",$score); }}$user = new User("user9527","123","xiaoming",100);echo "userName: ".$user->userName;echo "
";echo "password: ".$user->password;echo "
";echo "roleName: ".$user->role->roleName;echo "
";echo "score: ".$user->score->score;echo "
";/*userName: user9527password: 123roleName: xiaomingscore: 100 *///code end