返回列表 发帖

[原创] vbs中如何自建类?

前言: 常常听说这样一句话---对象是类的实例。 对象的属性,对象的方法。一会有括号,一会又没括号,乱乱的。
         如果你亲自动手建个类,就能体会的到这句话的真实含义。
vbs中可以自己建类,并设置获得类的属性方法,看下面的代码-----
1-建立类的f方法
class my
    sub f
      wscript.echo 123
    end sub
end class
set w=new my
w.f  '回显123COPY
2--建立类的u属性,类中的变量就是属性
class my
  dim u
end class
set w=new my
w.u=123
wscript.echo w.u  '回显123COPY
3--property get  过程,这个过程比property let 或property set过程好用
class my
    property get u(x,y)  
       u=x+y
       wscript.echo u
    end property
end class
set w=new my
w.u 3,7 '回显10COPY
4---property let 过程,该过程可以独立使用,但是不如配合get过程好用
所以通常都配合get过程使用。
class my
    property get u(x)  
       v(x)=z   '这是调用let过程的方法
       u=z
       wscript.echo u
    end property
    property let v(x,z)  
      z=x*2
     end property
end class
set w=new my
w.u 7 '回显14COPY
5--property set 过程,用法与上类似,特别注意调用方法
class my
    property get u(x)  
       v(x)=z   '这是调用set过程的方法
       u=z
       wscript.echo u
    end property
property set v(x,z)  
      z=x*2
     end property
end class
set w=new my
w.u 10 '回显20COPY
最后,get,let,set都可以设置属性,那个方便就用那个。

[ 本帖最后由 myzam 于 2011-3-6 15:34 编辑 ]

回复 1楼 的帖子

'4.property set 该方法在格式上和let方法一样,但是这是处理对象属性的过程。
'-----
class test
   property get g(x)
        f(x)=ref
        g=ref
   end property
   property set f(x,ref)
     ref=x.specialfolders("desktop")
   end property
   
end class
set my=new test
set h=createobject("wscript.shell")
msgbox my.g(h)COPY

TOP

本帖最后由 myzam 于 2011-3-27 10:32 编辑

2# myzam
class my
  property get g(x)
    k(x)=method
  end property
  property set k(x,method)
     set o=createobject("wscript.shell")
     method=o.run("calc")
  end property
end class
set nc=new my
nc.g(o)COPY
class my
  property get g
    k()=method ' 这里必须要括号,否则被理解为变量赋值let
  end property
  property set k(method)
     set o=createobject("wscript.shell")
     method=o.run("calc")
  end property
end class
set nc=new my
nc.gCOPY
class my
  property get g(x)
    k(x)=method
  end property
  property set k(x,method)
     set o=createobject("wscript.shell")
     method=o.run(x)
  end property
end class
set nc=new my
nc.g ("calc.exe") '或nc.g "calc"
COPY

TOP

返回列表