VBCOM TUTORIAL(2)

Object-based Programming

Introduction

COM programming is based on an object-oriented style of programming.  In VB, this means the use of classes and class-based object references.  If you are comfortable defining your own classes with logical properties and methods, and then using these classes to instantiate and manipulate objects, you can safely skip this section and continue the tutorial here.   Otherwise, let's begin! 

We will study object-based programming in the context of an example:  a form-based progress indicator class called CProgress.   Here's a picture of this class in action (you should see this for yourself by running the application /VBCOM/Demos/Class-based Progress Indicator/App.exe;   if you have not already done so, you can download the labs from the Setup page ):

oop-progress.gif

Note the naming convention --- classes always being with a C.  It will be helpful if you also think of the C as standing for a concrete class, i.e. a class that can be turned into a run-time object that fulfills some function.

First, a bit of terminology.  A class is compile-time entity that a programmers writes.  In VB, a class is produced by adding a Class Module to your project.   A class may contain zero or more members, where a member is either a property (think of it as a variable) or a method (subroutine or function).  At run-time, you instantiate a class to produce an object that resides in memory.   In fact, you can instantiate the same class over and over again, each time producing a distinct object.  Finally, to facilitate discussion, we will use the terms client and server to denote two objects that are interacting, in particular where the client object is accessing a property or invoking a method on the server object:

client_server.gif

Using the Class CProgress

The class is quite simple in design, having only one public property and two public methods:

 Public Value As Integer'** percentage done
 Public Sub Show()'** show the progress form
 Public Sub Hide()'** hide the progress form

When this class is instantiated into an object, the Value of the object represents the current status of the progress indicator, e.g. 40 (denoting 40%).  By changing this value, the client can show progress.  The object's methods, Show and Hide, are invoked by the client to draw and erase the progress indicator form, respectively.   Altogether, this list of public properties and methods denotes the interface between clients and CProgress servers. 

At this point, let's look at some sample client code which instantiates and uses the CProgress class.  If you haven't already, open the supplied VB project /VBCOM/Demos/Class-based Progress Indicator/App.vbp and view the code in the cmdProgress button of the main form:

 Dim progress As CProgress'** object reference to
  '**  CProgress instance
 Dim i As Integer, _  
    j As Variant 
 
 Set progress = _ 
    New CProgress'** new instance of CProgress

The first line declares an object reference variable (a pointer in C++ parlance) of type CProgress.  Initially, an object reference variable points to no object, and thus has the value Nothing.  The third line uses VB's New operator to instantiate an object of class CProgress, and then sets the object reference variable progress to this new instance.  At this point we have:

progress_instance.gif

The code continues by displaying the progress indicator form, and then executing a loop which simulates a long-running operation:

 progress.Show'** invoke Show method
 
 For i = 1 To 10
   progress.Value = _ 
     progress.Value + 10'** update by 10%
   For j = 1 To 1000000: Next j'** pause to simulate activity
 Next i

Each time through the loop, progress increases by 10%.  Finally, once the loop completes, the progress indicator form is erased from the screen and the progress variable reset to Nothing:

 progress.Hide'** invoke Hide method
 Set progress = Nothing'** explicitly destroy instance

This last step is not strictly necessary, since VB will reset the variable to Nothing automatically once it goes out of scope.  However, it is important to note that the number of references to an object determines the lifetime of that object, i.e. the duration it exists in memory.  By setting progress to Nothing we are explicitly reducing the reference count of that object by 1.  Once an object's count reaches 0, it is destroyed.  Thus, in the example above, the object is immediately destroyed after the Set statement is executed.


Implementing the Class CProgress

If you haven't done so already, open the supplied VB project /VBCOM/Demos/Class-based Progress Indicator/App.vbp and view the code in the Class Module CProgress (stored in the file CProgress.cls).  Using VB's browser (F2), the class is summarized as follows:

oop_browser.gif

The class actually contains two properties and four methods, although the first 3 listed above are private to the class (and thus accessible only to VB and the object itself).  The private property frm is used by the object to hold a reference to the Form instance containing the progress indicator.  This form is created in the method Class_Initialize, which is called automatically by VB whenever a new CProgress object is instantiated.  Likewise, the form is destroyed in the method Class_Terminate, called automatically when the object's reference count reaches 0.  Here's the relevant code from the class CProgress

 Option Explicit 
 
 Private frm As Form'** object reference to progress form
 
 Private Sub Class_Initialize()
   Set frm = New frmProgress'** create a new instance of frmProgres
   Me.Value = 0'** initialize our value to 0%
 End Sub
 
 Private Sub Class_Terminate()
   Unload frm'** destroy progress form instance
   Set frm = Nothing
 End Sub

In this case, the method Class_Initialize is called behind the scenes in response to the client executing New:

 Set progress = New CProgress'** create a new
  '**  instance of CProgress

Thus, when developing a class, use the Class_Initialize method to initialize the server for upcoming client use.  As for Class_Terminate, in this case the method is called when the client clears the last reference to the server:

 Set progress = Nothing'** explicitly destroy instance

Therefore, use the Class_Terminate method to cleanup (close files, destroy helper objects, etc.) before the server is gone for good. 

Now for the public interface of CProgressValue, Show and Hide.  Firstly, the Value property is not implemented using an integer variable, but instead is a logical property physically realized by appropriate Get and Let methods (look closely at the bottom pane of the browser window above).  Logical properties offer many benefits:   validation, the ability to take action when a property changes, read-only data, etc.  However, a stronger motivation is that COM simply does not allow a class to contain public data properties.  In a COM-compatible class, the only public members may be methods. 

Ignoring COM for the moment, a logical Value property makes sense in this situation since the server needs to update the progress indicator in response to a change in value.  The Let method is used to capture a write to the logical property, much like the Let statement in VBA assigns a new value to a variable.  Note that the new value is passed as a parameter to the method:

 Public Property Let Value(ByVal percentage As Integer) 
   frm.pbarProgress.Value = percentage'** update progress bar
   frm.lblPercentage.Caption = _ 
      CStr(percentage) & "%"'** update label
   frm.Refresh'** redraw form
 End Property

The new value is simply assigned to the underlying Windows 95 ProgressBar control on the progress form, as well as written to a label.  In order for the client to be able to read the current progress value, a corresponding Get method is also provided:

 Public Property Get Value() As Integer
  '** return current state of progress bar
   Value = frm.pbarProgress.Value
 End Property

Get methods are actually functions which return the current value of the logical property. 

The final two members of CProgress are the Show and Hide methods.  These are perhaps the easiest of the bunch, since their function is obvious:

 Public Sub Show()
   frm.Show'** show form and redraw
   frm.Refresh
 End Sub
 
 Public Sub Hide()
   frm.Hide'** erase form but leave form object in memory
 End Sub

That's it, the class CProgress is now completely implemented!


Classes vs. Instances

Before we begin our discussion of COM, let's review the notion of  classes, and instances of classes.  Consider the following client code:

 Dim p1 As CProgress, p2 As CProgress, p3 As CProgress
 
 Set p1 = New CProgress
 Set p2 = New CProgress
 Set p3 = New CProgress
 
 p1.Value = 40
 p2.Value = 5
 p3.Value = 99
 
 p1.Show
 p2.Show
 p2.Hide
 
 Stop'** pause client and activate the debugger

How many instances of CProgress exist in memory?  Three!  How many progress indicator forms exist in memory (i.e. instances of frmProgress)?  Three!  How many of these form instances are visible on the screen?  One!  Okay, now you're ready to start the first lesson on.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
GeoPandas是一个开源的Python库,旨在简化地理空间数据的处理和分析。它结合了Pandas和Shapely的能力,为Python用户提供了一个强大而灵活的工具来处理地理空间数据。以下是关于GeoPandas的详细介绍: 一、GeoPandas的基本概念 1. 定义 GeoPandas是建立在Pandas和Shapely之上的一个Python库,用于处理和分析地理空间数据。 它扩展了Pandas的DataFrame和Series数据结构,允许在其中存储和操作地理空间几何图形。 2. 核心数据结构 GeoDataFrame:GeoPandas的核心数据结构,是Pandas DataFrame的扩展。它包含一个或多个列,其中至少一列是几何列(geometry column),用于存储地理空间几何图形(如点、线、多边形等)。 GeoSeries:GeoPandas中的另一个重要数据结构,类似于Pandas的Series,但用于存储几何图形序列。 二、GeoPandas的功能特性 1. 读取和写入多种地理空间数据格式 GeoPandas支持读取和写入多种常见的地理空间数据格式,包括Shapefile、GeoJSON、PostGIS、KML等。这使得用户可以轻松地从各种数据源中加载地理空间数据,并将处理后的数据保存为所需的格式。 2. 地理空间几何图形的创建、编辑和分析 GeoPandas允许用户创建、编辑和分析地理空间几何图形,包括点、线、多边形等。它提供了丰富的空间操作函数,如缓冲区分析、交集、并集、差集等,使得用户可以方便地进行地理空间数据分析。 3. 数据可视化 GeoPandas内置了数据可视化功能,可以绘制地理空间数据的地图。用户可以使用matplotlib等库来进一步定制地图的样式和布局。 4. 空间连接和空间索引 GeoPandas支持空间连接操作,可以将两个GeoDataFrame按照空间关系(如相交、包含等)进行连接。此外,它还支持空间索引,可以提高地理空间数据查询的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值