继承是建立一个类,然后创建它的更特殊的版本。是OOP中不可缺少的部分。
人在描述事物的时候是有层次的,那么继承就使程序对现实世界有层次的描述的表达成为可能。对程序员来说继承的重点是共同点。因为有共同点才能重用。
实现是建立一个接口,然后由某些类来实现接口描述的细节。就好比是工程师绘制了部件,然后由工人做出具体的产品,工程师并没有去制造部件。
类与接口的区别就在于此,类不但有描绘,还实现了部分细节。而特殊的抽象类完全带抽象方法,就和接口完成的同样功用。而且,无论接口还是类,都不是凭空去描绘出来的 ,否则就是纸上谈兵了,而是根据具体类的特征抽象出来的。所以他们有共同点。
目录
一、继承与实现
接口实现示例
类继承示例
二、继承,实现简单论述
1、接口的实现以及接口对类的访问
2、类的继承关于以及父类对子类的访问
一、继承与实现
抽象有两种抽象方式。
1、他们有相同的工作方式;
2、他们有相同的名称、作用和特征。
第一种方式是类的继承,而第二种方式是接口的实现。
比如:数据库操作中一般都会用到 添加,删除,更新,读取。那么就是他们都有相同的名称、作用和特征。就可以使用接口实现。
using System.Collections;
using System.Data;
interface IMyData
{
void MyInsert(ArrayList al);
void MyDelete(ArrayList al);
void MyUpdate(ArrayList al);
DataSet MySelect(ArrayList al);
}
using System;
namespace FmRadio
{
/** <summary>
/// DMusicSort 的摘要说明。
/// </summary>
public class DMusicSort : IMyData
{
public DMusicSort()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
IMyData 成员#region IMyData 成员
public void MyInsert(System.Collections.ArrayList al)
{
// TODO: 添加 DMusicSort.MyInsert 实现
}
public void MyDelete(System.Collections.ArrayList al)
{
// TODO: 添加 DMusicSort.MyDelete 实现
}
public void MyUpdate(System.Collections.ArrayList al)
{
// TODO: 添加 DMusicSort.MyUpdate 实现
}
public System.Data.DataSet MySelect(System.Collections.ArrayList al)
{
// TODO: 添加 DMusicSort.MySelect 实现
return null;
}
#endregion
}
}
再比如:数据库的连接,这里只说MS Sql Srever 2000,在一个程序只使用一个库的情况下,所有关于数据库操作类都将执行相同的数据库连接。他们有相同的工作方式。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace FmRadio
{
/** <summary>
/// MyConnection 的摘要说明。
/// </summary>
public class MyConnection
{
private SqlConnection pConn;
public MyConnection()
{
//
// TODO: 在此处添加构造函数逻辑
//
pConn = new SqlConnection(ConfigurationSettings.AppSettings["DbLink"]);
}
public SqlConnection conn
{
get
{
// TODO: 添加 MyConnection.conn getter 实现
return pConn;
}
}
}
}
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace FmRadio
{
/** <summary>
/// Execute 的摘要说明。
/// </summary>
public class Execute : MyConnection
{
private int sType = 0;
private ArrayList pPara;
private string pContext;
public Execute()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public Execute(string pContext,ArrayList pPara)
{
this.pPara = pPara;
this.pContext = pContext;
}
public Execute(int pType,string pContext,ArrayList pPara)
{
this.sType = pType;
this.pPara = pPara;
this.pContext = pContext;
}
public int ExeNone()
{
// TODO: 添加 Execute.ExeNone 实现
SqlCommand sc = null;
CommandType ct = CommandType.StoredProcedure;
if(this.sType==0)
ct = CommandType.StoredProcedure;
else
ct = CommandType.Text;
int rs = 0;
sc = new SqlCommand();
sc.CommandType = ct;
sc.Connection = conn;
sc.CommandText = pContext;
foreach(DataPara dp in pPara)
{
sc.Parameters.Add("@" + dp.ColName,dp.ColContent.GetType()).Value = dp.ColContent;
}
// try
// {
conn.Open();
rs = sc.ExecuteNonQuery();
conn.Close();
// }
// catch{}
return rs;
}
public DataSet ExeTable()
{
// TODO: 添加 Execute.ExeTable 实现
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand sc = null;
CommandType ct = CommandType.StoredProcedure;
if(this.sType==0)
ct = CommandType.StoredProcedure;
else
ct = CommandType.Text;
sc = new SqlCommand();
sc.CommandType = ct;
sc.Connection = conn;
sc.CommandText = pContext;
foreach(DataPara dp in pPara)
{
sc.Parameters.Add("@" + dp.ColName,dp.ColContent.GetType()).Value = dp.ColContent;