VB.net学习笔记(十五)继承与多接口练习

来源:互联网 发布:网络赚钱棋牌游戏 编辑:程序博客网 时间:2024/06/05 16:08



新建项目ObjectAndCompoment,添加一个只能继承的基类Contact

Public MustInherit Class Contact    Private mID As Guid = Guid.NewGuid    Private mName As String    Public Property ID() As Guid        Get            Return mID        End Get        Set(value As Guid)            mID = value        End Set    End Property    Public Property Name() As String        Get            Return mName        End Get        Set(value As String)            mName = value        End Set    End PropertyEnd Class


再添加一个子类Customer:

Public Class Customer    Inherits Contact    Private mPhone As String    Public Property Phone() As String        Get            Return mPhone        End Get        Set(value As String)            mPhone = value        End Set    End PropertyEnd Class

这样ObjectAndCompoment项目里面有两个类,子类继承基类的属性及相关变量。






再添加一项目(文件->添加->添加新项目),选择类库,名为Interfaces,在这个项目中添加接口IPrintableObject

Imports System.DrawingPublic Interface IPrintableObject    Sub Print()    Sub PrintPreview()    Sub RenderPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)End Interface

该接口主要用于可以打印的对象。(注意使用.Net提供的标准打印机制,添加引用System.Drawing,并加入Imports语句)


接口含Print、PrintPreview方法,分别提供不同的打印类型,RenderPage方法将对象的数据送至打印页。




关键类:ObjectPrinter

由于打印的代码都是通用的,如果将打印代码放入类Customer中,重用性不好,为了重用性,建立一个专门操作打印的类ObjectPrinter

因此在ObjectAndInterface项目中添加一个专门操作打印的类ObjectPrinter:

Imports System.DrawingImports System.Drawing.PrintingImports InterfacesPublic Class ObjectPrinter    Private WithEvents document As PrintDocument    Private printObject As IPrintableObject    Public Sub Print(ByVal obj As IPrintableObject)        printObject = obj        document = New PrintDocument        document.Print()    End Sub    Public Sub PrintPreview(ByVal obj As IPrintableObject)        Dim PPdlg As PrintPreviewDialog = New PrintPreviewDialog        printObject = obj        document = New PrintDocument()        PPdlg.Document = document        PPdlg.ShowDialog()    End Sub    Private Sub PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs) Handles document.PrintPage        printObject.RenderPage(sender, ev)    End SubEnd Class

注意:由于要用到接口,须右击ObjectAndCompoment项目添加引用,把接口Interfaces引入,并添加imports语句

            由于该类要操作打印,须将标准打印机制引入,同样右击ObjectAndCompoment项目添加引用,并添加Imports语句




上面接口有了,打印代码对象有了,下面再把接口安在Customer类里:

Imports InterfacesPublic Class Customer    Inherits Contact    Implements IPrintableObject    Private mPhone As String    Public Property Phone() As String        Get            Return mPhone        End Get        Set(value As String)            mPhone = value        End Set    End Property    Public Sub Print() Implements IPrintableObject.Print        Dim printer As New ObjectPrinter        printer.PrintPreview(Me)    End Sub    Public Sub PrintPreview() Implements IPrintableObject.PrintPreview        Dim printer As New ObjectPrinter        printer.PrintPreview(Me)    End Sub    Public Sub RenderPage(sender As Object, ev As Printing.PrintPageEventArgs) Implements IPrintableObject.RenderPage        Dim printFont As New Font("Arial", 10)        Dim lineHeight As Single = printFont.GetHeight(ev.Graphics)        Dim leftMargin As Single = ev.MarginBounds.Left        Dim yPos As Single = ev.MarginBounds.Top        ev.Graphics.DrawString("ID:" & ID.ToString, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())        yPos += lineHeight        ev.Graphics.DrawString("Name:" & Name, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())        ev.HasMorePages = False    End SubEnd Class

同样,上面代码中:使用了接口,因此右击ObjectAndCompoment项目,添加System.Drawing,并添加Imports语句。

注意:上面中的Me,是指调用时对象本身,类似C++的This指针。




下面在ObjectAndCompoment项目的窗体中添加按钮,并添加对应单击事件代码:

Imports InterfacesPublic Class Form1    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        Dim obj As New Customer        obj.Name = "douglas Adams"        CType(obj, IPrintableObject).PrintPreview()    End SubEnd Class




点击按钮,运行:





打印知识:http://blog.csdn.net/zsyzsj/article/details/1620949


本文件源码备份:http://download.csdn.net/detail/dzweather/5990109







原创粉丝点击