文档库 最新最全的文档下载
当前位置:文档库 › CSharp编程攻略(封装)

CSharp编程攻略(封装)

编程攻略:封装
Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象。但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有class(类)。
那么,如果我们要把"属性"(property)和"方法"(method),封装成一个对象,甚至要从原型对象生成一个实例对象,我们应该怎么做呢?
一、 生成实例对象的原始模式
假定我们把猫看成一个对象,它有"名字"和"颜色"两个属性。
var Cat = {
name : '',
color : ''
}
现在,我们需要根据这个原型对象的规格(schema),生成两个实例对象。
var cat1 = {}; // 创建一个空对象
https://www.wendangku.net/doc/9718885081.html, = "大毛"; // 按照原型对象的属性赋值
cat1.color = "黄色";
var cat2 = {};
https://www.wendangku.net/doc/9718885081.html, = "二毛";
cat2.color = "黑色";
好了,这就是最简单的封装了,把两个属性封装在一个对象里面。但是,这样的写法有两个缺点,一是如果多生成几个实例,写起来就非常麻烦;二是实例与原型之间,没有任何办法,可以看出有什么联系。
二、 原始模式的改进
我们可以写一个函数,解决代码重复的问题。
function Cat(name,color) {
return {
name:name,
color:color
}
}
然后生成实例对象,就等于是在调用函数:
var cat1 = Cat("大毛","黄色");
var cat2 = Cat("二毛","黑色");
这种方法的问题依然是,cat1和cat2之间没有内在的联系,不能反映出它们是同一个原型对象的实例。
三、 构造函数模式
using System;
using System.Data;
using System.Collections.Generic;
using https://www.wendangku.net/doc/9718885081.html,mon;
using Maticsoft.Model;
namespace Maticsoft.BLL
{
///


/// Cuser
///

public partial class Cuser
{
private readonly Maticsoft.DAL.Cuser dal=new Maticsoft.DAL.Cuser();
public Cuser()
{}
#region BasicMethod
///
/// 是否存在该记录
///

public bool Exists(string UserID)
{
return dal.Exists(UserID);
}

///
/// 增加一条数据
///

public bool Add(Maticsoft.Model.Cuser model)
{
return dal.Add(model);
}

///
/// 更新一条数据
///

public bool Update(Maticsoft.Model.Cuser model)
{
return dal.Update(model);
}

///
/// 删除一条数据
///

public bool Delete(string UserID)
{

return dal.Delete(UserID);
}
///
/// 删除一条数据
///

public bool DeleteList(string UserIDlist )
{
return dal.DeleteList(UserIDlist );
}

///
/// 得到一个对象实体
//

/


public Maticsoft.Model.Cuser GetModel(string UserID)
{

return dal.GetModel(UserID);
}

///
/// 得到一个对象实体,从缓存中
///

public Maticsoft.Model.Cuser GetModelByCache(string UserID)
{

string CacheKey = "CuserModel-" + UserID;
object objModel = https://www.wendangku.net/doc/9718885081.html,mon.DataCache.GetCache(CacheKey);
if (objModel == null)
{
try
{
objModel = dal.GetModel(UserID);
if (objModel != null)
{
int ModelCache = https://www.wendangku.net/doc/9718885081.html,mon.ConfigHelper.GetConfigInt("ModelCache");
https://www.wendangku.net/doc/9718885081.html,mon.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
}
}
catch{}
}
return (Maticsoft.Model.Cuser)objModel;
}

///
/// 获得数据列表
///

public DataSet GetList(string strWhere)
{
return dal.GetList(strWhere);
}
///
/// 获得前几行数据
///

public DataSet GetList(int Top,string strWhere,string filedOrder)
{
return dal.GetList(Top,strWhere,filedOrder);
}
///
/// 获得数据列表
///

public List GetModelList(string strWhere)
{
DataSet ds = dal.GetList(strWhere);
return DataTableToList(ds.Tables[0]);
}
///
/// 获得数据列表
///

public List DataTableToList(DataTable dt)
{
List modelList = new List();
int rowsCount = dt.Rows.Count;
if (rowsCount > 0)
{
Maticsoft.Model.Cuser model;
for (int n = 0; n < rowsCount; n++)
{
model = dal.DataRowToModel(dt.Rows[n]);
if (model != null)
{
modelList.Add(model);
}
}
}
return modelList;
}

///
/// 获得数据列表
///

public DataSet GetAllList()
{
return GetList("");
}

///
/// 分页获取数据列表
///

public int GetRecordCount(string strWhere)
{
return dal.GetRecordCount(strWhere);
}
///
/// 分页获取数据列表
///

public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
{
return dal.GetListByPage( strWhere, orderby, startIndex, endIndex);
}
///
/// 分页获取数据列表
///

//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
//{
//return dal.GetList(PageSize,PageIndex,strWhere);
//}

#endregion BasicMethod
#region ExtensionMethod

#endregion ExtensionMethod
}
}

为了解决从原型对象生成实例的问题,Javascript提供了一个构造函数(Constructor)模式。
所谓"构造函数",其实就是一个普通函数,但是内部使用了this变量。对构造函数使用new运算符,就能生成实例,并且this变量

会绑定在实例对象上。
比如,猫的原型对象现在可以这样写,
function Cat(name,color){
https://www.wendangku.net/doc/9718885081.html,=name;
this.color=color;
}
我们现在就可以生成实例对象了。
var cat1 = new Cat("大毛","黄色");
var cat2 = new Cat("二毛","黑色");
alert(https://www.wendangku.net/doc/9718885081.html,); // 大毛
alert(cat1.color); // 黄色
这时cat1和cat2会自动含有一个constructor属性,指向它们的构造函数。
alert(cat1.constructor == Cat); //true
alert(cat2.constructor == Cat); //true
Javascript还提供了一个instanceof运算符,验证原型对象与实例对象之间的关系。
alert(cat1 instanceof Cat); //true
alert(cat2 instanceof Cat); //true
四、构造函数模式的问题
构造函数方法很好用,但是存在一个浪费内存的问题。
请看,我们现在为Cat对象添加一个不变的属性type(种类),再添加一个方法eat(吃)。那么,原型对象Cat就变成了下面这样:
function Cat(name,color){
https://www.wendangku.net/doc/9718885081.html, = name;
this.color = color;
this.type = "猫科动物";
this.eat = function(){alert("吃老鼠");};
}

相关文档