<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: Свой get\set при доступе к ассоциированным массивам внутри класса]]></title>
		<link>https://forum.script-coding.com/viewtopic.php?id=15095</link>
		<atom:link href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=15095&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: Свой get\set при доступе к ассоциированным массивам внутри класса».]]></description>
		<lastBuildDate>Fri, 13 Dec 2019 06:51:47 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[AHK: Свой get\set при доступе к ассоциированным массивам внутри класса]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136833#p136833</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (Шикарная услуга плана)]]></author>
			<pubDate>Fri, 13 Dec 2019 06:51:47 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136833#p136833</guid>
		</item>
	</channel>
</rss>
