51Testing软件测试论坛

标题: 又出错错错啦!!赵旭斌老师书中类的继承模式例子 [打印本页]

作者: yinjiajia0626    时间: 2014-8-5 16:44
标题: 又出错错错啦!!赵旭斌老师书中类的继承模式例子
本帖最后由 yinjiajia0626 于 2014-8-6 13:55 编辑

求大神看!出错图片[attach]91357[/attach]
不知道是不是书印刷错误,if和else里面的内容一样啊,都是 child(key)=super(key)
源代码如下:
  1. Class ClassHelper
  2.    Private dict
  3.           '添加方法
  4.           Public default function addMethod(name)
  5.                  Set dict(name)=getRef(dict("type")&name)
  6.           End Function
  7.           '创建字典对象
  8.         Private function CreateDict()
  9.            Set CreateDict=CreateObject("scripting.dictionary")
  10.         End Function

  11.         '复制字典对象
  12.         Private function CopyDict(super,child)
  13.            Dim key
  14.            For each key in super
  15.                    If isobject(super(key)) Then
  16.                            Set child(key)=super(key)
  17.                            Else
  18.                            child(key)=super(key)
  19.                    End If
  20.            Next
  21.            Set CopyDict=child
  22.         End Function

  23.         '新建一个类
  24.         Public function newClass(name)
  25.         Set dict=CreateDict()
  26.         dict("type")=name
  27.         Set newClass=dict
  28.         End Function

  29.         '继承方法
  30. Public function extends(super,childType)
  31.    Set dict=CopyDict(super,child)
  32.    Set dict("parent")=super
  33.    dict("type")=childType
  34.    Set extends=dict
  35. End Function

  36. '新建继承类
  37. Public function [new](super,arrParams)
  38.    Dim dict:set dict=CopyDict(super,CreateDict())
  39.    Call dict("initialize")(dict,arrParams)
  40.    Set [new]=dict
  41. End Function
  42. End Class

  43. '载入继承帮助类
  44. Dim Mercury:set Mercury=new ClassHelper

  45. '###################################################
  46. '创建一个AutoTestTools新类
  47. Dim AutoTestTools:Set AutoTestTools=Mercury.newClass("AutoTestTools")
  48. AutoTestTools("effect")="Automation Testing"
  49. Function AutoTestToolsInitialize(this,arrParams)
  50.    this("effect")=arrParams(0)
  51. End Function : Mercury "initialize"

  52. Function AutoTestToolsLaunch()
  53.    MsgBox "Launch AutoTestTools"
  54. End Function : Mercury "Launch"
  55. ''End Class
  56. '####################################################

  57. '####################################################
  58. '创建一个新继承AutoTestTools的类
  59. Dim QuickTest : set QuickTest=Mercury.extends(AutoTestTools,"QuickTest")
  60. Quicktest("name")="Quick Test Professional"
  61. QuickTest("support")="all"

  62. Function  QuickTestInitialize(this,arrParams)
  63.    this("support")=arrParams(0)

  64. End Function : QuickTest "initialize"

  65. Function QuickTestLaunch()
  66.    MsgBox "Launch QuickTest"
  67. End Function : Mercury "Launch"
  68. ''End Class
  69. '####################################################

  70. '初始化QuickTest类
  71. Dim QuickTestCls: set QuickTestCls=Mercury.new(QuickTest,array("web"))

  72. '打印support数学,此时已经被初始化为web
  73. MsgBox QuickTestCls("support")

  74. '执行QuickTest类中的Launch方法
  75. Call QuickTestCls("Launch")()

  76. '执行QuickTest类的父类的Launch方法
  77. Call QuickTestCls("parent")("Launch")
复制代码

作者: 赵佳乐SMILE    时间: 2014-8-6 14:12
高大上
作者: yinjiajia0626    时间: 2014-8-9 08:46
不要沉啊,问题还没有解决呢
作者: Shawn_xiao    时间: 2014-8-12 12:51
这本书刊误确实不少:1.CopyDict中需要加Set child = CreateObject("scripting.dictionary")
2.function QuickTestInitialize 后面的QuickTest "initialize"应该改成Mercury "Initialize" 才能那个将前面的方法加进字典
作者: yinjiajia0626    时间: 2014-8-14 10:02
回复 4# Shawn_xiao

太谢谢你啦!!




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2