【www.gdgbn.com--其他相关】
类和实例
每个对象都与其他的对象不同,但是某些对象之间也可能有相似性。
类是对象的抽象,对象是类的实例。
类
function stringbuffer(){
this.__strings__ = new array();
}
stringbuffer.prototype.append = function(str){
this.__strings__.push(str);
};
stringbuffer.prototype.tostring = function(){
return this.__strings__.join("");
};
可以认为共有同样行为的对象属于同一个类,一个“类”是对所有相似对象的一般分类。
类实际是对某种类型的对象定义状态和行为的原型,它表示对现实生活中一类具共同牲的事物的抽象.
类具就属性,它是对象状态的抽象,用数据结构来描述类的属性
类具有操作,它是对象行为的抽象,有操作名和实现操作折方法来描述
类允许我分描述一组对象在某个方面的普遍行为,然后当我们需要对象时,就创建具有那种行为方式的对象。
实例
由一个类按照规定的方式来表现行为的对象被称为这个类的“实例”.所有的对象都是某些类的实例,一旦一个实例被创建了,它表现的行为和这个类的其他实例一样。能够接收消息来完成它的方法所规定的任意操作。为了能以它的名义执行其他操作,这个实例还可以调用其他实例,也可以是其他类的实例。
继承
继承是面向对象语言中扩展已有类型的一种有效途继承
类是对被继承类的扩展.
继承是子类自动共享父类数据结构和方法的机制, 这是类之间的一种关系,在定义和实现一个类的时候,可以在一个已经存在的类的基础之上来进行,把这个已经存在的 类所定义的内容作为自己的内容,并加入若干新的内容.
继承的特性单一继承性:
子类只能有一个超类,而超类可以有多个子类
子类继承超类中非私有的全部成员
子类可以创建自己的成员
子类不能继承超类的构造器,但允许子类访问到超类的构造器
子类/派生类
一个子类是从另一个类继承行为的类,一个子类为了定义自己独特种类的对象,通常增加自己的行为。
超类/父类/基类
一个超类就是被继承特殊行为的类。一个类可以有一个超类,可以有多个超类.
抽象类
那些不需要产生实例和声明方法的存在而不去实现它的类称为"抽象类". 抽象类是不能被实例化的,它只能做为其他类的超类来使用。它们仅仅是一种存在,可以将各种都共有的行为提取公共因子到一个公共的地方,在那里作一次定义(如果需要,以后只要修改一次),就能反复的使用。抽象类充分定义了它们的行为,但是它们不需要完全实现,它们也能够定义由所有子类重新定义的方法。或许存在某个行为的默认实现,以防止系统错误时可以通过对在子类中实现的方法进行改进或增加来实现
看一个常用asp教程.net access数据访问类
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.data.oledb;//my using.
using system.io;///
/// security --manage user--role--power--resource
///
///
/// dbutil
///
public abstract class dbutil
{
public static string connectionstring = configurationmanager.apps教程ettings["connectionstring"].tostring() + system.web.httpcontext.current.server.mappath(configurationmanager.appsettings["dbpath"]);
///se
/// internal function to prepare a command for execution by the database
///
/// existing command object
/// database connection object
/// command type, e.g. stored procedure /// command text
private static void preparecommand(oledbcommand cmd, oledbconnection conn, commandtype cmdtype, string cmdtext)
{
//open the connection if required
if (conn.state != connectionstate.open)
conn.open();//set up the command
cmd.connection = conn;
cmd.commandtext = cmdtext;
cmd.commandtype = cmdtype;
}///
/// execute an oraclecommand (that returns no resultset) against an existing database transaction
/// using the provided parameters.
///
///
/// e.g.:
/// int result = executenonquery(trans, commandtype.storedprocedure, "publishorders", new oracleparameter(":prodid", 24));
///
/// the stored procedure name or pl/sql command
///an int representing the number of rows affected by the command
public static int executenonquery(string cmdtext)
{
// create a new oracle command
oledbcommand cmd = new oledbcommand();//create a connection
using (oledbconnection connection = new oledbconnection(configurationmanager.appsettings["connectionstring"].tostring() + system.web.httpcontext.current.server.mappath(configurationmanager.appsettings["dbpath"])))
{//prepare the command
preparecommand(cmd, connection, commandtype.text, cmdtext);//execute the command
int rowsaffected = cmd.executenonquery();return rowsaffected;
}
}
///
/// execute a select query that will return a result set
///
/// the stored procedure name or pl/sql command
///
public static oledbdatareader executereader(string cmdtext)
{
//create the command and connection
oledbcommand cmd = new oledbcommand();
oledbconnection conn = new oledbconnection(configurationmanager.appsettings["connectionstring"].tostring() + system.web.httpcontext.current.server.mappath(configurationmanager.appsettings["dbpath"]));try
{
//prepare the command to execute
preparecommand(cmd, conn, commandtype.text, cmdtext);//execute the query, stating that the connection should close when the resulting datareader has been read
oledbdatareader rdr = cmd.executereader(commandbehavior.closeconnection);
return rdr;}
catch
{//if an error occurs close the connection as the reader will not be used and we expect it to close the connection
conn.close();
throw;
}
}
///
/// execute an oraclecommand that returns the first column of the first record against the database specified in the connection string
///
/// the stored procedure name or pl/sql command
///an object that should be converted to the expected type using convert.to{type}
public static object executescalar(string cmdtext)
{
oledbcommand cmd = new oledbcommand();
using (oledbconnection conn = new oledbconnection(configurationmanager.appsettings["connectionstring"].tostring() + system.web.httpcontext.current.server.mappath(configurationmanager.appsettings["dbpath"])))
{
preparecommand(cmd, conn, commandtype.text, cmdtext);
object val = cmd.executescalar();
return val;
}
}///
/// 执行blob格式的文件上传sql
///
/// sql命令,使用":files" 标志 文件字段的值
/// 文件全路径
///
public static int uploadfile(string cmdtext, string filefullname)
{
// create a new oracle command
oledbcommand cmd = new oledbcommand();//create a connection
using (oledbconnection conn = new oledbconnection(configurationmanager.appsettings["connectionstring"].tostring() + system.web.httpcontext.current.server.mappath(configurationmanager.appsettings["dbpath"])))
{//prepare the command
preparecommand(cmd, conn, commandtype.text, cmdtext);//upload file
filestream fs = file.openread(filefullname);
byte[] content = new byte[fs.length];
fs.read(content, 0, content.length);
fs.close();cmd.parameters.add("@files", system.data.oledb.oledbtype.binary).value = content;
//execute the command
int rowsaffected = cmd.executenonquery();return rowsaffected;
}
}
public static dataset getdataset(string cmdtext)
{
oledbcommand cmd = new oledbcommand();using (oledbconnection conn = new oledbconnection(configurationmanager.appsettings["connectionstring"].tostring() + system.web.httpcontext.current.server.mappath(configurationmanager.appsettings["dbpath"])))
{
preparecommand(cmd, conn, commandtype.text, cmdtext);oledbdataadapter apr = new oledbdataadapter(cmd);
dataset ds = new dataset();
apr.fill(ds);return ds;
}
}
}