類和執行個體
每個對象都與其他的對象不同,但是某些對象之間也可能有相似性。
類是對象的抽象,對象是類的執行個體。
類
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;
/// <summary>
/// security --manage user--role--power--resource
/// </summary>
/// <summary>
/// dbutil
/// </summary>
public abstract class dbutil
{
public static string connectionstring = configurationmanager.apps教程ettings["connectionstring"].tostring() + system.web.httpcontext.current.server.mappath(configurationmanager.appsettings["dbpath"]);
/// <summary>
/// internal function to prepare a command for execution by the database
/// </summary>se
/// <param name="cmd">existing command object</param>
/// <param name="conn">database connection object</param>
/// <param name="cmdtype">command type, e.g. stored procedure</param
/// <param name="cmdtext">command text</param>
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;
}
/// <summary>
/// execute an oraclecommand (that returns no resultset) against an existing database transaction
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = executenonquery(trans, commandtype.storedprocedure, "publishorders", new oracleparameter(":prodid", 24));
/// </remarks>
/// <param name="commandtext">the stored procedure name or pl/sql command</param>
/// <returns>an int representing the number of rows affected by the command</returns>
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;
}
}
/// <summary>
/// execute a select query that will return a result set
/// </summary>
/// <param name="commandtext">the stored procedure name or pl/sql command</param>
/// <returns></returns>
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;
}
}
/// <summary>
/// execute an oraclecommand that returns the first column of the first record against the database specified in the connection string
/// </summary>
/// <param name="commandtext">the stored procedure name or pl/sql command</param>
/// <returns>an object that should be converted to the expected type using convert.to{type}</returns>
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;
}
}
/// <summary>
/// 執行blob格式的檔案上傳sql
/// </summary>
/// <param name="cmdtext">sql命令,使用":files" 標誌 檔案欄位的值</param>
/// <param name="filefullname">檔案全路徑</param>
/// <returns></returns>
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;
}
}
}