[AHK]面向对象开发基础教程

来源:互联网 发布:win7原版优化 编辑:程序博客网 时间:2024/06/07 14:51

AHK中的面向对象开发基础教程

本文将讲解AutoHotkey中如何使用类Class,即面向对象编程的一些基础知识:

在编程中使用类叫面向对象编程(Object Oriented Programming),希望你已有简单的了解。 “类”是具有相同属性和行为的一类事物。
在类中有两种定义:
-类
-实例

  • 什么是类
    假设你想建造一辆车,你必须有个蓝图。
    这蓝图就是类。在AutoHotkey中是这样的:
Class ClassName{}
  • 什么是实例
    想象你根据蓝图制造了一辆车,这辆车就是实例。
    所以,蓝图是类,汽车是实例。在AutoHotkey创建类的实例是这样的:
InstanceName:=New ClassName

每个类都有变量和方法
- 什么是类的变量
想象你的汽车的名字比如叫BMW,这是个类的变量(属性) 在AutoHotkey中这样表示:

CarName:=BMW

它还有4个轮子,同样也是属性,如是:

WheelsAmount:= 4
  • 什么是类的方法
    想象一下当你打方向盘时汽车会转向,这是个方法,在AutoHotkey里看起来是这样的:
TurnSteerWheel()

同样,你使用车钥匙解锁车,这也是类的方法:

CarOpenKey()

想象一下,你拟定了蓝图,里面有所有的变量何方法的定义,现在你想建造一个汽车,在AutoHotkey中就是这样的:

;首先,我们要声明一个类:Class Car{CarName:="BMW"WheelsAmount:=4TurnSteerWheel(){Some Commands here...}CarOpenKey(){Some Commands here...}}; 然后创建一个类的实例起名叫BMWConceptBMWConcept:=New Car

这是非常基础的,让我们用AutoHotkey来创建一些类。
来创建一个简单的窗口类。窗口有很多属性:窗口的标题、窗口ID、位置、大小等等这些是类的变量。
我们还可以让窗口有许多动作,比如:激活它、获取标题、获取它的位置等等动作,这些就是类的方法。
我们简单的创建一个记事本窗口类,只有一个变量和一个方法:

#SingleInstance force;Declare Class声明类Class Window{Title:="Untitled - Notepad";Variable Title 标题是一个类变量Activate();Activates window with Title - This.Title 激活窗口是类方法{    IfWinExist, % This.Title        WinActivate    else        MsgBox % "There is no Window: "This.Title "`nPleas Run Notepad!"}};Create Instance创建类实例SomeWin:=New WindowReturn!^a::;Hotkey to run Instance MethodSomeWin.Activate()Return

现在呢,我们创建一个方法方便我们自动获得窗口的属性 ,还有一些其它方法。

#SingleInstance force;Declare Class声明类Class Window{Get(){    WingetTitle TitleVar, A ; Get title from Active window.从激活的窗口得到标题    This.Title:=TitleVar ; Set TitleVar to This.Title将Title设置为TitleVar    WinGet IDVar,ID,A ; Get ID from Active window.从激活窗口获得窗口ID    This.ID:=IDVar ; Set IDVar to This.ID将ID设置为IDVar}Activate();Activates window with Title - This.Title{    ;MsgBox % This.ID    WinActivate % "ahk_id "This.ID ;Use word "This" when you reffer to    variable of this Class.当你引用类变量需要用This.    Return This.ID}AnnounceWinProps();Create message box with title and ID创建一个对话框显示标题和ID{    MsgBox % "Title is: "This.Title "`n ID is: "This.ID}};Create Instance创建类的实例SomeWin:=New Windowreturn^!a::;Hotkey to run Instance Method热键来调用实例方法IfWinExist, ahk_class Notepad    WinActivate ahk_class Notepadelse    {        MsgBox % "There is no Window: Notepad.`nPleas Run Notepad!"        exit    }SomeWin.get()SomeWin.AnnounceWinProps()Return^!s::If SomeWin.ID    SomeWin.Activate()else    MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!return
  • 添加了什么
    1.添加了get()方法,该方法从当前激活的窗口返回窗口标题和窗口ID
    2.移除了类变量Title,因为我们可以使用get()获取到标题信息
    3.修改了Activate方法,现在使用的不是窗口标题而是窗口ID
    4.增加了AnnounceWinProps()方法,它将输出Title和ID(由get方法返回的)
    5.修改了热键^!a,新版本是先激活Notepad,然后运行get方法。
    6.增加乐热键^!s,这个热键是用方法Activate()来激活窗口,如果ID是True(也就是有返回值情况)

现在让我们用嵌套类给这个类添加两个变量。
- 什么是嵌套类
嵌套类就是一个类在另一个类里面。

#SingleInstance force;Declare ClassClass Window{    Class Tmp{        A:=1        Static B:=1    }    Get()    {        WingetTitle TitleVar, A ; Get title from Active window.        This.Title:=TitleVar ; Set TitleVar to This.Title        WinGet IDVar,ID,A ; Get ID from Active window.        This.ID:=IDVar ; Set IDVar to This.ID    }    Activate() ;Activates window with Title - This.Title    {         IfWinExist, % "ahk_id "This.ID            WinActivate % "ahk_id " This.ID        else            MsgBox % "There is no Window with ID: "This.ID    }      AnnounceWinProps() ;Create message box with title and ID    {        MsgBox % "Title is: " This.Title "`n ID is: " This.ID    }};Create InstanceSomeWin:= New Windowreturn^!a:: ;Hotkey to run Instance MethodWinActivate ahk_class NotepadSomeWin.get()SomeWin.AnnounceWinProps()Return^!s::If SomeWin.ID    SomeWin.Activate() else    MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!return^!d::MsgBox % "SomeWin.Tmp.A = "SomeWin.Tmp.A "`nSomeWin.Tmp.B = "SomeWin.Tmp.Breturn

上面可以看到,可以像这样访问嵌套类中的变量:

BaseClass.NestedClass.Var 

也可以像这样访问嵌套类中的方法:

BaseClass.NestedClass.Method()

现在,如果你按下 “^!d”,您将看到消息 SomeWin.Tmp.A = Nothing. 为什么?
让我们看我们的变量声明:

Class Tmp{    A:=1    Static B:=1}

正如你所看到的这样,一个简单声明变量,这种变量——实例变量。 实例变量 是类的每个实例都拥有独立的副本(实例是从类派生的每个对象)。 他们如同普通赋值一样被声明。实例变量仅在实例创建时分配给实例。 我们要用嵌套类Window.Tmp创建实例,需要如下所示:SomeWin1:= New Window.Tmp。 创建实例后,变量A才可用:SomeWin1.A
我们转到变量B,它前面有“Static”关键字,这种变量 ——静态变量(也可以叫类变量区别于实例变量)。 这个变量属于类本身,所以这就是为什么我们可以访问它,弹出的消息框也的确显示B=1。
现在我们来讨论类继承。
-什么是类继承
想象你有类A,它有Var1和Var2。 您创建了B类,并且您希望此类从A派生所有内容。即具有与A类相同的变量和方法。
我们如下创建它:

Class A{    Static Var1:=1    Static Var2:=2}Class B extends A{    Var3:=3}NewClass:= New BMsgBox % "Var1=" NewClass.Var1 "`nVar2=" NewClass.Var2 "`nVar3=" NewClass.Var3return

你可以看到Class B从A类继承所有内容并添加自己的实例变量 Var3。
这叫类继承。

让我们回到我们的记事本项目。 让我们创建一个新的Notepad类,并使它从Window类继承所有内容,并添加一些新的方法。

#SingleInstance force;Declare ClassClass Window{    Class Tmp{        A:=1        Static B:=1    }    Get()    {        WingetTitle TitleVar, A ; Get title from Active window.        This.Title:=TitleVar ; Set TitleVar to This.Title        WinGet IDVar,ID,A ; Get ID from Active window.        This.ID:=IDVar ; Set IDVar to This.ID    }    Activate() ;Activates window with Title - This.Title    {         IfWinExist, % "ahk_id "This.ID            WinActivate % "ahk_id " This.ID        else            MsgBox % "There is no Window with ID: "This.ID    }      AnnounceWinProps() ;Create message box with title and ID    {        MsgBox % "Title is: " This.Title "`n ID is: " This.ID    }}Class Notepad extends Window{    Run()    {        IfWinNotExist ahk_class Notepad            Run Notepad        Else             WinActivate    }   };Create InstanceSomeWin:= New WindowSomeNot:= New Notepadreturn^!a:: ;Hotkey to run Instance MethodSomeNot.Run()SomeNot.get()SomeNot.AnnounceWinProps()Return^!s::If SomeWin.ID    SomeWin.Activate() else    MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!return^!d::MsgBox % "SomeWin.Tmp.A = "SomeWin.Tmp.A "`nSomeWin.Tmp.B = "SomeWin.Tmp.Breturn

您可以看到我们添加了一个新类Notepad,它扩展自类Window,并创建一个Notepad类的实例SomeNot。

还要看看热键 ^!a 您可以看到实例SomeNote使用类Window中的所有继承的方法。

最后…
假设创建实例时,我们需要一些动作。 初始化那些动作我们将使用:__New函数,下面是它的用法:

Class A{    __New(){        Some commands here...       }}

再回到我们的脚本,这是现在Notepad 类的样子:

#SingleInstance force;Declare ClassClass Window{    Class Tmp{        A:=1        Static B:=1    }    Get()    {        WingetTitle TitleVar, A ; Get title from Active window.        This.Title:=TitleVar ; Set TitleVar to This.Title        WinGet IDVar,ID,A ; Get ID from Active window.        This.ID:=IDVar ; Set IDVar to This.ID    }    Activate() ;Activates window with Title - This.Title    {         IfWinExist, % "ahk_id "This.ID            WinActivate % "ahk_id " This.ID        else            MsgBox % "There is no Window with ID: "This.ID    }      AnnounceWinProps() ;Create message box with title and ID    {        MsgBox % "Title is: " This.Title "`n ID is: " This.ID    }}Class Notepad extends Window{    __New()    {           This.Run()        This.get()    }    Run()    {        IfWinNotExist ahk_class Notepad            Run Notepad        Else             WinActivate    }   };Create InstanceSomeWin:= New WindowSomeNot:= New Notepadreturn^!a:: ;Hotkey to run Instance Method;SomeNot.Run();SomeNot.get();SomeNot.AnnounceWinProps()Return^!s::If SomeNot.ID    SomeNot.Activate() else    MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!return^!d::MsgBox % "SomeWin.Tmp.A = "SomeWin.Tmp.A "`nSomeWin.Tmp.B = "SomeWin.Tmp.Breturn

你可以看到__New函数被添加在记事本类中。这段代码
使得创建SomeNot实例时,它执行方法Run()和Get()
现在我们不需要从热键运行它们,看热键^!a ::,我们不再需要那些动作了。 现在我们可以直接用^!s来激活记事本了。

还有个__Del函数,类似__New函数它可以在实例删除时起作用,这里就不细讲了。

让我们做最后的再体会下,我们来创建Calc类,继承自Window类,我们还会添加到它的Run( )方法。

#SingleInstance force;Declare ClassClass Window{    Class Tmp{        A:=1        Static B:=1    }    Get()    {        WingetTitle TitleVar, A ; Get title from Active window.        This.Title:=TitleVar ; Set TitleVar to This.Title        WinGet IDVar,ID,A ; Get ID from Active window.        This.ID:=IDVar ; Set IDVar to This.ID    }    Activate() ;Activates window with Title - This.Title    {         IfWinExist, % "ahk_id "This.ID            WinActivate % "ahk_id " This.ID        else            MsgBox % "There is no Window with ID: "This.ID    }      AnnounceWinProps() ;Create message box with title and ID    {        MsgBox % "Title is: " This.Title "`n ID is: " This.ID    }}Class Notepad extends Window{    __New()    {           This.Run()        This.get()    }    Run()    {        IfWinNotExist ahk_class Notepad            Run Notepad        Else             WinActivate    }   }Class Calc extends Window{    __New()    {           This.Run()        This.get()    }    Run()    {        IfWinNotExist ahk_class CalcFrame            Run Calc        Else             WinActivate    }   };Create InstanceSomeWin:= New WindowSomeNot:= New NotepadSomeCalc:= New Calcreturn^!a:: ;Hotkey to run Instance Method;SomeNot.Run();SomeNot.get();SomeNot.AnnounceWinProps()Return^!s::If SomeNot.ID    SomeNot.Activate() else    MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!return^!d::If SomeCalc.ID    SomeCalc.Activate() else    MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!return

正如你所看到的,我们添加了Run( )方法,所以现在当我们创建实例SomeCalc时,会启动计算器。

希望这些基础,能让你学会使用AHK的类。
英文好的请猛戳