Dim amount As Double Dim height As Double Dim a As New FruitTree( "peach", 8, 2.1, 3, 20 ) amount = a.computeAmount( 25 ) height = a.inquireHeight() Messagebox.Show("Amount= " + str(amount) + "Height= " + str(height)) End Sub
此程序中﹐对象 a首先接到一讯息──computeAmount(25)﹐对象a 就会激活其内部的computeAmount()函数﹐此函数就拿25和对象中的价格20相乘﹔函数计算完毕得到 500﹔对象 a就将其送出来。
计算机再将 500存入amount变量之中﹐则amount之值是 500。接下来﹐对象 a又接到另一讯息──inquire_height()﹐对象 a激活其内部的inquire_height()函数﹐此函数就读取对象内的「高度」──2.1 ﹔且把 2.1送出来。
计算机将 2.1存入height变量之中﹐则height之值为 2.1。最后﹐计算机把amount及height变量之值显示于屏幕上── Amount = 500 Height = 2.1
7. 类别之定义
一只猫是对象(Object)﹐不同的猫其大小、颜色各不相同﹐但是它们有很多相同之行为﹐您才认为它们是猫﹐也才将它们归于同一类﹔譬如喜欢捉老鼠、善于爬树、怕水等等。因之归于同类别之对象具有共同的行为(Behavior)﹐亦即各对象对讯息(Message) 之反应过程是相同的﹐这反应过程可由同一函数担任。例如﹐下述指令产生 a及 k两对象──
Dim a As New FruitTree("peach", 8, 2.1, 3, 20) Dim k As New FruitTree("pineapple", 2, 0.5, 6, 10)
假设其内部资料为﹕
因 a及 k是同类别之对象﹐所以对同一种讯息的反应过程是一样的﹔但由于对象内部的资料并不相同﹐其反应的结果有所不同。
对于讯息──computeAmount(25)而言﹐对象 a会激活computeAmount()函数处理它﹔而对象 k也激活computeAmount()处理它﹐其处理过程是相同的。也就是说﹐对象 a和 k共享computeAmount()函数来处理这讯息。此computeAmount函数可写成﹕
Function computeAmount(重量) computeAmount = 重量 * 价格 End Function
于是﹐把这函数加入FruitTree类别中──
Class Tree 品种 年龄 高度 End Class
Class FruitTree Inheits Tree 成熟月份 价格 Function computeAmount(重量) computeAmount = 重量 * 价格 End Function End Class
Class Bamboo Inherits Tree 用途 End Class
如此﹐FruitTree类别之各对象都对computeAmount(25)讯息有所反应﹔各对象皆使用这computeAmount()函数来处理此讯息。您可认为各对象共享computeAmount()函数﹐也能认为每对象内皆有一份computeAmount()函数﹐以便处理这种讯息。接下来﹐继续将inquireHeight()函数加入FruitTree类别中──
Class Tree 品种 年龄 高度 End Class Class FruitTree Inherits Tree 成熟月份 价格 Function computeAmount(重量) computeAmount = 重量 * 价格 End Function Function inquireHeight() inquireHeight = 高度 End Function Class Bamboo Inherits Tree 用途 End Class
于是FruitTree类别之对象具有同样的行为──对computeAmount()和inquireHeight()讯息有所反应﹐且反应过程也相同﹐只是反应结果有所不同。写程序时﹐可认为FruitTree类别之对象共同享用computeAmount()及inquireHeight()函数。也可认为各对象内﹐各有一份computeAmount()函数及一份inquireHeight()函数﹐以便处理这两种讯息。
请看个主程序﹕
Protedted Sub Form1_Click( .... ) Dim amount As Double Dim height As Double Dim a As New FruitTree( "peach", 8, 2.1, 3, 20) Dim k As New FruitTree( "pineapple", 2, 0.5, 6, 10) amount = a.computeAmount( 25 ) height = a.inquireHeight() Messagebox.Show("Amount= " + str(amount) + "Height= " + str(height)) amount = k.computeAmount( 25 ) height = k.inquireHeight() Messagebox.Show("Amount= " + str(amount) + "Height= " + str(height)) End Sub
因FruitTree类别含有computeAmount()及inquireHeight()两函数﹐所以对象 a及 k皆能接受这两种讯息﹐并处理之。至于其它的讯息﹐因无函数来支持(处理)﹐FruitTree之对象无法接受之。您可继续把新函数加入FruitTree类别中﹐使其对象拥有更丰富之行为﹐亦即能接受更多样化之讯息。换句话说﹐对象之功能就更多了。上述程序输出如下﹕
Amount = 500 Height = 2.1 Amount = 250 Height = .5 8. 着手写 VB.net程序
上节之主程序中﹐我们是假设对象 a及 k之内部已有资料了。也许您想问道﹕如何把资料存入对象内呢﹖很简单﹗当您有这种需要时﹐就设计一个函数﹐并加入类别中﹐由它担任资料的输入工作。资料输入对象中﹐也是对象之行为﹐一旦您将此函数加入类别之后﹐对象就能接受讯息﹐并且处理它。以下的实际VB程序﹐让您了解详细的情形了。这程序叫oop01-01:
''''ex01.bas Imports System.ComponentModel Imports System.Drawing Imports System.WinForms ''''----------------------------------------------------------------- Class Tree Public varity As String Public age As Integer Public height As Double End Class
Class FruitTree Inherits Tree Public month As Integer Public price As Double Public Function computeAmount(ByVal weight As Single) As Double computeAmount = weight * price End Function Public Function inquireHeight() As Double inquireHeight = height End Function Public Sub SetPrice(ByVal pr As Double) price = pr End Sub Public Sub SetHeight(ByVal hei As Double) height = hei End Sub End Class
Class Bamboo Inherits Tree Public usage As String End Class ''''----------------------------------------------- Public Class Form1 &n 上一页 [1] [2] [3] [4] 下一页 [系统软件]14.5.10.1 Object creation expressions [Delphi程序]The Delphi Object Model (PART III) [Delphi程序]The Delphi Object Model (PART II) [Delphi程序]The Delphi Object Model (PART I) [Delphi程序]Object Pascal:从对象指针谈起 [Delphi程序]Object Pascal Style Guide [Delphi程序]Object TreeView简要说明 [Delphi程序]Object Pascal中String类型的内幕探讨 [Delphi程序]delphi学习笔记(2)-object pascal语言的语句 [Delphi程序]delphi学习笔记(1)-object pascal语言的数据类…
|