【VB学习】——再学橘子苹果

【VB学习】——再学橘子苹果,第1张

概述   前提    前几天讲课一直强调《橘子苹果》的实例,再上之前的学习只是单纯的实现代码,含义没有深究,现在回想起一片空白。最近看软工视频里面有继承,对象,类等内容,所以温故而新!重新敲实例《橘子苹果》。。。    这个实例中主要用到类模块,何为类模块?    使用类模块创建含有方法和属性代码的自己的对象,在这个例子中创建Fruit类模块(接口),并在其中定义Fruit的属性和方法。Apple类和

前提

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

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

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


继承

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

多态

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

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

Fruit类

<span >Option ExplicitPublic Property Get CurWeight() As Double   '方法:统计当前的重量End PropertyPublic Function ReduceWeight() As Double    '方法:统计减少多少重量End Function</span>

Apple类

<span >Option ExplicitImplements Fruit   '实现接口类Private mvarCurWeight As Double '定义当前重量的变量Private mvarTotalWeight As Double '定义总质量的变量Private Sub Class_Initialize()    '变量初始化    mvarTotalWeight = 50          '苹果的初始化重量为50,当前的重量为50    mvarCurWeight = mvarTotalWeightEnd SubPrivate Property Get Fruit_CurWeight() As Double '写入当前苹果的质量    Fruit_CurWeight = mvarCurWeight    End PropertyPrivate 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 >Option ExplicitImplements FruitPrivate mvarCurWeight As Double  '当前重量的变量Private mvarTotalWeight As Double '定义总重量的变量Private Sub Class_Initialize()    '初始化变量    mvarCurWeight = 30            '橘子的初始化变量为30,当前的质量为30    mvarTotalWeight = mvarCurWeight    End SubPrivate Property Get Fruit_CurWeight() As Double  '写入橘子当前重量    Fruit_CurWeight = mvarCurWeight    End PropertyPrivate 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 >Option ExplicitPrivate mcol As Collection   '定义集合,元素是水果Public Sub AddFruit(afruit As Fruit)    mcol.Add afruit            '向盒子中添加一个水果End SubPublic 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 FunctionPublic 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 FunctionPublic 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 FunctionPublic 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 FunctionPrivate Sub Class_Initialize()    Set mcol = New Collection         '初始化集合建立    End SubPrivate Sub Class_Terminate()    ReleaseFruits                     '清空内存,释放对象集合    Set mcol = nothingEnd SubPrivate Sub ReleaseFruits()           '释放对象    Dim afruit As Fruit    For Each afruit In mcol        Set afruit = nothing    Next    End Sub</span>

frmMain运行窗体

<span >Option ExplicitDim Box As FruitBox                             '定义一个盒子类型Private Sub cmdApple_Click()                 '添加一个苹果    Box.AddFruit New AppleEnd SubPrivate Sub cmdOrange_Click()                '添加一个橘子    Box.AddFruit New OrangeEnd SubPrivate 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 SubPrivate Sub Form_Load()                         '创建一个盒子对象    Set Box = New FruitBoxEnd SubPrivate Sub Form_Unload(Cancel As Integer)      '释放一个盒子对象    Set Box = nothingEnd Sub</span>

运行窗体


【总结】

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

总结

以上是内存溢出为你收集整理的【VB学习】——再学橘子苹果全部内容,希望文章能够帮你解决【VB学习】——再学橘子苹果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1269227.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存