您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

面向对象分析与设计疑点

2024/4/21 5:14:42发布31次查看
面向对象分析与设计疑问
各位大侠好,小弟接触面向对象也有段时间了,可每次设计新系统时都会遇到关于对象如何拆分与设计的问题,特来请教一番。
比如,现在的系统要求有:用户,角色,积分。
用户有:用户名,密码。
角色有:角色名。
积分有:积分值,所属用户,所属模块。
通过阅读网上的帖子,我觉得:
用户对象应该包含:用户名,密码,角色对象,积分对象
code: 
class user {
  public $username;
  public $password;
  public $role;
  public $score;
}
请问是这样设计的吗?可我总感觉哪地方不对,或许我觉得,角色对象和积分对象不作为用户对象的属性,当需要获取用户对象时,重新new一下角色对象,然后将用户对象传入,这样获取用户所对应的角色。这样设计合理吗?
------解决思路----------------------
你第一设计方案,本身并无不妥。只是写死了
而你的第二方案就灵活多了
至于是将用户传入角色,还是将角色传入用户,就是设计模式的选取了
------解决思路----------------------
積分可以分到另一個 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: user9527
password: 123
rolename: xiaoming
score: 100
*/
//code end

该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product