当前位置: 代码迷 >> ASP.NET >> 面向对象类的设计 有关问题。大家帮忙了
  详细解决方案

面向对象类的设计 有关问题。大家帮忙了

热度:7870   发布时间:2013-02-25 00:00:00.0
面向对象类的设计 问题。大家帮忙了。
问大家一个类的问题。

我现在做个项目 想用标准的面向对象来做,顺带学习下。 但是

设计类的时候 我有些疑问。
举个例子:
商品 有 ID 名称 品牌 价格,评论 等。 用户可以对商品 评论

。 评论 有 评论ID,题目,内容,发表时间,评论人。 用户 

有ID 姓名。 品牌 又有 品牌ID 名称
我的分析上面共有
商品,品牌,用户,评论 这4个对象
商品拥有品牌,用户可以操作(添加,删除,修改)商品,用户

可以发表评论。 商品拥有评论。

写成类的时候我有些疑惑了。
我认为的几种写法:
第一种:
class object
{
string id;
}
class user:object // 用户类
{
string name;
}
class brand:object //品牌类
{
string name;
string summary;
}
class pro:object //商品类
{
string name;
class brand;
class comment;

void add(){}
void void edit(){}
void update(){}
void delete(){}
}
class comment:object //评论类
{
string title;
string context;
datetime time;
class user;

void postcontext(){}
}
第二种:
class object
{
string id;
}
class user:object // 用户类
{
string name;
}
class brand:object //品牌类
{
string name;
string summary;
}
class pro:object //商品类
{
string name;
string brandid;
string commentid;

void add(){}
void void edit(){}
void update(){}
void delete(){}
}
class comment:object //评论类
{
string title;
string context;
datetime time;
string userid;

void postcontext(){}
}

============
第一种 是 类包含类,第2种是 类和类是独立的。 没关系。
第一种 看起来 规范些(我看起来而已)
但是 new 一个 类的时候,会 连带new 很多子类。会不会 占资

源? 效率底?
比如 商品类
class pro:object //商品类
{
string name;
string brandid;
class brand;
public setInfo()
{
  取出商品的基本信息 name brandid赋值;
}
public pro(string proid)
{
  setInfo();
  brand = new brand(brandid);//为了这个 我加了个brandid 

属性 我觉得和brand 这个类有点 重复了 不知道如果处理。
}
}

这样 用起来 很方便 要得到 商品的品牌的信息 只需要
class pro = new pro(id);
pro.brand.summary 就可以得到了
第2种 就要分别 再去 操作brand类 得到summary
但是 这样对吗? 标准的类 是如何设计的?
我觉得上面2个设计的都不太好,不知道标准的是怎么做的。

麻烦大家了。 说说这个例子上的类应该如何设计。
我在网上查页查不到资料。

------解决方案--------------------------------------------------------
对于类的继承,如果没必要就不继承。一般都是类单独
------解决方案--------------------------------------------------------
例子来说,简单一点,设计成四个单独的类就行,类的属性跟数据库的字段对应,然后需要什么其它的再增加

------解决方案--------------------------------------------------------
C# code
public class User    {        #region 私有成员        private int _userID;        //用户ID        private string _loginName;    //用户登录名        private string _userName;    //用户姓名        private string _password;    //用户密码        private string _address;    //用户地址        private string _homepage;    //用户主页        private string _email;        //用户Email        private bool _exist;        //是否存在标志        #endregion 私有成员        #region 属性        public int UserID        {            set            {                this._userID = value;            }            get            {                return this._userID;            }        }        public string LoginName        {            set            {                this._loginName = value;            }            get            {                return this._loginName;            }        }        public string UserName        {            set            {                this._userName = value;            }            get            {                return this._userName;            }        }        public string Password        {            set            {                this._password = value;            }            get            {                return this._password;            }        }        public string Address        {            set            {                this._address = value;            }            get            {                return this._address;            }        }        public string Homepage        {            set            {                this._homepage = value;            }            get            {                return this._homepage;            }        }        public string Email        {            set            {                this._email = value;            }            get            {                return this._email;            }        }        public bool Exist        {            get            {                return this._exist;            }        }        #endregion 属性