前提

   前几天讲课一直强调《橘子苹果》的实例,再上之前的学习只是单纯的实现代码,含义没有深究,现在回想起一片空白。最近看软工视频里面有继承,对象,类等内容,所以温故而新!重新敲实例《橘子苹果》。。。

   这个实例中主要用到类模块,何为类模块?

   使用类模块创建含有方法和属性代码的自己的对象,在这个例子中创建Fruit类模块(接口),并在其中定义Fruit的属性和方法。Apple类和Orange类实现接口Fruit,FruitBox中定义所需要求的功能。一张图胜过千言万语。

【VB学习】——再学橘子苹果_赋值

   继承

隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别;将抽象得到的数据和行为(或功能)相结合,形成一个有机的整体,也就是将数据与操作数据的源代码进行有机的结合,形成“类”,其中数据和函数都是类的成员。在这个例子中Fruit类,就是封装,只对Apple和orange公开了接口,隐藏了其实现细节。

   多态

在面向对象语言中,接口的多种不同的实现方式即为多态。作用:把不同的子类对象都当作父类来看,可以屏蔽不同子类对象之间的差异,写出通用的代码,做出通用的编程,以适应需求的不断变化。赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作。也就是说,父亲的行为像儿子,而不是儿子的行为像父亲。回归程序:你造个篮子 FruitBox();参数是水果 fruit,这样你调用的时候就可以这样 FruitBox(fruit f);然后往里放苹果啊,梨啊,什么都行,只要是水果类的子类。这样的好处增加了程序的可扩展性。

特此附上源码,这一次还是很不足,有是看着源码敲的遗憾,个人觉得肯定不是一次的,争取完全实践!

Fruit类


<span style="font-family:KaiTi_GB2312;font-size:24px;">Option Explicit

Public Property Get CurWeight() As Double '方法:统计当前的重量
End Property

Public Function ReduceWeight() As Double '方法:统计减少多少重量
End Function
</span>


Apple类


<span style="font-family:KaiTi_GB2312;font-size:24px;">Option Explicit

Implements Fruit '实现接口类

Private mvarCurWeight As Double '定义当前重量的变量

Private mvarTotalWeight As Double '定义总质量的变量



Private Sub Class_Initialize() '变量初始化
mvarTotalWeight = 50 '苹果的初始化重量为50,当前的重量为50
mvarCurWeight = mvarTotalWeight

End Sub

Private Property Get Fruit_CurWeight() As Double '写入当前苹果的质量
Fruit_CurWeight = mvarCurWeight

End Property
Private Function Fruit_ReduceWeight() As Double '苹果的重量每天减少4,直到是原始重量的3/5质量不在减少
Dim oldWeight As Double '定义旧重量的变量
oldWeight = mvarCurWeight '将减少前的质量赋给旧的重量值

mvarCurWeight = mvarCurWeight - 4
If (mvarCurWeight < mvarTotalWeight * 3 / 5) Then
mvarCurWeight = mvarTotalWeight * 3 / 5
End If
Fruit_ReduceWeight = oldWeight - mvarCurWeight '返回值是减少的重量=旧重量-新重量

End Function</span>


Orange类


<span style="font-family:KaiTi_GB2312;font-size:24px;">Option Explicit

Implements Fruit

Private mvarCurWeight As Double '当前重量的变量
Private mvarTotalWeight As Double '定义总重量的变量



Private Sub Class_Initialize() '初始化变量
mvarCurWeight = 30 '橘子的初始化变量为30,当前的质量为30
mvarTotalWeight = mvarCurWeight

End Sub

Private Property Get Fruit_CurWeight() As Double '写入橘子当前重量
Fruit_CurWeight = mvarCurWeight

End Property
Private Function Fruit_ReduceWeight() As Double '橘子的质量每天减少3,直到总质量为原来质量的3/5后,不在减少

Dim oldWeight As Double '定义旧重量的变量
oldWeight = mvarCurWeight '将减少前的质量赋给旧的重量值

mvarCurWeight = mvarCurWeight - 3
If (mvarCurWeight < mvarTotalWeight * 3 / 5) Then
mvarCurWeight = mvarTotalWeight * 3 / 5

End If
Fruit_ReduceWeight = oldWeight - mvarCurWeight '返回值是减少的重量=旧重量-新重量
End Function
</span>


FruitBox类


<span style="font-family:KaiTi_GB2312;font-size:24px;">Option Explicit
Private mcol As Collection '定义集合,元素是水果

Public Sub AddFruit(afruit As Fruit)
mcol.Add afruit '向盒子中添加一个水果
End Sub

Public Function TotalFruitWeight() As Double '当前盒子的总质量
Dim afruit As Fruit
Dim total As Double
total = 0
For Each afruit In mcol
total = total + afruit.CurWeight
Next
TotalFruitWeight = total '返回当前盒子的总质量

End Function
Public Function PassOneDay() As Double '过了一天的总质量
Dim afruit As Fruit
Dim total As Double

total = 0
For Each afruit In mcol
total = total + afruit.ReduceWeight
Next
PassOneDay = total '返回一天后的质量


End Function
Public Function NumOfApples() As Long '统计苹果的个数
Dim afruit As Fruit
Dim count As Long

count = 0
For Each afruit In mcol
If (TypeName(afruit) = "Apple") Then
count = count + 1
End If
Next
NumOfApples = count

End Function
Public Function NumOfOranges() As Long '统计橘子的个数
Dim afruit As Fruit
Dim count As Long

count = 0
For Each afruit In mcol
If (TypeName(afruit) = "Orange") Then
count = count + 1
End If
Next
NumOfOranges = count

End Function

Private Sub Class_Initialize()
Set mcol = New Collection '初始化集合建立

End Sub

Private Sub Class_Terminate()
ReleaseFruits '清空内存,释放对象集合
Set mcol = Nothing
End Sub
Private Sub ReleaseFruits() '释放对象
Dim afruit As Fruit
For Each afruit In mcol
Set afruit = Nothing
Next

End Sub
</span>


frmMain运行窗体


<span style="font-family:KaiTi_GB2312;font-size:24px;">Option Explicit

Dim Box As Fruitbox '定义一个盒子类型
Private Sub cmdApple_Click() '添加一个苹果
Box.AddFruit New Apple
End Sub
Private Sub cmdOrange_Click() '添加一个橘子
Box.AddFruit New Orange
End Sub
Private Sub cmdPassDay_Click() '过了一天统计
Dim str As String '定义字符串变量
Dim Old As Double '定义旧重量变量
Dim Reduce As Double '定义损失重量变量
Dim Now As Double '定义新重量变量

Old = Box.TotalFruitWeight() '调用FruitBox类的TotalFruitWeight方法
str = "一天前总重量有:" & Old
LstShow.AddItem str

str = "苹果有:" & Box.NumOfApples() '调用FruitBox类的NumOfApples方法
LstShow.AddItem str

str = "橘子有:" & Box.NumOfOranges() '调用FruitBox类的NumOfOranges方法
LstShow.AddItem str

Reduce = Box.PassOneDay '调用FruitBox类的PassOneDay方法
str = "一天损失重量:" & Reduce
LstShow.AddItem str

Now = Box.TotalFruitWeight() '调用FruitBox类的TotalFruitWeight方法
str = "当前重量:" & Now
LstShow.AddItem str
LstShow.AddItem ""
End Sub
Private Sub Form_Load() '创建一个盒子对象
Set Box = New Fruitbox
End Sub
Private Sub Form_Unload(Cancel As Integer) '释放一个盒子对象
Set Box = Nothing
End Sub
</span>

运行窗体

【VB学习】——再学橘子苹果_初始化_02

【总结】

     昨天听完社合师哥讲的经验,最大的感受的学习过程要踏实,没有什么技术是高大上,脚踏实地一步一步的走好,想要不成功都难。所以学习的过程中及时总结,颗粒归仓,新旧知识结合起来,构建自己的知识体系,应该会达到更好的效果。