<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Свой get\set при доступе к ассоциированным массивам внутри класса]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=15095&amp;type=atom" />
	<updated>2019-12-13T06:51:47Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=15095</id>
		<entry>
			<title type="html"><![CDATA[AHK: Свой get\set при доступе к ассоциированным массивам внутри класса]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=136833#p136833" />
			<content type="html"><![CDATA[<p>Общий привет!<br />Вот модифицированный код и справки:<br /></p><div class="codebox"><pre><code>
red  := new Color(0xff0000) ; but not here
red.R := 30 ; how to do some special here??
cyan := new Color(0), cyan.G := 255, cyan.B := 255

MsgBox % &quot;red: &quot; red.R &quot;,&quot; red.G &quot;,&quot; red.B &quot; = &quot; red.RGB
MsgBox % &quot;cyan: &quot; cyan.R &quot;,&quot; cyan.G &quot;,&quot; cyan.B &quot; = &quot; cyan.RGB

class Color
{
    __New(aRGB)
    {
        this.init := 1
        this.RGB := aRGB
        this.init := 0
    }

    static Shift := {R:16, G:8, B:0}

    __Get(aName)
    {
        if (Color.init1 != 0)
        {
            MsgBox, % A_ThisFunc . aName
            ; NOTE: Using this.Shift here would cause an infinite loop!
            shift := Color.Shift[aName]  ; Get the number of bits to shift.
            if (shift != &quot;&quot;)  ; Is it a known property?
                return (this.RGB &gt;&gt; shift) &amp; 0xff
            ; NOTE: Using &#039;return&#039; here would break this.RGB.
        }
    }

    __Set(aName, aValue)
    {
        if (Color.init1 != 0)
        {
            MsgBox, % A_ThisFunc . aName . &quot;-&quot; . aValue
            if ((shift := Color.Shift[aName]) != &quot;&quot;)
            {
                aValue &amp;= 255  ; Truncate it to the proper range.

                ; Calculate and store the new RGB value.
                this.RGB := (aValue &lt;&lt; shift) | (this.RGB &amp; ~(0xff &lt;&lt; shift))

                ; &#039;Return&#039; must be used to indicate a new key-value pair should not be created.
                ; This also defines what will be stored in the &#039;x&#039; in &#039;x := clr[name] := val&#039;:
                return aValue
            }
            ; NOTE: Using &#039;return&#039; here would break this.stored_RGB and this.RGB.
        }
    }

    ; Meta-functions can be mixed with properties:
    RGB[] {
        get
        {
            ; Return it in hex format:
            return format(&quot;0x{:06x}&quot;, this.stored_RGB)
        }
        set
        {
            ; MsgBox, % &quot;Set RGB&quot; . this.stored_RGB
            return this.stored_RGB := value
        }
    }

    init[]
    {
        get
        {
            return this.stored_init
        }

        set
        {
            return this.stored_init := value
        }
    }
}

</code></pre></div><p>Объясню по-простому, в свой класс добавляются ассоциированные массивы. Получается так: <br /></p><div class="codebox"><pre><code>
myClass.someArray.arrayKey := &quot;testValue&quot; ; set
Msgbox, % myClass.someArray.arrayKey  ;get
</code></pre></div><p>Мне нужно выполнять еще некоторые действия, когда я обращаюсь к элементам класса как выше, но не при создании самого класса.</p>]]></content>
			<author>
				<name><![CDATA[Шикарная услуга плана]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=40534</uri>
			</author>
			<updated>2019-12-13T06:51:47Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=136833#p136833</id>
		</entry>
</feed>
