3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AutoHotkey でクラスを使う

Posted at

定義

AutoHotkey ではクラスを以下のように定義します。

AutoHotkey
class pos{
    __New(x=0, y=0){
        this.x := x
        this.y := y
    }
}

これでメンバ変数 x, y を持つクラス pos が定義できました。__New はコンストラクタであり、インスタンスが生成される度に呼ばれます。インスタンス自身にアクセスするには this を使います。
次にインスタンスを生成します。

AutoHotkey
p := new pos(1, 2)
msgbox, % p.x ; -> 1
msgbox, % p.y ; -> 2

インスタンスは、変数 := new クラス名() の形で生成できます。

メソッド

メソッドを宣言します。

AutoHotkey
class pos{
    __New(x=0, y=0){
        this.x := x
        this.y := y
    }
    get_angle(){
        return atan(this.y / this.x)
    }
    get_distance(){
        return (this.x ** 2 + this.y ** 2) ** (1 / 2)
    }
}

p := new pos(1, 1)
text := ""
text .= p.x . "`n"
text .= p.y . "`n"
text .= p.get_angle() . "`n"
text .= p.get_distance() . "`n"
msgbox, %text%

; 1
; 1
; 0.785398
; 1.414214

メソッドは インスタンス.メソッド名() の形で呼ぶことができます。インスタンス自身にアクセスする際には this を使います。

継承

継承もできます。

AutoHotkey
class pos{
    __New(x=0, y=0){
        this.x := x
        this.y := y
    }
    get_distance(){
        return (this.x ** 2 + this.y ** 2) ** (1 / 2)
    }
}

class 3pos extends pos{
    __New(x=0, y=0, z=0){
        base.__New(x, y)
        this.z := z
    }
    get_distance(){
        return (this.x ** 2 + this.y ** 2 + this.z ** 2) ** (1 / 2)
    }
}

p := new pos(1, 2)
3p := new 3pos(3, 4, 5)
text := ""
text .= p.get_distance() . "`n"
text .= 3p.get_distance() . "`n"
msgbox, %text%

;2.236068
;7.071068

継承は、class 継承先クラス名 extends 継承元クラス名 と記述することで行うことができます。継承元クラスには base でアクセスできます。

ゲッター・セッター

メンバ変数を取得・メンバ変数に代入する際に特殊な処理をかませることができます。

AutoHotkey
class pos{
    __New(x=0, y=0){
        this.x := x
        this.y := y
    }
    __Get(name){
        if (name == "distance"){
            return (this.x ** 2 + this.y ** 2) ** (1 / 2)
        }else if (name == "angle"){
            return atan(this.y / this.x)
        }else{
            Object.__Get(this, name)
        }
    }
    __Set(name, value){
        if (name == "distance"){
            angle := this.angle
            this.x := cos(angle) * value
            this.y := sin(angle) * value
            return
        }else if (name == "angle"){
            distance := this.distance
            this.x := cos(value) * distance
            this.y := sin(value) * distance
            return
        }else{
            Object.__Set(this, name, value)
        }
    }
}

p := new pos(1, 1)

text := ""
text .= p.x . "`n"
text .= p.y . "`n"
text .= p.distance . "`n"
text .= p.angle . "`n"

p.distance := 5

text .= p.x . "`n"
text .= p.y . "`n"
text .= p.distance . "`n"
text .= p.angle . "`n"

p.angle := 3.14

text .= p.x . "`n"
text .= p.y . "`n"
text .= p.distance . "`n"
text .= p.angle . "`n"

msgbox, %text%

; 1
; 1
; 1.414214
; 0.785398
; 3.535534
; 3.535534
; 5.000000
; 0.785398
; -4.999994
; 0.007963
; 5.000000
; -0.001593

atan の仕様上最後の angle が誤っていますが、本来存在しないメンバ変数 distance, angle への参照、代入が __Set, __Get によって可能になっています。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?