其他分享
首页 > 其他分享> > AutoHotkey里prototype(原型)和class的关系

AutoHotkey里prototype(原型)和class的关系

作者:互联网

AutoHotkey v2-beta 可能是参考 javascript 用了 prototype 的定义,和面向对象语言的 class 类似。
之前一直没搞清楚,今天看了Promise从入门到自定义突然明白。
见下方示例和注释文字

class Person {
    static count := 0 ;类属性
    name := "" ;实例属性

    static add() { ;类方法
        Person.count++
        return Person.count
    }

    run() { ;实例方法
        return this.name . " is runing"
    }
}

p := Person()
p.name := "john"
Person.prototype.eat := (o)=>(o.name . " is eating") ;类似上面定义的 run 实例方法
p.base.shop := (o)=>(o.name . " is shopping") ;改用实例 p 定义方法,效果同上
Person.reduce := (p*) =>(Person.count--, Person.count) ;类似上面定义的 add 类方法
/* msgbox 结果:
john is running
john is eating
john is shopping
1
0
*/
msgbox(p.run() . "`n" . p.eat() . "`n" . p.shop() . "`n" . Person.add() . "`n" . Person.reduce())

标签:count,john,run,AutoHotkey,Person,实例,prototype,class,name
来源: https://www.cnblogs.com/hyaray/p/15855954.html