我们都知道面向对象的特征是封装、继承和多态,封装我们在vb版的机房中都用过,那么继承该怎么用?
在机房重构中有很多窗体都是相类似的,我们可以把这些窗体中公共的部分抽象出来,然后再继承这些窗体,
可以很好的达到代码复用的效果
一、父类窗体
机房中组合查询的窗体一共有三个,他们除了数据显示不同之外其他基本都是一样的,我们可以把一样的
东西封装起来单独构成一个父窗体
‘组合查询实体为保护型,为了子类成员函数的使用
Protected groupcheck As New Entity.GroupCheckEntity()
‘查询按钮
Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
If cobRelation1.Text = "" Then
If CombName1.Text = "" Or txtContent1.Text = "" Or CombOper1.Text = "" Then
MsgBox("第一行不能为空", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "系统提示")
Exit Sub
End If
Else
If cobRelation2.Text = "" Then
If CombName2.Text = "" Or txtContent2.Text = "" Or CombOper2.Text = "" Then
MsgBox("第二行不能为空", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "系统提示")
Exit Sub
End If
Else
If CombName3.Text = "" Or txtContent3.Text = "" Or CombOper3.Text = "" Then
MsgBox("第三行不能为空", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "系统提示")
Exit Sub
End If
End If
End If
‘把窗体上的信息赋值给实体
groupcheck.TableName = GetdbName()
groupcheck.CombName1 = ToEnglish(CombName1.Text)
groupcheck.CombName2 = ToEnglish(CombName2.Text)
groupcheck.CombName3 = ToEnglish(CombName3.Text)
groupcheck.CombOper1 = CombOper1.Text.Trim
groupcheck.CombOper2 = CombOper2.Text.Trim
groupcheck.CombOper3 = CombOper3.Text.Trim
groupcheck.TxtContent1 = txtContent1.Text.Trim
groupcheck.TxtContent2 = txtContent2.Text.Trim
groupcheck.TxtContent3 = txtContent3.Text.Trim
groupcheck.CobRelation1 = ToEnglish(cobRelation1.Text)
groupcheck.CobRelation2 = ToEnglish(cobRelation2.Text)
Dim dt As New DataTable
Dim Group As New BLL.GroupCheckBLL()
‘b层返回的数据类型为Datatable
dt = Group.GroupChecks(groupcheck)
If dt.Rows.Count = 0 Then
MsgBox("没有符合条件的记录", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "系统提示")
DataGrid.DataSource = Nothing
DataGrid.Refresh()
Exit Sub
Else
‘调用子窗体重写的Todatagrid方法
Call Todatagrid()
End If
End Sub
Private Sub PreStuBasicInfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
‘窗体的显示
CombOper1.Items.Add("<")
CombOper1.Items.Add(">")
CombOper1.Items.Add("=")
CombOper1.Items.Add("<>")
CombOper2.Items.Add("<")
CombOper2.Items.Add(">")
CombOper2.Items.Add("=")
CombOper2.Items.Add("<>")
CombOper3.Items.Add("<")
CombOper3.Items.Add(">")
CombOper3.Items.Add("=")
CombOper3.Items.Add("<>")
cobRelation1.Items.Add("与")
cobRelation1.Items.Add("或")
cobRelation2.Items.Add("与")
cobRelation2.Items.Add("或")
CombName2.Enabled = False
CombName3.Enabled = False
CombOper2.Enabled = False
CombOper3.Enabled = False
txtContent2.Enabled = False
txtContent3.Enabled = False
DataGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect
‘DataGridView控件,选取整行
Dim i As Integer
For i = 0 To DataGrid.Columns.Count - 1
DataGrid.Columns(i).Width = DataGridViewAutoSizeColumnMode.AllCells
Next
End Sub
Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
Me.Dispose()
End Sub
‘定义可重写的把汉字转换为英文的方法
Public Overridable Function ToEnglish(ByVal Name As String) As String
Return ""
End Function
‘定义可重写并返回要操作的数据库表
Public Overridable Function GetdbName() As String
Return ""
End Function
‘定义保护类型可被子类重写的显示数据的方法
Protected Overridable Sub Todatagrid()
End Sub
‘界面的优化
Private Sub cobRelation1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cobRelation1.SelectedIndexChanged
If cobRelation1.Text = "" Then
CombName2.Enabled = False
CombOper2.Enabled = False
txtContent2.Enabled = False
CombName3.Enabled = False
CombOper3.Enabled = False
txtContent3.Enabled = False
cobRelation2.Enabled = False
Else
CombName2.Enabled = True
CombOper2.Enabled = True
txtContent2.Enabled = True
cobRelation2.Enabled = True
End If
End Sub
‘界面的优化
Private Sub cobRelation2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cobRelation2.SelectedIndexChanged
If cobRelation2.Text = "" Then
CombName3.Enabled = False
CombOper3.Enabled = False
txtContent3.Enabled = False
Else
CombName3.Enabled = True
CombOper3.Enabled = True
txtContent3.Enabled = True
End If
End Sub
‘清空各个控件
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
‘如果comb的类型为ComBox控件那么清空
For Each comb As Control In Me.Controls
If comb.GetType Is GetType(ComboBox) Then
comb.Text = ""
End If
Next
txtContent1.Text = ""
txtContent2.Text = ""
txtContent3.Text = ""
DataGrid.DataSource = Nothing
End Sub
以上是抽象出三个组合查询的公共部分,其中包含三个可以被写的方法,子窗体可以根据本窗体的需求重
写该方法
二、继承窗体
‘显示本窗体需要的数据
Private Sub CheckStuInfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CombName1.Items.Add("学号")
CombName1.Items.Add("姓名")
CombName1.Items.Add("性别")
CombName1.Items.Add("学院")
CombName1.Items.Add("年级")
CombName1.Items.Add("班级")
CombName1.Items.Add("操作员")
CombName2.Items.Add("学号")
CombName2.Items.Add("姓名")
CombName2.Items.Add("性别")
CombName2.Items.Add("学院")
CombName2.Items.Add("年级")
CombName2.Items.Add("班级")
CombName2.Items.Add("操作员")
CombName3.Items.Add("学号")
CombName3.Items.Add("姓名")
CombName3.Items.Add("性别")
CombName3.Items.Add("学院")
CombName3.Items.Add("年级")
CombName3.Items.Add("班级")
CombName3.Items.Add("操作员")
Me.Text = "学生基本信息维护"
cobRelation2.Enabled = False
End Sub
‘选择了需要的数据,并且通过ToEnglish方法转换为数据库中的字段名
Public Overrides Function ToEnglish(Name As String) As String
Select Case Name
Case "学号"
ToEnglish = "studentno"
Case "姓名"
ToEnglish = "name"
Case "性别"
ToEnglish = "sex"
Case "学院"
ToEnglish = "department"
Case "年级"
ToEnglish = "grade"
Case "班级"
ToEnglish = "classes"
Case "操作员"
ToEnglish = "head"
Case "或"
ToEnglish = "or"
Case "与"
ToEnglish = "and"
Case Else
ToEnglish = ""
End Select
End Function
‘返回要操作的数据库的表名称
Public Overrides Function GetdbName() As String
Return "Student_Info"
End Function
‘显示本窗体的数据
Protected Overrides Sub Todatagrid()
Dim dt As New DataTable
‘定义B层GroupCheckBLL方法的新对象
Dim ShowInfo As New BLL.GroupCheckBLL()
‘传入数据,并调用该对象的方法,返回dt
dt = ShowInfo.GroupChecks(groupcheck)
DataGrid.AutoGenerateColumns = True
‘改变dt中每一列要显示的名称
dt.Columns("studentno").ColumnName = "学号"
dt.Columns("name").ColumnName = "姓名"
dt.Columns("sex").ColumnName = "性别"
dt.Columns("department").ColumnName = "学院"
dt.Columns("grade").ColumnName = "年级"
dt.Columns("classes").ColumnName = "班级"
dt.Columns("head").ColumnName = "操作员"
‘把值传给DataGrid控件
DataGrid.DataSource = dt
End Sub
以上包含了子类窗体重写父类窗体的方法
三、比较
通过对继承方法实现组合查询与以前我们用VB实现组合查询的方法进行对比,我们可以很明显发现用继承
方法的方便,而且也使得代码得到复用,显得更加简洁
这里为什么要返回数据库表的名称?这涉及到D层是如何实现对数据库的操作的,下篇博客会提到实现的方
法-存储过程
原文地址:http://blog.csdn.net/husen1314/article/details/43989515